Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm trying to compile by GCC an old C program that defines and uses a function named getline, which conflicts with the function of the same name in the GNU C library. How can you compile it? Passing -ansi -D_XOPEN_SOURCE_EXTENDED to GCC give me strange errors in strings.h:

/usr/include/strings.h:74:16: error: expected identifier or '(' before '__extension__'

The relevant lines in strings.h is:

extern char *index (__const char *__s, int __c)
 __THROW __attribute_pure__ __nonnull ((1));
share|improve this question
3  
You may get more answers on stackoverflow. In the meantime, here is a related SO question about overriding function calls in C. –  jw013 Mar 7 '12 at 5:02
add comment

3 Answers 3

up vote 1 down vote accepted

While the -fno-builtin-function option to gcc works for built-in functions such as malloc and strlen (see Other built-in functions provided by GCC), it does not work for glibc built-in GNU extensions such as the getline function.

Similar to some of the solutions pointed out by jw013's link, you may try to insert #define statements in the source files to rename the getline function defined by the program, e. g. #define getline my_getline.

Here's a small code example to illustrate this approach.

/*

gcc -Wall -Wextra -o mystrdup mystrdup.c

./mystrdup

*/

#include <stdio.h> 
#include <string.h>

// mystrdup.c:14: error: conflicting types for 'strdup'
//#define strdup my_strdup

char *strdup(char *str) {
   str = str;
   return str;
}

int main (void) { 
   char *str = 0;
   char mystr[] = "Hello, mystrdup!";
   str = strdup(mystr);
   printf("%s\n", str);
   return 0;
} 
share|improve this answer
add comment

In the GCC documentation, their suggestion for dealing with this issue is to add the -ansi compiler switch because getline is a GCC extension to ansi.

share|improve this answer
add comment

Try:

gcc -fno-builtin-getline ...

For more information please see:

man gcc | less -p '-fno-builtin'
share|improve this answer
    
This doesn't work. –  Pteromys Mar 6 '12 at 8:02
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.