Symbols from convenience library not getting exported in executable

前端 未结 2 1808
野性不改
野性不改 2020-12-03 12:19

I have a program, myprogram, which is linked with a static convenience library, call it libconvenience.a, which contains a function, func()

2条回答
  •  天涯浪人
    2020-12-03 13:05

    I managed to solve it. It was this note from John Calcote's excellent Autotools book that pointed me in the right direction:

    Linkers add to the binary product every object file specified explicitly on the command line, but they only extract from archives those object files that are actually referenced in the code being linked.

    To counteract this behavior, one can use the --whole-archive flag to libtool. However, this causes all the symbols from all the system libraries to be pulled in also, causing lots of double symbol definition errors. So --whole-archive needs to be right before libconvenience.a on the linker command line, and it needs to be followed by --no-whole-archive so that the other libraries aren't treated that way. This is a bit difficult since automake and libtool don't really guarantee keeping your flags in the same order on the command line, but this line in Makefile.am did the trick:

    myprogram_LDFLAGS = -Wl,--export-dynamic \
        -Wl,--whole-archive,libconvenience/libconvenience.a,--no-whole-archive
    

提交回复
热议问题