如何使用CMake自动链接Boost库 [英] How to auto-link boost libraries with CMake

查看:189
本文介绍了如何使用CMake自动链接Boost库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

project(learn)

cmake_minimum_required(VERSION 3.11)

set(CMAKE_CXX_STANDARD 17)

if(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")
    message("Current OS is Linux")
    include_directories("/mnt/e/c++/boost_1_72_0")
    link_directories("/mnt/e/c++/boost_1_72_0/stage/lib")
    link_libraries(pthread boost_thread boost_fiber boost_context)
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    message("Current OS is Windows")
    include_directories("E:/c++/boost_1_72_0")
    link_directories("E:/c++/boost_1_72_0/stage/lib")
endif(${CMAKE_SYSTEM_NAME} STREQUAL "Linux")

add_executable(learn_asio learn_asio.cpp)

learn_asio.cpp:

learn_asio.cpp:

#include <boost/asio.hpp>
#include <boost/fiber/all.hpp>
#include <boost/thread.hpp>
#include <iostream>

using boost::asio::async_write;
using boost::asio::buffer;
using boost::asio::io_context;
using boost::asio::use_future;
using boost::asio::ip::make_address;
using boost::asio::ip::tcp;
using boost::fibers::async;
using boost::fibers::fiber;
using boost::system::error_code;

int main(){
  io_context ioc;
  tcp::socket socket(ioc);

  tcp::endpoint ep(make_address("192.168.1.20"), 80);

  auto ret_f = socket.async_connect(ep, boost::asio::use_future);

  boost::thread_group t;
  t.create_thread([&ioc]() {
    ioc.run();
    std::cout << "jfiejf" << std::endl;
  });

  ret_f.wait_for(std::chrono::seconds(3));

  t.join_all();

  return 0;
}


我的图书馆文件夹:
通过上面的代码,我可以成功地构建代码.但是我讨厌代码:


My library folder:
Accroding to the code above I can successfully build my code. But I hate the code:

link_libraries(pthread boost_thread boost_fiber boost_context)

在Linux平台上的

.为什么在Windows平台上不需要它?
我记得Linux也可以自动链接该库.
我该如何实现?

on the linux platform. Why do I not need it on windows platforms?
As I remember Linux can auto link the library too.
How can I achieve it?

推荐答案

提升文档:

自动链接

大多数Windows编译器和链接器都具有所谓的自动链接支持",从而消除了第二个挑战.Boost头文件中的特殊代码将检测您的编译器选项,并使用该信息将正确库的名称编码到目标文件中.链接器从您告诉它进行搜索的目录中选择具有该名称的库.

Most Windows compilers and linkers have so-called "auto-linking support," which eliminates the second challenge. Special code in Boost header files detects your compiler options and uses that information to encode the name of the correct library into your object files; the linker selects the library with that name from the directories you've told it to search.

GCC工具链(Cygwin和MinGW)是显着的例外.GCC用户应参阅 Unix变体操作系统的链接说明,以使用适当的命令行选项.

The GCC toolchains (Cygwin and MinGW) are notable exceptions; GCC users should refer to the linking instructions for Unix variant OSes for the appropriate command-line options to use.

请注意,已知自动链接功能有时会失败(例如,使用非标准设置安装Boost库时).您可以定义 BOOST_ALL_NO_LIB 禁用 Windows上的功能.

Note that the auto-linking feature has been known to fail on occasion (e.g. when your Boost libraries are installed using a non-standard setting). You can define BOOST_ALL_NO_LIB to disable the feature on Windows.

不过,您不应该将Boost路径硬编码到CMakeLists.txt中.最好使用与平台无关的 find_package :

You shouldn't be hard-coding Boost paths into your CMakeLists.txt, though. Better use the platform-independent find_package:

set( Boost_USE_STATIC_LIBS OFF )
set( Boost_USE_MULTITHREADED ON )
set( Boost_USE_STATIC_RUNTIME OFF )

find_package( Boost 1.72.0 COMPONENTS thread fiber context )

if ( Boost_FOUND )
    include_directories( ${Boost_INCLUDE_DIRS} )
    link_libraries( learn_asio ${Boost_LIBRARIES} )
else()
    message( FATAL_ERROR "Required Boost packages not found. Perhaps add -DBOOST_ROOT?" )
endif()
    

这篇关于如何使用CMake自动链接Boost库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆