链接多个使用boost的项目时出现LNK2005错误

时间:2015-11-10 07:06:51

标签: c++ boost visual-studio-2013 linker boost-asio

我有一个项目,包括一个主项目,3个使用boost的项目和很少的其他链接到静态库的项目。这3个项目使用boost。但我不能建立我的主要项目。我有很多error LNK2005: .... already defined in ... 错误。据我所知,boost自动将库链接到这两个项目。我尝试使用 BOOST_ALL_NO_LIB ,但它没有用。添加此宏后,我得到了未解决的符号错误,但重新定义错误仍然存​​在。我该如何解决这个问题?

UPD1:大约有400多个错误。以下是第一个:

1>Project_with_boost_1.lib(VS_Project_with_boost_1.obj) : error LNK2005: "unsigned long __cdecl boost::asio::detail::win_tss_ptr_create(void)" (?win_tss_ptr_create@detail@asio@boost@@YAKXZ) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(VS_Project_with_boost_1.obj) : error LNK2005: "void __cdecl boost::asio::detail::do_throw_error(class boost::system::error_code const &,char const *)" (?do_throw_error@detail@asio@boost@@YAXABVerror_code@system@3@PBD@Z) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(VS_Project_with_boost_1.obj) : error LNK2005: "class boost::system::error_category const & __cdecl boost::asio::error::get_misc_category(void)" (?get_misc_category@error@asio@boost@@YAABVerror_category@system@3@XZ) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(VS_Project_with_boost_1.obj) : error LNK2005: "protected: static void __cdecl boost::asio::detail::winsock_init_base::startup(struct boost::asio::detail::winsock_init_base::data &,unsigned char,unsigned char)" (?startup@winsock_init_base@detail@asio@boost@@KAXAAUdata@1234@EE@Z) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(VS_Project_with_boost_1.obj) : error LNK2005: "protected: static void __cdecl boost::asio::detail::winsock_init_base::cleanup(struct boost::asio::detail::winsock_init_base::data &)" (?cleanup@winsock_init_base@detail@asio@boost@@KAXAAUdata@1234@@Z) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(VS_Project_with_boost_1.obj) : error LNK2005: "protected: static void __cdecl boost::asio::detail::winsock_init_base::throw_on_error(struct boost::asio::detail::winsock_init_base::data &)" (?throw_on_error@winsock_init_base@detail@asio@boost@@KAXAAUdata@1234@@Z) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(VS_Project_with_boost_1.obj) : error LNK2005: "public: void __thiscall boost::asio::detail::win_iocp_io_service::post_deferred_completions(class boost::asio::detail::op_queue<class boost::asio::detail::win_iocp_operation> &)" (?post_deferred_completions@win_iocp_io_service@detail@asio@boost@@QAEXAAV?$op_queue@Vwin_iocp_operation@detail@asio@boost@@@234@@Z) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(VS_Project_with_boost_1.obj) : error LNK2005: "struct boost::asio::detail::signal_state * __cdecl boost::asio::detail::get_signal_state(void)" (?get_signal_state@detail@asio@boost@@YAPAUsignal_state@123@XZ) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(VS_Project_with_boost_1.obj) : error LNK2005: _boost_asio_signal_handler already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(VS_Project_with_boost_1.obj) : error LNK2005: "public: static void __cdecl boost::asio::detail::signal_set_service::deliver_signal(int)" (?deliver_signal@signal_set_service@detail@asio@boost@@SAXH@Z) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(Some_file.obj) : error LNK2005: "unsigned long __cdecl boost::asio::detail::win_tss_ptr_create(void)" (?win_tss_ptr_create@detail@asio@boost@@YAKXZ) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(Some_file.obj) : error LNK2005: "void __cdecl boost::asio::detail::do_throw_error(class boost::system::error_code const &,char const *)" (?do_throw_error@detail@asio@boost@@YAXABVerror_code@system@3@PBD@Z) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(Some_file.obj) : error LNK2005: "class boost::system::error_category const & __cdecl boost::asio::error::get_misc_category(void)" (?get_misc_category@error@asio@boost@@YAABVerror_category@system@3@XZ) already defined in Project_with_boost_2.lib(asio.obj)
1>Project_with_boost_1.lib(Some_file.obj) : error LNK2005: "protected: static void __cdecl boost::asio::detail::winsock_init_base::startup(struct boost::asio::detail::winsock_init_base::data &,unsigned char,unsigned char)" (?startup@winsock_init_base@detail@asio@boost@@KAXAAUdata@1234@EE@Z) already defined in Project_with_boost_2.lib(asio.obj)

UPD2:我还尝试使用宏 BOOST_SYSTEM_NO_LIB BOOST_ASIO_NO_LIB 。结果与使用 BOOST_ALL_NO_LIB

相同

UPD3:我可以在我的主项目中设置链接器选项&#34;强制文件输出&#34; &#34;仅限定义符号/ FORCE:MULTIPLE&#34; 和项目编译成功,但在执行过程中崩溃,所以我无法使用此选项。

1 个答案:

答案 0 :(得分:4)

您的问题与自动链接无关。问题在于,您已将boost :: asio(一个仅具有某种全局状态的标头库)导入两个单独的二进制文件并将它们链接在一起(可能是一个或多个dll和一个exe)。

通过执行此操作,您将创建这些对象/函数的多个定义,从而导致LNK2005错误。在this post中的一点点。您需要按照手册中有关此情况的说明进行操作,并正确构建。来自the docs

  

可选的单独编译

     

默认情况下,Boost.Asio是一个仅限标头的库。但是,有些   开发人员可能更喜欢使用单独编译来构建Boost.Asio   源代码。为此,请将#include <boost/asio/impl/src.hpp> 添加到一个   (并且只有一个)程序中的源文件,然后用它构建程序   项目/编译器中定义的BOOST_ASIO_SEPARATE_COMPILATION   设置。或者,可以定义BOOST_ASIO_DYN_LINK来构建a   单独编译的Boost.Asio作为共享库的一部分。

强调我的。确保定义BOOST_ASIO_SEPARATE_COMPILATION是包含boost :: asio的所有关联项目。关于/FORCE:MULTIPLE选项,假装此标志不存在。你可以看到,这是邪恶的。它在垃圾中抛出错误,告诉你某些事情是严重错误的。