如何在依赖的静态库中链接Boost [英] How to link Boost in a dependent static library

查看:413
本文介绍了如何在依赖的静态库中链接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:

  1. 我不希望图书馆的用户 不必担心链接 促进.
  2. 很乱
  1. I don't want users of the library to have to worry about linking in boost.
  2. It is messy

我知道答案是创建一个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

这篇关于如何在依赖的静态库中链接Boost的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆