Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LNK2019 when including asio headers, solution generated with cmake

I am trying to port a big project from gcc (Linux) to msvc (windows), using cmake and boost libraries.

The project compile and runs fine for gcc but on msvc it returns the following error:

Dyna.obj : error LNK2019: unresolved external symbol "void __cdecl boost::throw_exception(class std::exception const &)" (?throw_exception@boost@@YAXABVexception@std@@@Z) referenced in function "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)

I tried running a simple project using boost asio and it worked, which teorethically excludes boost build problems.

The CMakeLists.txt is as follows: (separated the parts of interest)

    .
    .
    .
IF(WIN32)
          # Flags para garantir a compilação em windows
          SET(CMAKE_CXX_COMPILER icpl)
          SET(TPN_WIN32 "/D WIN32")
          SET(TPN_WIN32_LIB ws2_32.lib odbc32.lib odbccp32.lib)
          SET(CMAKE_EXE_LINKER_FLAGS /NODEFAULTLIB:LIBC;LIBCMT)

    ENDIF(WIN32)

    # Comando para se livrar de warning sobre o caminho da library pthread
    IF(COMMAND cmake_policy)
      cmake_policy(SET CMP0003 NEW)
    ENDIF(COMMAND cmake_policy)

    # Configuracao do TPN REALTIME
    # === inicio ===
    IF (REALTIME_YES)
      MESSAGE ("[TPN] REALTIME ENABLED")
      set(Boost_ADDITIONAL_VERSIONS "1.45.0")
      set(Boost_USE_MULTITHREAD ON)
      set(Boost_USE_STATIC_LIBS ON)
      FIND_PACKAGE( Boost "1.45.0" COMPONENTS system filesystem serialization program_options regex thread date_time REQUIRED)
      FIND_PACKAGE( Threads REQUIRED )

      set(HYDRO_CXX_FLAGS "-DREALTIME -DJOYSTICK")
      set(HYDRO_CXX_LFLAGS ${Boost_LIBRARIES})

      INCLUDE_DIRECTORIES(hydro)


       INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
        ENDIF (REALTIME_YES)
        # ===  final ===
        .
        .
        .

    TARGET_LINK_LIBRARIES(Dyna
      tpn
      preadyn
      ${WHERE_PREA3D}
      ${WHERE_WAMIT_IO}
      ${WHERE_WAMIT_CONVERTER}
      ${WHERE_TECLINE}
      ${HYDRO_CXX_LFLAGS}
      ${TPN_WIN32_LIB}
    )

Thanks in advance

like image 581
Ishida Avatar asked Jul 26 '11 15:07

Ishida


3 Answers

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.

like image 86
Metariku Avatar answered Nov 04 '22 12:11

Metariku


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
like image 6
fabrica Avatar answered Nov 04 '22 12:11

fabrica


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)
like image 3
Sergei Krivonos Avatar answered Nov 04 '22 10:11

Sergei Krivonos