LNK2019 when including asio headers, solution generated with cmake

痞子三分冷 提交于 2019-12-03 03:01:20

Try to add the flag "/EHsc" into your TPN_WIN32 variable in cmake. It seems that MSVC is not throwing exceptions and you need to enable it in your vcproj.

fabrica

In my case, the /EHsc flag did not work. Turned out that BOOST_NO_EXCEPTIONS was defined so the compiler was searching for the "user defined" (as in boost/throw_exception.hpp) function.

Therefore, a quick fix is to write your favorite boost::throw_exception() function:

namespace boost
{
#ifdef BOOST_NO_EXCEPTIONS
void throw_exception( std::exception const & e ){
    throw 11; // or whatever
};
#endif
}// namespace boost
Tom

When running on windows you need (by default) to link to boost.system and boost.regex

As it says here:

Note With MSVC or Borland C++ you may want to add -DBOOST_DATE_TIME_NO_LIB and -DBOOST_REGEX_NO_LIB to your project settings to disable autolinking of the Boost.Date_Time and Boost.Regex libraries respectively. Alternatively, you may choose to build these libraries and link to them.

If you don't want to link to other boost libraries then you can use the identical (non-boost) asio library from here.

In terms of your CMakeLists.txt file, you want a line such as

target_link_libraries (your_application ${Boost_LIBRARIES})

to actually link the library.

EDIT: also, have a look at How to link against boost.system with cmake, it could be that you have to specify the individual boost libraries specifically rather than ${Boost_LIBRARIES}

Looks like, to be linking compatible, binary must have same structure exception handling enablement option. MSVC standard library implementation use structured exception handling option on. Looks like this is why boost::system also uses this on. You might see corresponding warnings telling you to add structure exception handling.

IF(MSVC)
    ADD_DEFINITIONS("/EHsc")
ENDIF(MSVC)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!