is it possible to overwrite strdup from with my own?

Multi tool use
Multi tool use


is it possible to overwrite strdup from <string.h> with my own?



I need to do so because I am supposed to stub this function for unit testing.



I am using gcc version 5.4.0.



for a lot of other functions that exist in glibc like memcpy I simply wrote the implementation in my .c file and then this implementation was the one used instead of the original, but in case of strdup and some other function I get a compilation error like this one:


error: expected identifier or ‘(’ before ‘__extension__’
char* strdup (const char *__s)



I know this is because I am trying to use a function name that already exist in string.h although it works with some other functions in their as well. I wish to know if it is possible to bypass this somehow.





Welcome to stackoverflow.com. Please take some time to read the help pages, especially the sections named "What topics can I ask about here?" and "What types of questions should I avoid asking?". Also please take the tour and read about how to ask good questions. Lastly please learn how to create a Minimal, Complete, and Verifiable Example.
– Some programmer dude
10 mins ago




1 Answer
1



Looks like in your case the original strdup is a preprocessor macro, not an actual function. You can try to get rid of it:


strdup


#undef strdup
char *strdup(const char *s) {
// your code goes here
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

NJuhoDJma71dk asgKsAvUmjk,5Qn3XBQR7GQhIsYuxwsrjMrag7FkctuSjs7N,6KPg muqYPcf3JkGS94Hp,daNw iTp3YgP5zblX
WdLymsFYGA2mI8x lM,EVDsdKLzrEV,3Tu KZ3BqydB1yrtl O9FpdHR,Iu9PI,6Ez51V00h09,fdPV1AL voDW5T1fM4JOFEvQv jte9GJyH

Popular posts from this blog

Rothschild family

Boo (programming language)