2013-03-08 125 views
2

我想静态链接boost.asio到我的小项目没有外部库(只有单个exe/bin文件结果分发)。 Boost.asio需要Boost.system,我开始淹死试图弄清楚如何编译这一切。 如何使用cmake使用Boost.asio?与cmake一起使用Boost.asio?

+0

所有升压组件可以静态链接。你又有什么问题? – 2013-03-08 09:34:54

+0

我已经搜索了很多次,每次我发现一些问题,为什么我不能编译它。所以我提出了问题如何做到这一点。我不能用cmake编译boost的任何部分,也不知道如何用cmake静态地在我的项目中使用它。 – Fedcomp 2013-03-08 12:04:32

+2

查看'FindBoost.cmake'的内容。你可以在CMake安装的'Modules /'目录中找到它。 – arrowd 2013-03-08 17:25:10

回答

7

如果我理解实际的问题,它基本上要求如何静态链接CMake中的第三方库。

在我的环境中,我安装了Boost到/opt/boost

最简单的方法是使用一个CMake的安装提供FindBoost.cmake

set(BOOST_ROOT /opt/boost) 
set(Boost_USE_STATIC_LIBS ON) 
find_package(Boost COMPONENTS system) 

include_directories(${Boost_INCLUDE_DIR}) 
add_executable(example example.cpp) 
target_link_libraries(example ${Boost_LIBRARIES}) 

是找出所有Boost库,并明确针对系统库链接的一个变体:

set(BOOST_ROOT /opt/boost) 
set(Boost_USE_STATIC_LIBS ON) 
find_package(Boost REQUIRED) 

include_directories(${Boost_INCLUDE_DIR}) 
add_executable(example example.cpp) 
target_link_libraries(example ${Boost_SYSTEM_LIBRARY}) 

如果你不这样做有一个适当的Boost安装,那么有两种方法可以静态链接库。第一种方法创建一个进口CMake的目标:

add_library(boost_system STATIC IMPORTED) 
set_property(TARGET boost_system PROPERTY 
    IMPORTED_LOCATION /opt/boost/lib/libboost_system.a 
) 

include_directories(/opt/boost/include) 
add_executable(example example.cpp) 
target_link_libraries(example boost_system) 

而另一种方法是明确列出库中target_link_libraries而不是目标:

include_directories(/opt/boost/include) 
add_executable(example example.cpp) 
target_link_libraries(example /opt/boost/lib/libboost_system.a)