简体   繁体   English

创建库时的GCC C ++链接增强

[英]GCC C++ linking boost when creating a library

I suppose my question is what is the correct way to statically link to Boost when creating an archive, when Boost is just compiled and stored in my home directory, not in the system? 我想我的问题是,当仅将Boost编译并存储在我的主目录中而不是在系统中时,创建存档时静态链接到Boost的正确方法是什么? The idea behind creating my program as a library is that I will be able to redistribute the library file for others to link against in their programs. 将我的程序创建为库的想法是,我将能够重新分发该库文件,以供其他人链接到他们的程序中。

I want to make a C++ library file I can link to on Linux. 我想制作一个可以在Linux上链接到的C ++库文件。 I don't really care if it's static or dynamic, but static might make it easier for me. 我并不在乎它是静态的还是动态的,但是静态可能会让我更轻松。

The issue I'm having is that the examples I find don't tell you how to link other libraries in your library. 我遇到的问题是,我发现的示例没有告诉您如何链接库中的其他库。 My library uses Boost, and I have built Boost but I don't have permission to move it outside my user area. 我的图书馆使用Boost,并且我已经构建了Boost,但没有权限将其移动到用户区域之外。

I've tried making a static archive like this: 我试过制作这样的静态档案:

Compiling the cpp files: 编译cpp文件:

g++ -O3 -ffast-math -Wall -Wextra -g -I./ -I../../boost/ -c BuddyManager.cpp -o build/BuddyManager.o g ++ -O3-快速数学-Wall -Wextra -g -I./ -I ../../ boost / -c BuddyManager.cpp -o build / BuddyManager.o

Creating a static archive: 创建静态档案:

ar rcs build/libppmi.a ../../boost/lib/libboost_system.a build/Application.o build/DataBlock.o (etc with all my object files listed after) ar rcs build / libppmi.a ../../boost/lib/libboost_system.a build / Application.o build / DataBlock.o (等,之后列出所有我的目标文件)

But it gives undefined reference errors when I later link to the .a file I created. 但是,当我以后链接到我创建的.a文件时,它会提供未定义的参考错误。

I managed to create my library by building a shared library SO file on Mac, but on that system Boost was installed in the standard system location. 我设法通过在Mac上构建共享库SO文件来创建库,但是在该系统上,Boost安装在标准系统位置。 When I built a shared library on Linux I got the error "hidden symbol `__dso_handle' in /usr/lib/crtbegin.o is referenced by DSO" and I often had problems with it not being able to find the Boost .SO files. 当我在Linux上建立共享库时,出现错误“ DSO引用了/usr/lib/crtbegin.o中的隐藏符号'__dso_handle”,并且我经常遇到无法找到Boost .SO文件的问题。 I therefore decided to change to static linking. 因此,我决定更改为静态链接。

The system is running on Scientific Linux SL release 5.3 (Boron) and I'm using gcc-4.6.2. 该系统在Scientific Linux SL版本5.3(Boron)上运行,我使用的是gcc-4.6.2。 However, I want this to be portable to pretty much all versions of GCC. 但是,我希望它可以移植到几乎所有版本的GCC。

Thanks. 谢谢。 If anything isn't clear or you want more details please ask. 如果不清楚,或者您想了解更多详细信息,请询问。

Some of the missing references are: 一些缺少的参考资料是:

undefined reference to `boost::thread::interrupt()' 未定义对`boost :: thread :: interrupt()的引用

boost/boost/thread/pthread/thread_data.hpp:247: undefined reference to `boost::this_thread::hiden::sleep_until(timespec const&)' boost / boost / thread / pthread / thread_data.hpp:247:未定义对`boost :: this_thread :: hiden :: sleep_until(timespec const&)'的引用

boost/boost/archive/detail/iserializer.hpp:158: undefined reference to `boost::archive::detail::basic_iserializer::~basic_iserializer()' boost / boost / archive / detail / iserializer.hpp:158:未定义对`boost :: archive :: detail :: basic_iserializer ::〜basic_iserializer()的引用

They all seem to be in Boost. 他们似乎都在助推器中。 It can't find the references that should be in the Boost archives. 它找不到应该在Boost档案中的引用。

For more information this is what I am compiling with: 有关更多信息,这是我正在编译的内容:

ar rcs build/libppmi.a ../../boost/lib/libboost_system.a ../../boost/lib/libboost_iostreams.a ../../boost/lib/libboost_serialization.a ../../boost/lib/libboost_thread.a build/Application.o build/DataBlock.o build/Iteration.o build/Launcher.o build/Message.o build/Network.o build/Processes.o build/DataBlockManager.o build/Communicator.o build/NetworkServer.o build/NetworkClientSession.o build/NetworkMessage.o build/NetworkConnection.o build/Threadable.o build/BuddyManager.o ar rcs build / libppmi.a ../../boost/lib/libboost_system.a ../../boost/lib/libboost_iostreams.a ../../boost/lib/libboost_serialization.a ../。 ./boost/lib/libboost_thread.a build / Application.o build / DataBlock.o build / Iteration.o build / Launcher.o build / Message.o build / Network.o build / Processes.o build / DataBlockManager.o build /Communicator.o build / NetworkServer.o build / NetworkClientSession.o build / NetworkMessage.o build / NetworkConnection.o build / Threadable.o build / BuddyManager.o

This works ok. 这样可以。 It's when I use the archive it breaks. 当我使用存档时,它会中断。 I have also tried extracting the .o files from the boost libraries but this gives other linker errors about pthread libraries. 我也尝试过从boost库中提取.o文件,但这会带来其他有关pthread库的链接器错误。

You're creating an archive of archives, but when the linker looks in an archive it expects object files. 您正在创建档案的档案,但是当链接程序在档案中查找时,它将需要目标文件。 The linker will look in libppmi.a and skip all the non-object files, including libboost_system.a , because it doesn't know what to do with them, and so won't find any Boost symbols. 链接器将在libppmi.alibppmi.a并跳过所有非对象文件,包括libboost_system.a ,因为它不知道如何处理它们,因此不会找到任何Boost符号。 You can confirm that by linking with -t (if you're linking using gcc or g++ then use -Wl,-t to pass -t through to the linker) which prints the name of each file as the linker processes it, and you'll see it doesn't process the nested archives. 您可以通过与-t链接进行确认(如果您要使用gccg++进行链接,然后使用-Wl,-t-t传递给链接器),当链接器处理该文件时,它会打印每个文件的名称,然后会看到它不处理嵌套档案。

If you want to include all the Boost code in your archive then you need to list the individual objects (either by referring to them directly where you built Boost or by extracting them from the Boost archives then adding them to your new archive.) 如果要在存档中包含所有Boost代码,则需要列出各个对象(通过直接在构建Boost的位置引用它们,或从Boost存档中提取它们,然后将其添加到新存档中。)

This works ok. 这样可以。 It's when I use the archive it breaks. 当我使用存档时,它会中断。

Of course, all that does it create an archive containing the files you listed. 当然,所有这些操作都会创建一个包含您列出的文件的存档。 Creating an archive just involves putting a load of separate files into one big file and you can put almsot any file type in there. 创建存档只需要将一个单独的文件加载到一个大文件中,就可以在其中放入任何文件类型。 It can't really go wrong, but that doesn't mean the result is correct or usable. 它不可能真的出错,但这并不意味着结果正确或可用。

I have also tried extracting the .o files from the boost libraries but this gives other linker errors about pthread libraries. 我也尝试过从boost库中提取.o文件,但这会带来其他有关pthread库的链接器错误。

That's the right approach, but you've hit a different problem, so solve that instead of giving up on the approach. 这是正确的方法,但是您遇到了另一个问题,因此请解决该问题,而不要放弃该方法。 If you're using Boost.Thread then you need to compile and link with -pthread 如果您使用Boost.Thread,则需要使用-pthread进行编译和链接

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

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