gettext: undefined symbol

Discussion in 'Mac Programming' started by vocaro, Apr 7, 2010.

  1. vocaro macrumors regular

    Joined:
    Mar 5, 2004
    #1
    I'm trying to use getline in a C program on OS X 10.5.8, but I keep getting linker errors. Here's a simple test:

    Code:
    #include <stdio.h>
    
    int main() {
    	char buffer[100];
    	size_t size = 100;
      	FILE* file = fopen("getline.c","r");
    
    	if (file == NULL) {
    		perror("Cannot open file");
    		return 1;
    	}
    
    	getline(&buffer, &size, file);
    
    	return 0;
    }
    This fails to build:

    Code:
    $ gcc getline.c 
    Undefined symbols:
      "_getline", referenced from:
          _main in ccIz4nCZ.o
    ld: symbol(s) not found
    collect2: ld returned 1 exit status
    Any suggestions? Thanks.
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
  3. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #3
    Try this command-line:
    Code:
    gcc -Wall getline.c
    
    From the getline man page you referenced:
    Underline added for emphasis.
     
  4. vocaro thread starter macrumors regular

    Joined:
    Mar 5, 2004
    #4
    No, I'm not linking against anything.

    There's no "libgetline" on my OS X system. Am I supposed to install it manually? If so, where do I obtain the source to libgetline? A Google search didn't turn up anything.

    I thought getline is in GNU libc, which is linked automatically, is it not?
     
  5. vocaro thread starter macrumors regular

    Joined:
    Mar 5, 2004
    #5
    I get:

    getline.c:13: warning: implicit declaration of function ‘getline’

    which I don't understand because getline is supposed to be defined in stdio.h. At least, that's what the GNU libc manual says.

    Yes, getline is a nonstandard function. But it's provided by GNU libc, and OS X uses GNU libc. So the fact that it's a GNU extension shouldn't be a problem because I'm using GNU libraries, correct?
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    You might want to confirm that. You could look in the headers. You could compile a simple C-standard (not GNU-standard) program and see what dylibs it uses, then apply the nm command to those dylibs.
    Code:
    gcc helloWorld.c
    otool -L a.out
    nm /usr/lib/libSystem.dylib
    
    Just because the toolchain is GCC based does not require that libc also be GCC based. In any case, even if libc were GCC based, it's entirely possible for the vendor (i.e. Apple) to remove elements it doesn't want, e.g. if it doesn't want non-standard functions.
     
  7. vocaro thread starter macrumors regular

    Joined:
    Mar 5, 2004
    #7
    Yeah, I was making the wrong assumption. (I guess I'm too used to the Linux world, where everything everywhere uses GNU libc.) Mac OS X uses FreeBSD's libc, which would certainly explain why getline cannot be found.

    Interestingly enough, though, getline is now a standard. It's part of the POSIX:2008 spec. Unfortunately, even though Snow Leopard is certified POSIX-compatible, it only conforms to POSIX:2001. Maybe in OS X 10.9...

    I guess for now I'll just have to use fgets instead.
     
  8. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #8
    Or grab the GNU source to getline(). You could build it as a dylib, or static link it if distribution isn't an issue. The GNU licenses only come into play when distribution occurs.
     

Share This Page