简体   繁体   English

如何在Eclipse CDT中将静态库链接到静态库

[英]How to link static library into static library in Eclipse CDT

I have 3 projects A, B and C. A is universal and used by B and eventually C. B is a bit more special and up to now only used by C. 我有3个项目A,B和C. A是通用的,由B使用,最终使用C. B有点特别,到目前为止仅由C使用。

Now as I want A and B to be reused as much as possible I thought of both being static libraries. 现在我希望尽可能多地重用A和B,我认为它们都是静态库。

So I link A and B to static libraries and C to an executable, but when I want to link C it gives me some undefined reference errors on A functions. 所以我将A和B链接到静态库,将C链接到可执行文件,但是当我想链接C时,它给了我一些关于A函数的undefined reference错误。 So I added the library A to the linker options to resolve this but it did not help. 所以我将库A添加到链接器选项以解决此问题,但它没有帮助。

So I thought: when I want to deploy B then I also will have to deploy A to be able to link. 所以我想:当我想部署B时,我也必须部署A才能链接。 Is there a way to link the static library A into static library B so I do not have to deploy 2 files? 有没有办法将静态库A链接到静态库B,所以我不必部署2个文件?

And if so, how do I achieve this with Eclipse CDT, because I cant find a place where to define additional targets for the archiver. 如果是这样,我如何使用Eclipse CDT实现这一目标,因为我无法找到为归档程序定义其他目标的位置。

Addition 加成

I am working under linux but the things should also work under windows. 我在linux下工作,但事情也应该在Windows下工作。 But mainly I need a solution for linux to keep going with the development. 但主要是我需要一个linux的解决方案来继续开发。

EDIT 编辑

Right now I "solved" the problem by linking A and B into static libraries and then use the remaining *.o files to link them into the executable but I do not consider this as good. 现在我通过将A和B链接到静态库然后使用剩余的* .o文件将它们链接到可执行文件来“解决”问题,但我不认为这很好。

change your link order. 更改您的链接顺序。 C use B, and B use A. C does not use A directly, right? C使用B,而B使用A. C不直接使用A,对吗?

you write: 你写:

gcc -o C -lA -lB

it will not work. 不起作用。 you write 你写

gcc -o C -lB -lA

it works. 有用。

if CDT, you could go to option->C/C++ Build->setting->XXX C linker->Libraries , and change A and B's order. 如果是CDT,您可以转到选项 - > C / C ++构建 - >设置 - > XXX C链接器 - >库 ,并更改A和B的顺序。

Another way: use -u option. 另一种方法:使用-u选项。

see this: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html BUT, I NEVER succeeded. 看到这个: http//gcc.gnu.org/onlinedocs/gcc/Link-Options.html但是,我从未成功过。 May anyone tell me how to use -u option? 谁能告诉我如何使用-u选项?

a static library cannot be linked with another static library. 静态库不能与另一个静态库链接。 I think because a static library is not an executable code and so full linking process (mainly references resolution) is not done. 我认为因为静态库不是可执行代码,所以没有完成完整的链接过程(主要是引用解析)。 It's done only for dynamic libraries and executables. 它仅适用于动态库和可执行文件。

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

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