如何在目标文件中使用“U”未定义的符号类型的程序,编译时没有任何链接器错误?

时间:2015-11-05 14:16:09

标签: c++ linker linker-errors symbols undefined-symbol

我观察到当我编译程序时(在linux上),目标文件中的一些符号未定义为“U”(U表示该函数被引用,但未定义)。

例:
XXX.cpp.o:
         U _ZN5NJs16CRapidD2Ev
         U _ZN5NJs7CWriter6
         U _ZN9NGeo22TConvertEi

程序编译时没有任何链接器错误。链接是如何发生在这里的? “U”符号类型的确切含义是什么?

2 个答案:

答案 0 :(得分:2)

如果您希望链接器检查未定义的符号并报告错误,可以使用选项--no-undefined

我认为没有使用这些符号,这就是链接器忽略h​​tem的原因。

答案 1 :(得分:1)

链接器选项" -Wl, - unresolved-symbols = ignore-in-object-files"在创建可执行文件时忽略目标文件中的未定义符号

见" man ld"

- 未解决的符号=方法            确定如何处理未解析的符号。方法有四种可能的值:

       ignore-all
           Do not report any unresolved symbols.

       report-all
           Report all unresolved symbols.  This is the default.

       ignore-in-object-files
           Report unresolved symbols that are contained in shared libraries, but ignore them if they come from regular object files.

       ignore-in-shared-libs
           Report unresolved symbols that come from regular object files, but ignore them if they come from shared libraries.  This can be useful when
           creating a dynamic binary and it is known that all the shared libraries that it should be referencing are included on the linker's command line.

       The behaviour for shared libraries on their own can also be controlled by the --[no-]allow-shlib-undefined option.

       Normally the linker will generate an error message for each reported unresolved symbol but the option --warn-unresolved-symbols can change this to a
       warning.