linker option to list libraries used

后端 未结 1 596
野趣味
野趣味 2020-12-17 17:40

I am working on a Linux platform and using the GNU C++ compiler. I am trying to resolve a link error that says some symbols are undefined. I can go find libraries with the

相关标签:
1条回答
  • 2020-12-17 18:18

    If you get an undefined symbol error it means you forgot to link some library, knowing which libraries you link to will probably not be as useful as you may think, because obviously the symbol is missing from those libraries, however you can use the -print-file-name=library option to find out which path gcc would use to link a library, example:

    $ gcc -print-file-name=libc.a
    /usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/../../../libc.a
    

    Also passing --trace to the linker shows a similar output

    gcc -Wl,--trace myprog.c -o myprog -L. -lmylib
    -lmylib (./libmylib.a)
    -lgcc_s (/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/libgcc_s.so)
    ....
    

    (Note in the above that --trace is an argument to the linker so it goes in -Wl. gcc invoked for linking won't emit anything useful for --trace as an argument to gcc its self).

    You could also use ldd after you successfully build the program to find out which dynamically linked libraries were used, its output looks like this:

    ldd `which ls`
    linux-vdso.so.1 =>  (0x00007fff7ffff000)
    libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007f2e8ea93000)
    librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f2e8e88b000)
    libacl.so.1 => /lib/x86_64-linux-gnu/libacl.so.1 (0x00007f2e8e682000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f2e8e2ee000)
    ....
    
    0 讨论(0)
提交回复
热议问题