我可以将未解析的引用链接到中止吗?

fal*_*tro 12 c++ linker gcc

我正在尝试为一个相当大的项目的一小部分写一些小测试.不幸的是,如果不将整个项目链接在一起,试图链接这个野兽是相当不可能的,这是我不想做的(这是一个非常复杂的系统,用于查找所有依赖项和内容,我不想干涉它).

现在,我肯定知道在我的测试期间不会调用引用函数的函数,恰好恰好是与我测试的东西共享文件的函数的一部分.

有没有办法简单地将这些未解决的引用链接到,比方说,中止,或者什么?或者是否有一个工具可以创建适当的存根对象文件,其中所有调用都会导致中止,给定我拥有的目标文件集?

我使用gcc(g ++)进行编译/链接,版本3.4.4.平台是unix(solaris/sparc,如果这很重要).

P S*_*ved 15

您可以告诉链接器忽略未解析的符号.我找不到将它们链接到abort或类似的选项.

我想,忽略目标文件中未解析符号的策略是最自然的:

gcc -Wl,--unresolved-symbols=ignore-in-object-files  obj.o another.o etc.o
Run Code Online (Sandbox Code Playgroud)

其他选项包括(引用man ld):

   --unresolved-symbols=method
       Determine how to handle unresolved symbols.  There are four possi-
       ble values for method:

       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  con-
       trolled 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-sym-
       bols can change this to a warning.
Run Code Online (Sandbox Code Playgroud)

在我的Linux系统上尝试调用未解析的函数导致"分段错误".

  • --warn-unresolved-symbols对我来说是完美的,这样我(希望)也会注意到是否有一些不应该解决的问题. (2认同)