boost LNK2005 重定义错误

开发的库使用了boost库,然后出现如下错误:

libboost_system-vc141-mt-gd-x64-1_68.lib(error_code.obj) : error LNK2005: "class boost::system::error_category const & __cdecl boost::system::detail::generic_category_ncx(void)" (?generic_category_ncx@detail@system@boost@@YAAEBVerror_category@23@XZ) 已经在 boost_system-vc141-mt-gd-x64-1_68.lib(boost_system-vc141-mt-gd-x64-1_68.dll) 中定义
libboost_filesystem-vc141-mt-gd-x64-1_68.lib(path_traits.obj) : error LNK2005: "void __cdecl boost::filesystem::path_traits::convert(char const *,char const *,class std::basic_string<wchar_t,struct std::char_traits<wchar_t>,class std::allocator<wchar_t> > &,class std::codecvt<wchar_t,char,struct _Mbstatet> const &)" (?convert@path_traits@filesystem@boost@@YAXPEBD0AEAV?$basic_string@_WU?$char_traits@_W@std@@V?$allocator@_W@2@@std@@AEBV?$codecvt@_WDU_Mbstatet@@@5@@Z) 已经在 boost_filesystem-vc141-mt-gd-x64-1_68.lib(boost_filesystem-vc141-mt-gd-x64-1_68.dll) 中定义
libboost_filesystem-vc141-mt-gd-x64-1_68.lib(operations.obj) : error LNK2005: "class boost::filesystem::file_status __cdecl boost::filesystem::detail::status(class boost::filesystem::path const &,class boost::system::error_code *)" (?status@detail@filesystem@boost@@YA?AVfile_status@23@AEBVpath@23@PEAVerror_code@system@3@@Z) 已经在 boost_filesystem-vc141-mt-gd-x64-1_68.lib(boost_filesystem-vc141-mt-gd-x64-1_68.dll) 中定义
libboost_filesystem-vc141-mt-gd-x64-1_68.lib(operations.obj) : error LNK2005: "bool __cdecl boost::filesystem::detail::create_directories(class boost::filesystem::path const &,class boost::system::error_code *)" (?create_directories@detail@filesystem@boost@@YA_NAEBVpath@23@PEAVerror_code@system@3@@Z) 已经在 boost_filesystem-vc141-mt-gd-x64-1_68.lib(boost_filesystem-vc141-mt-gd-x64-1_68.dll) 中定义
libboost_filesystem-vc141-mt-gd-x64-1_68.lib(path.obj) : error LNK2005: "public: static class std::codecvt<wchar_t,char,struct _Mbstatet> const & __cdecl boost::filesystem::path::codecvt(void)" (?codecvt@path@filesystem@boost@@SAAEBV?$codecvt@_WDU_Mbstatet@@@std@@XZ) 已经在 boost_filesystem-vc141-mt-gd-x64-1_68.lib(boost_filesystem-vc141-mt-gd-x64-1_68.dll) 中定义
libboost_thread-vc141-mt-gd-x64-1_68.lib(thread.obj) : error LNK2005: "void __cdecl boost::detail::set_tss_data(void const *,class boost::shared_ptr<struct boost::detail::tss_cleanup_function>,void *,bool)" (?set_tss_data@detail@boost@@YAXPEBXV?$shared_ptr@Utss_cleanup_function@detail@boost@@@2@PEAX_N@Z) 已经在 boost_thread-vc141-mt-gd-x64-1_68.lib(boost_thread-vc141-mt-gd-x64-1_68.dll) 中定义
libboost_thread-vc141-mt-gd-x64-1_68.lib(thread.obj) : error LNK2005: "void * __cdecl boost::detail::get_tss_data(void const *)" (?get_tss_data@detail@boost@@YAPEAXPEBX@Z) 已经在 boost_thread-vc141-mt-gd-x64-1_68.lib(boost_thread-vc141-mt-gd-x64-1_68.dll) 中定义

明显错误不在我的代码是,出错的全部都是模板类,换句话说:

  • 自己的代码里,这个模板类实例化一次,
  • 在已经编译好的boost库(如libboost_filesystem-vc141-mt-gd-x64-1_68.lib)已经实例化一次了

一个模板类实例化两次,所以错了

上网google了好久,找到一个提示,可能是混了static/dynamic,但我编译时全是dynamic
然后查看FindBoost.cmake,发现其中有Boost::dynamic_linking变量,而这个变量只是添加一个宏定义BOOST_ALL_DYN_LINK

抱着试试的态度,在cmake脚本中添加了

add_definitions(-DBOOST_ALL_DYN_LINK)

编译成功,不提示LNK2005

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值