如何将两个静态库链接到一个Code::Blocks项目

How to link 2 static libraries to one Code::Blocks project

本文关键字:一个 Code 项目 Blocks 静态 两个 链接      更新时间:2023-10-16

我的项目有3个代码"块":2个静态库:LibraryA和LibraryB,以及一个控制台应用程序:Client。

客户端调用LibraryA,LibraryA调用LibraryB。客户端根本不使用LibraryB。

现在,我已经很好地编译了库A,但当我试图编译客户端时,我遇到了一个错误,因为它找不到库B。以下是构建日志中导致错误的内容:

g++ -L/path/to/LibraryA -L/path/to/LibraryB/ -o bin/Debug/Client obj/Debug/main.o   LibraryA.a "LibraryB.a"
LibraryA.a(LibraryA.o): In function `LibraryA_Class::function(args)':
LibraryA.cpp:136: undefined reference to `namespace::LibraryB::LibraryB_class()'

我不知道问题出在哪里,我们非常感谢您的帮助。我在Ubuntu 14.04(VirtualBox)中使用代码::Blocks 13.12和GNU GCC编译器

提前谢谢。如果需要更多信息,请告诉我。

更新

一开始我修改了设置,去掉了-L命令,所以现在看起来是这样的(带有实际的库名称):

g++  -o bin/Debug/Client obj/Debug/main.o   ../../Wrapper/FCS_PlanTrajectory/bin/Debug/libFCS_PlanTrajectory.a ../../BAE/lib_obj/libCMetInterpIf.a
../../Wrapper/FCS_PlanTrajectory/bin/Debug/libFCS_PlanTrajectory.a(PlanTrajectory.o): In function `PlanTrajectory_Class::planSingleTrajectory(unsigned char, Position_T const&, double, double*, Position_T const&, unsigned char, std::vector<agsfirecontrol::METOCGridPoint_T, std::allocator<agsfirecontrol::METOCGridPoint_T> > const&, unsigned char, double, double, double*, Position_T*, double&, double&, unsigned int&, agsfirecontrol::AgsFireControlCompliance_T&)':
/home/angela/Work/FCS/Code/Wrapper/FCS_PlanTrajectory/src/PlanTrajectory.cpp:136: undefined reference to `agsfirecontrol::CMetInterpIf::CMetInterpIf()'
/home/angela/Work/FCS/Code/Wrapper/FCS_PlanTrajectory/src/PlanTrajectory.cpp:193: undefined reference to `agsfirecontrol::CMetInterpIf::~CMetInterpIf()'
/home/angela/Work/FCS/Code/Wrapper/FCS_PlanTrajectory/src/PlanTrajectory.cpp:193: undefined reference to `agsfirecontrol::CMetInterpIf::~CMetInterpIf()' 

LibraryA=PlanTrajectory,LibraryB=CMetInterpIf

如果有帮助的话,第二个2个错误也一直存在。顺便说一句,我使用相对路径,因为最终我需要它可以移植到另一台机器上。我可以改变到绝对路径,如果这将有助于任何

nm -C libCMetInterpIf.a | fgrep 'agsfirecontrol::CMetInterpIf::CMetInterpIf()'
nm: CEnvironment.o: no symbols
nm: CMetInterpIf.o: no symbols
nm: CMathUtilities.o: no symbols

为了成功链接,您需要为链接器提供程序所需的所有库,包括那些与其他库相关的库。你的客户端程序可能不会直接调用LibraryB中的东西,但它仍然是一个依赖项,因为LibraryA需要它。试试这个:

g++ -o bin/Debug/Client obj/Debug/main.o /path/to/LibraryA.a /path/to/LibraryB.a

实际上不需要-L/path/to/libraryA ... LibraryA.a的详细信息-您可以像上面那样直接命名文件,或者,假设您的库是使用更标准的libA.a模式命名的,您可以使用-L/path/to/libraryA ... -lA--l<something>标志搜索名为lib<something>.alib<something>.so的库。