简体   繁体   English

Linux gcc 链接问题

[英]Linux gcc linking issues

I have two libraries , named ( lib1 and lib2) , both are not dependent on each other.我有两个名为 ( lib1 和 lib2) 的库,它们都不相互依赖。 I do not have control over how these libraries are built.我无法控制这些库的构建方式。 When I try to link them the order of linking dictates on different behaviors within the final executable,当我尝试链接它们时,链接顺序决定了最终可执行文件中的不同行为,

For example:例如:

If I link them as [1]如果我将它们链接为 [1]

gcc $(CC_FLAGS) -o app.out -llib1 -llib2 

everything works fine一切正常

But, if I link them as following [2]但是,如果我将它们链接如下 [2]

gcc $(CC_FLAGS) -o app.out -llib2 -llib1 

A segmentation fault occurs during executable run.在可执行文件运行期间发生分段错误。

Any advice or pointer on the cause of this problem would be helpful.有关此问题原因的任何建议或指示都会有所帮助。

Update:更新:

Both of these libraries are dependent on another dynamic library, which is Apache Thrift ( Version 0.11.0) the segfault occurs on lib1 if it is compiled [1] option above, and exception occurs on lib2 if it compiled with option [2] above.这两个库都依赖于另一个动态库,即 Apache Thrift(版本 0.11.0),如果编译上述 [1] 选项,则在 lib1 上发生段错误,如果使用上述选项 [2] 编译,则在 lib2 上发生异常.

Update 2更新 2

The issue is due to global namespace violations between the libraries.该问题是由于库之间的全局命名空间冲突造成的。 Since thrift uses IDL mechanism to generate source files, Both libraries ( somehow ) defined the same namespace for their IDL definitions, hence such behavior was observed.由于 thrift 使用 IDL 机制来生成源文件,两个库(不知何故)为其 IDL 定义定义了相同的命名空间,因此观察到了这种行为。 Accepting the answer below as correct one since it addresses the question indirectly.接受以下答案为正确答案,因为它间接解决了问题。

Thank you.谢谢你。

Is it possible that there resides a symbol (same name) in both of those libs?这两个库中是否有可能存在一个符号(同名)?

Which one the linker will use to resolve an usage of such a symbol depends on the order (not sure where I've read this).链接器将使用哪一个来解析此类符号的使用取决于顺序(不确定我在哪里阅读过)。

(HINT: This is just a guess) (提示:这只是一个猜测)

So lets assume lib1 defines this function:所以让我们假设 lib1 定义了这个函数:

void foo( int, char* );

And lib2 defines:而 lib2 定义了:

void foo( int );

And somewhere we have a call like:在某个地方,我们有一个类似的电话:

foo( 42 );

Now if the linker finds the definition of lib1, this foo most probably will get funny garbage as its 2nd argument.现在如果链接器找到 lib1 的定义,这个 foo 很可能会得到有趣的垃圾作为它的第二个参数。

Update (reaction to uptdate of question)更新(对问题更新的反应)

It is hard to make precise statements with the insight we have.以我们所拥有的洞察力,很难做出准确的陈述。 So I can only point out some guesses and hope they'll help you.所以我只能指出一些猜测,希望它们能帮助你。 So sorry for my inaccuracy here.很抱歉我在这里的不准确。 But I've no idea how else I could continue:但我不知道我还能如何继续:

Guess 1 :猜一猜

Update says: " segfault occurs on lib1 if it is compiled [1] option above, and exception occurs on lib2 if it compiled with option [2] "更新说:“如果使用上面的 [1] 选项编译 lib1,则会发生段错误,如果使用选项 [2] 编译,则会在 lib2 上发生异常

ABI incompatible exception handling in lib1? lib1 中的 ABI 不兼容异常处理? Something like this ?这样的东西?

Guess 2 (Or should I name it "story"):猜猜2 (或者我应该将其命名为“故事”):

Our code inherits (and implements) an abstract class declared in thrift header.我们的代码继承(并实现)了一个在 thrift 头文件中声明的抽象类。 This impl then gets passed to lib1/lib2 (through our conflicting symbol) which perform calls to this impl.这个 impl 然后被传递给 lib1/lib2(通过我们的冲突符号),它执行对这个 impl 的调用。 But lib1 used a different thrift header during compilation (or used conditional compiling, or used another version of thrift).但是 lib1 在编译期间使用了不同的 thrift 头文件(或者使用了条件编译,或者使用了另一个版本的 thrift)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM