Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

vocaro

macrumors regular
Original poster
Mar 5, 2004
120
0
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.
 
Have you linked against getline?

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?
 
Try this command-line:
Code:
gcc -Wall getline.c

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.

Underline added for emphasis.

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?
 
Yes, getline is a nonstandard function. But it's provided by GNU libc, and OS X uses GNU libc.

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.
 
You might want to confirm that.

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.
 
I guess for now I'll just have to use fgets instead.

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.