如何在依赖静态库中链接 Boost

2021-12-14 00:00:00 linker c++ boost visual-studio-2010

在 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:

  1. 我不希望图书馆的用户必须担心链接促进.
  2. 有点乱

我知道一个答案是创建一个 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 标头的方法和类.因此,只需通过在项目中定义预处理器符号 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

相关文章