gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:52:05 25 4
gpt4 key购买 nike

我有一个项目,它由一个主项目、3 个使用 boost 的项目和几个链接到静态库的其他项目组成。这3个项目使用boost。但是我无法构建我的主要项目。我有很多 error LNK2005: .... already defined in ...错误。据我了解,boost 会自动将库链接到这两个项目。我尝试使用 BOOST_ALL_NO_LIB,但没有用。添加此宏后,我遇到了 Unresolved 符号错误,但重新定义错误仍然存​​在。我该如何解决这个问题?

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_LIBBOOST_ASIO_NO_LIB 类的宏。结果与使用 BOOST_ALL_NO_LIB

相同

UPD3:我可以在我的主项目中设置链接器选项 "Force File Output""Multiply Defined Symbols Only/FORCE:MULTIPLE" 并且项目编译成功,但是在执行过程中它崩溃了,所以我不能使用这个选项。

最佳答案

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

通过这样做,您为这些对象/函数创建了多个定义,因此 LNK2005错误。关于 this post 中的一些内容.您需要按照手册中有关这种情况的说明进行操作,并正确构建。来自 the docs :

Optional separate compilation

By default, Boost.Asio is a header-only library. However, some developers may prefer to build Boost.Asio using separately compiled source code. To do this, add #include <boost/asio/impl/src.hpp> to one (and only one) source file in a program, then build the program with BOOST_ASIO_SEPARATE_COMPILATION defined in the project/compiler settings. Alternatively, BOOST_ASIO_DYN_LINK may be defined to build a separately-compiled Boost.Asio as part of a shared library.

强调我的。确保定义 BOOST_ASIO_SEPARATE_COMPILATION是包含 boost::asio 的所有相关项目。关于/FORCE:MULTIPLE选项,假装这个标志不存在。正如你所看到的,这是邪恶的。它在垃圾中抛出错误,告诉您出现了严重错误。

关于c++ - 链接多个使用 boost 的项目时出现 LNK2005 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33624584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com