c++ - 动态链接 boost 库和自动链接不能正常工作?

标签 c++ boost linker-errors boost-thread dynamic-linking

我使用以下命令来构建 boost-1.53.0Win7 ProVS 2013 Pro (VC12.0, 18.00.21005.1) :

b2 stage toolset=msvc link=shared runtime-link=shared threading=multi --without-graph --without-graph_parallel --without-mpi --without-wave --without-python

在此之前,我按照这个补丁修补了我的源代码:#8750 for_vs2013.patch .

最后我得到了$BOOST_ROOT/stage/lib下的文件像这样:

boost_atomic-vc120-mt-1_53.dll
boost_atomic-vc120-mt-1_53.lib
boost_atomic-vc120-mt-gd-1_53.dll
boost_atomic-vc120-mt-gd-1_53.lib
...
boost_thread-vc120-mt-1_53.dll
boost_thread-vc120-mt-1_53.lib
boost_thread-vc120-mt-gd-1_53.dll
boost_thread-vc120-mt-gd-1_53.lib
...
// only theses four files have "lib" prefix
libboost_exception-vc120-mt-1_53.lib
libboost_exception-vc120-mt-gd-1_53.lib
libboost_test_exec_monitor-vc120-mt-1_53.lib
libboost_test_exec_monitor-vc120-mt-gd-1_53.lib

我构建失败 boost::signals图书馆。不知道是不是跟这个issue有关,所以在此说明一下。

我写了一个很简单的demo来学习boost::thread根据刚才的教程。但是编译器给我一个链接错误:LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc120-mt-gd-1_53.lib' .我四处搜索,我有一个问题。我确定我已经设置了 Project Properties > C/C++ > Code Generation > Runtime Library/MDd .但是为什么编译器仍然尝试链接静态库 libboost_thread-vc120-mt-gd-1_53.lib什么时候重建项目?我还尝试使用我找到的一些相关解决方案,例如使用 BOOST_ALL_DYN_LINKBOOST_DYN_LINK预编译指令。但它不起作用。我在 auto-link.hpp 中找到了这样一个片段:

//
// select linkage opt:
//
#if (defined(_DLL) || defined(_RTLDLL)) && defined(BOOST_DYN_LINK)
#  define BOOST_LIB_PREFIX
#elif defined(BOOST_DYN_LINK)
#  error "Mixing a dll boost library with a static runtime is a really bad idea..."
#else
#  define BOOST_LIB_PREFIX "lib"
#endif

它应该可以工作,但我得不到我想要的结果:(

顺便说一句,我应该用 link=static 重建 boost 库吗?选项?

演示代码

// c++ std libraries
#include <iostream>
// c++ boost libraries
#include <boost/thread.hpp>
#include <boost/date_time.hpp>

// #define BOOST_ALL_DYN_LINK
// #define BOOST_DYN_LINK

void WorkerFunc()
{
    boost::posix_time::seconds worktime(3);
    std::cout << "Worker: running..." << std::endl;

    // Pretend to do sth. useful...
    boost::this_thread::sleep(worktime);

    std::cout << "Worker: finished." << std::endl;
}


int main()
{
    std::cout << "main: startup." << std::endl;
    // Create a worker thread.
    boost::thread worker_thread(WorkerFunc);

    std::cout << "main: waiting for thread..." << std::endl;
    worker_thread.join();

    std::cout << "main: done." << std::endl;

    return 0;
}

最佳答案

从 visual studio 内部:转到您的项目并右键单击它以获取上下文菜单。转到属性。在属性对话框中转到:

配置属性->C/C++->预处理器->预处理器定义

尝试将 BOOST_ALL_DYN_LINK(或 BOOST_THREAD_USE_DLL)放入预处理器定义列表中。

然后重建您的演示。

关于c++ - 动态链接 boost 库和自动链接不能正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23455670/

相关文章:

c++ - 如何在 C++ 中声明 wchar_t[] 的 vector ?

c++ - 为 Visual Studio 2017 构建 boost 1.64

c++ - 使用 boost::iterator_range;我的数据在哪里超出范围?

C++ MySQL 链接器错误

c++ - C2059 : syntax e r r o r : ';'

c++ - 在 Android 上使用 Boost.Log 进行 logcat

c++ - 在 Linux 中链接时发生库冲突,但在 OSX 中则不会

c++ - 什么是 undefined reference /未解析的外部符号错误以及如何修复它?

nvcc 不支持 C++11 函数 iota()?

c++ - boost::mpl::string 大小错误消息