gpt4 book ai didi

c++ - 我可以将 Unresolved 引用链接到中止吗?

转载 作者:IT老高 更新时间:2023-10-28 23:15:25 24 4
gpt4 key购买 nike

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

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

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

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

最佳答案

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

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

gcc -Wl,--unresolved-symbols=ignore-in-object-files  obj.o another.o etc.o

其他选项包括(引用 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.

在我的 Linux 系统上尝试调用未解析的函数会导致“段错误”。

关于c++ - 我可以将 Unresolved 引用链接到中止吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2006886/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com