20

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?

3
  • 1
    Does gcc -L. -o program program.c -lexternal fix it?
    – Flexo
    Nov 15, 2011 at 17:28
  • Ah, yes it does, thank you, but why? Why does -l have to be at the end?
    – Warren
    Nov 15, 2011 at 17:35
  • @Warren, do you know of a gcc flag --echo-ld-options i.e. tell me what you're doing ?
    – denis
    Nov 20, 2012 at 14:04

1 Answer 1

42

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.

4
  • 3
    Thanks for also adding "--as-needed/--no-as-needed" explanation. This is exactly what I was looking for.
    – Warren
    Nov 15, 2011 at 17:42
  • 1
    Adding on, when compiling a dynamic library (A.so) which also links to other dynamic library (B.so). And if linking to the compiled library (A.so) via dlopen. It is possible for the dlopen to just silently fail. Without error. As the 2nd library is not loaded properly (B.so); This alone costed me a month of productivity time, as we were migrating to 64-bit, to finally trace down this question as a "possible cause" for a project of mine. So be wary of it. Jan 3, 2013 at 9:12
  • "Recent versions" - any chance you know exactly which one introduced this change? Jun 11, 2016 at 6:14
  • @NathanOsman not off hand and different distros patched it in earlier/later as they saw fit. I'd guess GCC 4.2 era being approximately the cutover.
    – Flexo
    Jun 11, 2016 at 8:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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