LNK2019 包含 asio 标头时,使用 cmake 生成的解决方案

发布于 2024-11-26 13:38:07 字数 2013 浏览 1 评论 0原文

我正在尝试使用 cmake 和 boost 库将一个大项目从 gcc (Linux) 移植到 msvc (windows)。

该项目对于 gcc 编译并运行良好,但在 msvc 上它返回以下错误:

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)

我尝试使用 boost asio 运行一个简单的项目,它工作了,理论上排除了 boost 构建问题。

CMakeLists.txt如下:(分隔感兴趣的部分)

    .
    .
    .
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}
    )

提前致谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凹づ凸ル 2024-12-03 13:38:07

尝试将标志“/EHsc”添加到 cmake 中的 TPN_WIN32 变量中。
看来 MSVC 没有抛出异常,您需要在 vcproj 中启用它。

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.

时光暖心i 2024-12-03 13:38:07

就我而言,/EHsc 标志不起作用。事实证明,定义了 BOOST_NO_EXCEPTIONS,因此编译器正在搜索“用户定义”(如 boost/throw_exception.hpp 中)函数。

因此,一个快速解决方法是编写您最喜欢的 boost:: throw_exception() 函数:

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

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
情深缘浅 2024-12-03 13:38:07

看起来,为了链接兼容,二进制文件必须具有相同的结构异常处理启用选项。 MSVC 标准库的实现使用结构化异常处理选项。看起来这就是 boost::system 也使用它的原因。您可能会看到相应的警告,告诉您添加结构异常处理。

IF(MSVC)
    ADD_DEFINITIONS("/EHsc")
ENDIF(MSVC)

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)
滴情不沾 2024-12-03 13:38:07

在 Windows 上运行时,您需要(默认情况下)链接到 boost.systemboost.regex

正如它所说的 此处

注意对于 MSVC 或 Borland C++,您可能需要添加
-DBOOST_DATE_TIME_NO_LIB 和 -DBOOST_REGEX_NO_LIB 到您的项目
设置禁用 Boost.Date_Time 和 Boost.Regex 自动链接
分别是库。或者,您可以选择构建这些
库并链接到它们。

如果您不想链接到其他 boost 库,那么您可以使用 此处 的相同(非 boost)asio 库

就您的 CMakeLists.txt 文件而言,您需要一行

target_link_libraries (your_application ${Boost_LIBRARIES})

来实际链接库。

编辑:另外,看看 如何链接boost.system 使用 cmake,您可能必须专门指定各个 boost 库,而不是 ${Boost_LIBRARIES}

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}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文