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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4736877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 16:18:58  来源:igfitidea点击:

How to link Boost in a dependent static library

c++visual-studio-2010boostlinker

提问by T33C

In MS Visual C++ 2010

在 MS Visual C++ 2010 中

I had a single C++ project in my solution which used boost and worked perfectly.

我的解决方案中有一个 C++ 项目,它使用了 boost 并且运行良好。

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.

现在,我转换的静态库构建没有错误和警告(编译器和链接器),但新项目编译但不链接。

I am getting:

我正进入(状态:

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. I don't want users of the library to have to worry about linking in boost.
  2. It is messy
  1. 我不希望图书馆的用户不必担心在 boost 中链接。
  2. 很乱

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.

我知道一个答案是创建一个 DLL,但是有没有办法静态地做到这一点并将链接保持在我的静态库级别。

Edit:

编辑:

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.

如果我告诉 .exe 链接器显式忽略 boost 库,那么一切都可以,除了 .exe 根本不必担心 boost。

/NODEFAULTLIB:"libboost_thread-vc100-mt-1_45.lib" /NODEFAULTLIB:"libboost_date_time-vc100-mt-1_45.lib"

回答by user52875

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.

显然你不需要 .libs,因为你的 exe 也没有它们链接。您似乎在使用仅包含 boost 标头的方法和类。因此,只需通过在项目中定义预处理器符号 BOOST_ALL_NO_LIB 来告诉 boost 禁用自动链接。

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

如果您想通过包含所有 boost 使您的 .lib 变得不必要,这个问题似乎有一个答案(我自己从未真正尝试过):将静态库链接到其他静态库

回答by Daniel Gehriger

When building your library, you can include the boost libraries in yours. To do so, in VisualStudio's Librarian > Generalproperty page, list your boost libraries as Additional Dependencies.

在构建库时,您可以将 boost 库包含在您的库中。为此,在 VisualStudio 的Librarian > General属性页中,将您的 boost 库列为Additional Dependencies.

However, there may be a problem if your clients use boost themselves, and statically link to it (especially a different version than the one you are using).

但是,如果您的客户自己使用 boost 并静态链接到它(尤其是与您使用的版本不同的版本),则可能会出现问题。

回答by yasouser

Did you build boost library? There are certain libraries in Boost that needs to be compiled. In case if you haven't done that, refer to "Getting started in Windows" on how to build the Boost library.

你建立了boost库吗?Boost 中有一些需要编译的库。如果您还没有这样做,请参阅“ Windows 入门”以了解如何构建 Boost 库。

EDIT-1:Boost can be built both as a static and dynamically loadable (dll) libraries.

EDIT-1:Boost 可以构建为静态和动态可加载 (dll) 库。

EDIT-2:If you have already built Boost, then the answer by @Daniel Gehriger tells you how to add it in VS.

EDIT-2:如果您已经构建了 Boost,那么@Daniel Gehriger 的回答会告诉您如何在 VS 中添加它。