开发的库使用了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