c++ - 使用静态版本的 boost::filesystem 时出现有趣的 LNK1104 错误

标签 c++ visual-c++ boost linker linker-errors

我正在尝试测试一些使用 boost::filesystem 的代码为了各种各样的事情。
我为 vc11 构建了 boost 1.51 的静态版本,预期的库按预期出现。

以下是我使用的标志:
b2 --with-filesystem --build-type=complete --build-dir=.\build link=static runtime-link=static

这是它生成的文件列表:

libboost_filesystem-vc110-mt-s-1_51.lib
libboost_filesystem-vc110-mt-sgd-1_51.lib
libboost_filesystem-vc110-s-1_51.lib
libboost_filesystem-vc110-sgd-1_51.lib
libboost_system-vc110-mt-s-1_51.lib
libboost_system-vc110-mt-sgd-1_51.lib
libboost_system-vc110-s-1_51.lib
libboost_system-vc110-sgd-1_51.lib


然后我创建了一个新的 Win32 DLL 项目并添加了正确的 include/lib 目录。
但是当我尝试编译时,我得到以下输出:

LNK1104: cannot open file 'libboost_filesystem-vc110-mt-gd-1_51.lib

但是这个文件不存在,因为(据我所知)它不是库的静态版本...
这很奇怪,因为我从未要求在我的项目中的任何地方链接 DLL 版本!

那么为什么我的项目提示一个我从未要求使用的库?
boost header 会自动尝试链接到它们相应的库吗?
我应该设置某种预处理器标志来告诉 boost 我想使用 boost::filesystem 的静态单线程版本吗? ?

更新:

我在评论中得知 boost 确实实际上尝试通过 <boost/config/auto_link.hpp> 为支持它的编译器自动链接。 ...
在多次尝试正确配置这些 header 以使用静态后,/MDd (调试)&/MD (release) 版本的 boost,我仍然收到这个错误。

因此,如果有人能告诉我如何正确配置禁用此功能,我会将其标记为答案。
另外,我假设 libboost_filesystem-vc110-s-1_51.lib 是否正确?和 libboost_filesystem-vc110-sgd-1_51.lib是正确的 /MD/MDd库?

谢谢!

最佳答案

要禁用自动链接,您需要定义 BOOST_ALL_NO_LIB。来自 boost 文档:

// BOOST_ALL_NO_LIB: Tells the config system not to automatically select 
// which libraries to link against.  
// Normally if a compiler supports #pragma lib, then the correct library 
// build variant will be automatically selected and linked against, 
// simply by the act of including one of that library's headers.  
// This macro turns that feature off.

但不要那样做 - 它不会解决您的问题。自动链接通常是正确的,即您要么链接到错误的库,要么错误地配置了预处理器宏。如果链接器想要链接到共享库,我猜你定义了 BOOST_ALL_DYN_LINKBOOST_FILE_SYSTEM_DYN_LINKBOOST_SYSTEM_DYN_LINK。删除它,它应该可以正常链接。

关于c++ - 使用静态版本的 boost::filesystem 时出现有趣的 LNK1104 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16380652/

相关文章:

c++ - 在运行时从 xml 文件构建对象并一次性初始化?

opencv - 在openCV中逐像素复制

visual-studio - 如何在 Visual C++ 2017 中链接静态库?

使用 Boost 的 C++ 阻塞队列

c++ - 使用 Boost 的 Visual Studio C++/OpenCL 项目未在 Linux 上使用 g++ 进行编译

c++ - 使用 boost 库构建 socket.io C++

c++ - bool 和 C/C++ 互操作性

用于 win32 或 C++ 的 C#

c++ - C++中 vector 数组的最佳库

c++ - 优化 C++ 中的模块化算术计算