2010-01-05 43 views
12

我正在尝试为相当大的项目的一小部分编写一些小测试。试图链接这个野兽是不幸的,如果没有将整个项目链接在一起,这是我不想做的事情(这是一个非常复杂的系统,可以找到所有的依赖关系和东西,而且我不会干涉它)。现在我可以将未解析的引用链接到中止吗?

,我确切知道所引用的功能将不会在我的测试中被调用的函数,则恰好是与东西,我做的测试文件共享功能的一部分。

有没有办法简单地链接到这些未解决的参考,让我们说,中止,还是什么?或者是否有一种工具创建了适当的存根目标文件,其中所有调用都会导致中止,给定了我拥有的一组目标文件?

我使用用于编译/链接,版本3.4.4的gcc(克++)。平台是unix(solaris/sparc如果这很重要)。

我能想到的

回答

15

你可以告诉链接器忽略未解决的符号。我找不到将其链接到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系统尝试将未解析的函数调用结果称为“分段错误”。

+0

segmantation故障会做我想... :)谢谢,我会尝试--warn-unresolved-符号以及 – falstro 2010-01-05 15:39:14

+0

酷。这是很好的信息。 – 2010-01-05 15:42:16

+2

--warn-unresolved-symbols对我来说是完美的,那样我会(希望)也会注意到有什么东西不应该被解决。 – falstro 2010-01-05 15:44:00

1

那么一种方法是首先编译.o文件,为你的资料库。

然后用像nm工具(在* nix系统中很常见),让所有的符号,在nm,所有的“外部”(未在此的.o发现又名者)是型ü(它可能不同于非GNU版本的nm请参阅您的文档)。

如果库是一个所有的源文件,那么很简单,几乎U型的所有符号将是要么在另一个库中发现了一个功能或将在链接时没有解决。如果你的图书馆要多于一个源文件,这会稍微复杂一点,因为你将会有源文件间的依赖关系。

所以,现在你有AA手段创造无法解析外部的潜在名单,那么你就可以创建具有为每一个,你可以用这样的填充存根符号“test_stub.c”:

void some_func() { abort(); } 

其中some_func是将无法解析的外部。编译并链接到您的库,所有调用都会导致中止。

+0

很棒的技巧,这是从一个单一的.o文件遗憾的是远,所以它需要一些工作......但我想没有任何一个位脚本不会解决。 – falstro 2010-01-05 15:27:58

+0

是啊,我有点小小的困惑应该可以走很长的路。如果你所有的库函数都有一些通用的命名约定来让你分离出libc引用,那将是非常容易的。 – 2010-01-05 15:29:04

+0

它的C++不幸(name-mangling ftw!):(你知道是否有办法将对象链接到单个对象中?(有点像我想的ar,但我猜nm不会在.a文件上工作?) – falstro 2010-01-05 15:32:06

1

试图编译下面的程序

#include <iostream> 

extern int bar(); 

int foo() 
{ 
    return bar() + 3; 
} 

int main() 
{ 
    std::cout << "Hello, world!" << std::endl; 
    // std::cout << foo() << std::endl; 

    return 0; 
} 

结果

$ g++ -o main main.cc 
/tmp/ccyvuYPK.o: In function `foo()': 
main.cc:(.text+0x5): undefined reference to `bar()' 
collect2: ld returned 1 exit status

但是我们可以告诉链接忽略未解决的符号,并运行它只是罚款:

$ g++ -Wl,--unresolved-symbols=ignore-all -o main main.cc 
$ ./main 
Hello, world!

说一些未解决的功能通过您的测试工具(通过取消对foo的调用来模拟此工具),它将编译并链接正常,但在执行程序时会出现段错误。请务必ulimit -c unlimited,以便获得core

0

尝试GCC alias属性:

/* cannot directly alias to yet undefined symbols, 
* so need an intermediate function. 
*/ 
static void do_abort() { abort(); } 

void func0() __attribute__ ((weak, alias ("do_abort"))); 
void func1() __attribute__ ((weak, alias ("do_abort"))); 
... 
相关问题