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

感情迁移 提交于 2019-11-26 05:34:32

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/8140494/why-am-i-getting-a-gcc-undefined-reference-error-trying-to-create-shared-objec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!