【问题标题】:Dynamically Linking boost with Cmake使用 Cmake 动态链接 boost
【发布时间】:2016-07-19 18:48:47
【问题描述】:

我检查了几个问题,但没有一个答案对我的情况有所帮助。这是我检查的那些

How to link C++ program with Boost using CMake

Whats the proper way to link Boost with CMake and Visual Studio in Windows?

Error linking Boost with CMake

我已经用 Boost 1.61 构建了

b2 variant=debug,release link=static,shared threading=single,multi

在 stage/lib 下,我有所有的 dll 和 lib。以下 Boost::System 库位于 stage/lib 下

boost_system-vc140-mt-1_61.dll
boost_system-vc140-mt-1_61.lib
boost_system-vc140-mt-gd-1_61.dll
boost_system-vc140-mt-gd-1_61.lib
libboost_system-vc140-mt-1_61.lib
libboost_system-vc140-mt-gd-1_61.lib

我想编译一个基本的 boost asio 示例(需要来自 boost 的系统库)。我在 windows 中使用 cmake-gui 制作我的 cmake 文件,然后生成 VS2015 项目文件。尝试构建项目后,出现以下错误。

1>------ Build started: Project: ZERO_CHECK, Configuration: Debug Win32 ------
2>------ Build started: Project: boost-asio, Configuration: Debug Win32 ------
2>  main.cpp
2>  Please define _WIN32_WINNT or _WIN32_WINDOWS appropriately. For example:
2>  - add -D_WIN32_WINNT=0x0501 to the compiler command line; or
2>  - add _WIN32_WINNT=0x0501 to your project's Preprocessor Definitions.
2>  Assuming _WIN32_WINNT=0x0501 (i.e. Windows XP target).
2>LINK : fatal error LNK1104: cannot open file 'libboost_system-vc140-mt-gd-1_61.lib'
========== Build: 1 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我的 CMakeLists.txt 的内容

cmake_minimum_required(VERSION 3.6)

project(boost-asio)

find_package(Boost 1.61.0 COMPONENTS system REQUIRED)

# set cmake variables
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}/bin)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)

# definitions
add_definitions(-DBOOST_LOG_DYN_LINK=1)

# set sources
set(SOURCES src/main.cpp)


# manage compilation and linkage
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    add_executable(boost-asio ${SOURCES})
    target_link_libraries(boost-asio ${Boost_LIBRARIES})

    message(${Boost_INCLUDE_DIRS})
    message(${Boost_LIBRARIES})
endif()

请注意,cmake 源代码中的定义是根据我检查的某个问题的答案添加的。 FindBoost 找到 boost 库,这里是 cmake-gui 的输出

Boost version: 1.61.0
Found the following Boost libraries:
  system
C:/Boost/boost_1_61_0
optimizedC:/Boost/boost_1_61_0/stage/lib/boost_system-vc140-mt-1_61.libdebugC:/Boost/boost_1_61_0/stage/lib/boost_system-vc140-mt-gd-1_61.lib

现在这里有两个问题。首先,它找不到库,其次,虽然它明确指示链接器动态链接,但它仍然寻找静态库。我不知道这些问题是否相关。如何解决这个问题呢?如何使用 Cmake 动态链接 Boost?

EDIT1:这是我正在使用的代码,如果你想测试一下。

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

int main() {

    boost::asio::io_service ioservice;

    boost::asio::deadline_timer t(ioservice, boost::posix_time::seconds(5));

    t.wait();

    std::cout << "Hello, world!" << std::endl;

    return 0;
}

【问题讨论】:

  • Boost_USE_* 变量应该设置之前 find_package(Boost)调用有效果。
  • 尝试在if(Boost_FOUND)前添加find_package(Boost)后构建,似乎没有效果
  • 您需要清除 CMake 缓存以重新生成 find_package() 的结果。
  • 我删除了缓存并重新配置并重新生成了项目文件。还是一样。
  • 您不应该将asio 添加为find_package() 中的一个组件,因为您想要的不仅仅是system??

标签: boost linker cmake dynamic-linking


【解决方案1】:

在 CMakeLists.txt 中添加以下标志可以解决问题:

add_definitions( -DBOOST_ALL_NO_LIB )
add_definitions( -DBOOST_ALL_DYN_LINK )

【讨论】:

  • 最佳方式:使用 add target_compile_definitions(&lt;your_target&gt; PRIVATE BOOST_&lt;component&gt;_DYN_LINK)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-11
  • 1970-01-01
  • 1970-01-01
  • 2018-09-25
  • 1970-01-01
  • 2012-07-25
  • 1970-01-01
相关资源
最近更新 更多