Why am I getting a gcc "undefined reference" error trying to create shared objects?

Warren picture Warren · Nov 15, 2011 · Viewed 23.8k times · Source

Why am I getting an "undefined reference" error using gcc?

I am trying to create a shared object (.so) that exports one function, "external()". I then try to link against the .so but get "undefined reference 'external'". What am I doing wrong here?

File: external.c

int external() {
    return 5;
}

File: program.c

int external();
int main(char** argv, int* argc) {
    return external();
}

Commands:

$ gcc -fPIC -c external.c
$ gcc -shared -o libexternal.so external.o
$ gcc -L. -lexternal -o program program.c
/tmp/cc3MmhAE.o: In function `main':
program.c:(.text+0x7): undefined reference to `external'
collect2: ld returned 1 exit status

I can even run nm and see that the .so is defining 'external':

Command:

$ nm libexternal.so | grep external
0000040c T external

What am I missing here?

Answer

Flexo picture Flexo · Nov 15, 2011

Recent versions of gcc/ld default to linking with --as-needed.

This means if you write -lexternal before the C file the library will automatically get excluded (the order matters when testing if things are "needed" like this)

You can fix this with either of:

  • gcc -L. -o program program.c -lexternal
  • gcc -L. -Wl,--no-as-needed -lexternal -o program program.c

The latter of which passes --no-as-needed to the linker, which would cause the library to still be linked, even if you didn't call external() from it.

Note: -Wl,--no-as-needed isn't applied globally to everything that's linked, it's only applied to things that follow it in the command line order. So -lexternal -Wl,--no-as-needed also wouldn't work. This does mean that you can mix and match behaviours though, for example gcc -L. -Wl,--no-as-needed -lexternal -Wl,--as-needed -o program program.c -lmightneed would always link against external, but only link against mightneed if one or both of program.c/libexternal.so caused it to be needed.