G ++连接C code和C ++ code时,链接顺序依赖性 [英] g++ linking order dependency when linking c code to c++ code

查看:187
本文介绍了G ++连接C code和C ++ code时,链接顺序依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此之前,今天我一直认为,该对象和库都通过了以G ++在链接阶段是不重要的。那么,今天,我想从C ++ code链接到c code。我包所有的C头文件在externC块,但连接仍然有困难找我就知道是在C对象档案符号。

Prior to today I had always believed that the order that objects and libraries were passed to g++ during the linking stage was unimportant. Then, today, I tried to link from c++ code to c code. I wrapped all the C headers in an extern "C" block but the linker still had difficulties finding symbols which I knew were in the C object archives.

困惑,我创建了一个比较简单的例子来链接错误,但很多隔离到我惊讶的是,简单的例子,没有任何问题联系在一起的。

Perplexed, I created a relatively simple example to isolate the linking error but much to my surprise, the simpler example linked without any problems.

一个小试验和错误之后,我发现,通过模拟在简单示例中使用的连接模式,我能得到的主要code链接确定。该模式是对象code首先,对象档案第二例如:

After a little trial and error, I found that by emulating the linking pattern used in the simple example, I could get the main code to link OK. The pattern was object code first, object archives second eg:

G ++ -o serverCpp serverCpp.o algoC.o libcrypto.a

任何人都可以解释为什么这可能是这样一些轻?链接普通C ++ code时,我从来没有见过这个问题。

Can anyone shed some light on why this might be so?. I've never seen this problem when linking ordinary c++ code.

推荐答案

您指定目标文件和库的顺序是GCC非常重要的 - 如果你还没有被这个被咬之前你有过令人陶醉的生活。链接程序搜索它们出现的顺序符号,所以如果你有一个包含调用库函数源文件,你需要把它放在库之前,或连接不知道它有解决它。复杂的使用库可能意味着你必须不止一次指定库,这是一个皇家疼痛得到正确的。

The order you specify object files and libraries is VERY important in GCC - if you haven't been bitten by this before you have lead a charmed life. The linker searches symbols in the order that they appear, so if you have a source file that contains a call to a library function, you need to put it before the library, or the linker won't know that it has to resolve it. Complex use of libraries can mean that you have to specify the library more than once, which is a royal pain to get right.

这篇关于G ++连接C code和C ++ code时,链接顺序依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆