5

I have a project that consists from one main project, 3 projects that use boost and few other projects that links to static libraries. These 3 projects use boost. But I can't build my main project. I've got a lot of error LNK2005: .... already defined in ... errors. As I understood, boost automaticaly links libraries to both of this projects. I tried to use BOOST_ALL_NO_LIB, but it didn't work. After adding this macro I've got unresolved symbols errors, but the redefinition errors still here. How can I solve this problem?

UPD1: there about 400+ errors. Here are first of them:

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: I also tried to use macros kind of BOOST_SYSTEM_NO_LIB, BOOST_ASIO_NO_LIB. Result is the same as by using BOOST_ALL_NO_LIB

UPD3: I can set linker option in my main project "Force File Output" to "Multiply Defined Symbols Only /FORCE:MULTIPLE" and project compiles successfully, but during execution it crashes, so I can't use this option.

user2123079
  • 656
  • 8
  • 29
  • Are you using boost libs as dlls and using `BOOST_ALL_DYN_LINK`? You need to post some code because this could be lots of things. It might have absolutely nothing to do with boost, in fact. In fact it could be nearly anything. Definitely need at the very least the complete compiler and linker phase console output, not just `error LNK2005` - this tells us absolutely nothing. –  Nov 10 '15 at 07:16
  • By using **BOOST_ALL_DYN_LINK** I've got same errors. – user2123079 Nov 10 '15 at 07:29
  • 1
    You've got to post more about your errors. My bet is that the error codes give away exactly what the real issue is. They're designed to do that. :) –  Nov 10 '15 at 07:35
  • Yes, I thought this was the problem. You're including asio inside of dll's aren't you? –  Nov 10 '15 at 07:51

1 Answers1

4

Your issues have nothing to do with auto linking. The issue is the fact that you've imported boost::asio, a header only library that has some global state, into two separate binaries and linked them together (probably one or more dlls and an exe).

By doing this, you're creating more than one definition of these objects/functions, hence the LNK2005 errors. A bit on that in this post. You need to follow what the manual says about this situation, and build properly. From 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.

Emphasis mine. Make sure you define BOOST_ASIO_SEPARATE_COMPILATION is all associated projects that include boost::asio. About the /FORCE:MULTIPLE option, pretend this flag doesn't exist. It's evil, as you can see. It's throwing errors in the garbage that were there to tell you that something is seriously wrong.

Community
  • 1
  • 1
  • 1
    Thank you good man! God bless you!!! I added to depend-projects macro **BOOST_ASIO_SEPARATE_COMPILATION** with **BOOST_ALL_NO_LIB** and in main project added to linker input necessary libraries and it compiled! Thank you once again!! – user2123079 Nov 10 '15 at 08:08
  • No problem, same to you. However, you shouldn't need `BOOST_ALL_NO_LIB` anymore. Just use `BOOST_ASIO_SEPARATE_COMPILATION`. –  Nov 10 '15 at 08:13