如何在依赖的静态库中链接 Boost
在 MS Visual C++ 2010 中
In MS Visual C++ 2010
我的解决方案中有一个 C++ 项目,它使用了 boost 并且运行良好.
I had a single C++ project in my solution which used boost and worked perfectly.
然后我决定将这个项目转换成一个静态库并创建一个依赖于这个静态库的新项目.
I then decided to convert this project into a static library and create a new project which depends on this static library.
现在,我转换后的静态库构建没有错误和警告(编译器和链接器)但新项目编译但不链接.
Now, my converted static library builds without errors and warnings (compiler and linker) but the new project compiles but does not link.
我得到:
1>LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc100-mt-1_45.lib'
作为测试,我将完整目录路径添加到该库的链接器选项中...然后它抱怨
As a test I added the full directory path to the linker options for this library... and then it complained about
1>LINK : fatal error LNK1104: cannot open file 'libboost_date_time-vc100-mt-1_45.lib'
我现在已经添加了所有库的完整路径,它现在可以构建并运行.
I have now added complete paths to all the libraries and it now builds and run.
我对这个解决方案不满意,因为:
I am not happy with this solution because:
- 我不希望图书馆的用户不得不担心链接促进.
- 很乱
我知道答案是创建一个 DLL,但有没有办法静态地执行此操作并将链接保持在我的静态库级别.
I know an answer would be to create a DLL but is there a way to do this statically and keep the linking at my static library level.
如果我告诉 .exe 链接器明确忽略 boost 库,那么一切都可以,除了 .exe 根本不必担心 boost.
If I tell the .exe linker to ignore the boost libs explicitly then it all is ok except the .exe should not have to worry about boost at all.
/NODEFAULTLIB:"libboost_thread-vc100-mt-1_45.lib" /NODEFAULTLIB:"libboost_date_time-vc100-mt-1_45.lib"
推荐答案
显然你不需要 .libs,因为你的 exe 也链接没有它们.您似乎正在使用 boost-only 方法和类.因此,只需通过在项目中定义预处理器符号 BOOST_ALL_NO_LIB 来告诉 boost 禁用自动链接.
Apparently you don't need the .libs, as your exe also links without them. You seem to be using boost header-only methods and classes. So just tell boost to disable auto linking by defining the preprocessor symbol BOOST_ALL_NO_LIB in your project.
如果您想通过包含所有 boost 来使您的 .lib 变得不必要的大,这个问题似乎有一个答案(我自己从未真正尝试过):将静态库链接到其他静态库
If you want to make your .lib unnecessary big by including all of boost, this question seems to hold an answer (which I never really tried myself): Linking static libraries to other static libraries
相关文章