实现cmake编译Aiso
Asio库是boost下的头文件实现异步输入/输出(Asynchronous input/output)的功能库。在很多项目中需要Asio库的支持。Asio库源代码采用configure、make、make install配置生成,未提供CMakelists文件。
目的:为了方便用cmake生成asio,并提供一个使用asio库的简单程序CMakelists配置。因此分享编写了CMakelists文件的方法。
1、准备
当然你需要下载asio库。git:
2、编写独立生成库CMakelists文件
CMakelists文件中主要配置几个内容:
1)包含文件路径
2)加入源文件
3)连接为静态库
4)设置安装
5)※注意,需要声明ASIO_SEPARATE_COMPILATION,表示单独生成libasio库。
cmake_minimum_required(VERSION 3.5)
project(asio LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions(-DASIO_SEPARATE_COMPILATION) #声明独立生成libasio
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
option(HAVE_SSL "Disables OpenSSL Support" OFF)
# ------------------------------------------------------------------------
# 我自定义了install时候的路径,也可以定义为自己的install路径
#!SET(CMAKE_INSTALL_PREFIX ${PROJECT_SOURCE_DIR}/install)
if(NOT HAVE_SSL)
add_library (asio STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/asio.cpp)
else()
add_library (asio STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/asio_ssl.cpp)
endif()
target_include_directories(asio PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)
set_target_properties(asio PROPERTIES PUBLIC_HEADER "include/asio.hpp")
#==================生成目标文件的xxxTarget.cmake======================
# 会将生成的库libasio.a安装到${CMAKE_INSTALL_PREFIX}/lib下
message(STATUS "install path = ${CMAKE_INSTALL_PREFIX}")
install(
TARGETS asio
EXPORT ${PROJECT_NAME}Targets
PUBLIC_HEADER DESTINATION include
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
# 生成 xxxTargets.cmake文件 , 安装到 ${CMAKE_INSTALL_PREFIX}/lib/cmake
install(
EXPORT ${PROJECT_NAME}Targets
FILE ${PROJECT_NAME}Targets.cmake
DESTINATION lib/cmake
)
#======================生成 xxxConfig.cmake===============================
# 该变量会通过xxxConfig.cmake.in用于在生成的xxxConfig.cmake中
set(INCLUDE_DIRS include)
set(LIBRARIES asio) # 库文件名称
set(VAR_INCLUDE_DIRS ${CMAKE_INSTALL_PREFIX}/include)
set(VAR_LIBRARY_DIRS ${CMAKE_INSTALL_PREFIX}/lib)
# 由cmake提供
include(CMakePackageConfigHelpers)
# 生成 xxxConfigVersion.cmake文件
write_basic_package_version_file(
${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
VERSION 1.1.1
COMPATIBILITY SameMajorVersion
)
# 设置替换 Config.cmake @PACKAGE_INCLUDE_DIRS@指定的变量
set(INCLUDE_DIRS ${CMAKE_INSTALL_PREFIX}/include)
set(LIB_DIR ${CMAKE_INSTALL_PREFIX}/lib)
# 用于生成 xxxConfig.cmake文件
configure_package_config_file(
${PROJECT_SOURCE_DIR}/${PROJECT_NAME}Config.cmake.in
${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake
INSTALL_DESTINATION lib/cmake
PATH_VARS VAR_INCLUDE_DIRS VAR_LIBRARY_DIRS
INSTALL_PREFIX ${CMAKE_INSTALL_PREFIX}
)
#安装头文件
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/include DESTINATION ${CMAKE_INSTALL_PREFIX}
FILES_MATCHING PATTERN "*.h"
PATTERN "*.hpp"
PATTERN "*.ipp")
# 将xxxConfig.cmake安装到 ${CMAKE_INSTALL_PREFIX}/lib/cmake
install(
FILES ${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake ${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake
DESTINATION lib/cmake
)
编辑一个config.cmake.in用着生成asioconfig.cmake的模板:
@PACKAGE_INIT@
## set(@PROJECT_NAME@_LIBRARIES plus) # 库文件名称 libxxx
set(@PROJECT_NAME@_LIBRARIES ${PROJECT_NAME}) # 库文件名称 libxxx
#set(@PROJECT_NAME@_INCLUDE_DIRS @PACKAGE_VAR_INCLUDE_DIRS@) # xxxx_INCLUDE_DIRS 路径设置
set(Asio_INCLUDE_DIRS @PACKAGE_VAR_INCLUDE_DIRS@)
set(@PROJECT_NAME@_LIBRARY_DIRS @PACKAGE_VAR_LIBRARY_DIRS@) # xxxx_LIBRARY_DIRS 路径设置
#set(@PROJECT_NAME@_LIBRARIES @PACKAGE_VAR_LIBRARIES@) #依赖库名称
check_required_components(${PROJECT_NAME})
3、编写生成示例的CMakelists文件
cmake_minimum_required(VERSION 3.5)
project(asio LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(ASIO_INCLUDE ${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${ASIO_INCLUDE})
#编译生成 examples/cpp11/echo/blocking_tcp_echo_client.cpp ,生成一个示例程序。
option(INSTALL_TEST "Install test" ON)
if(INSTALL_TEST)
add_executable(blocking_tcp_echo_client ${CMAKE_CURRENT_SOURCE_DIR}/src/examples/cpp11/echo/blocking_tcp_echo_client.cpp)
#采用标准定义宏生成方式
target_compile_options(blocking_tcp_echo_client PUBLIC -DASIO_STANDALONE)
target_include_directories(blocking_tcp_echo_client PUBLIC ${ASIO_INCLUDE})
target_link_libraries(blocking_tcp_echo_client PUBLIC -lwsock32 -lws2_32)
# 安装设置
install(TARGETS blocking_tcp_echo_client
#EXPORT blocking_tcp_echo_clientTargets
#LIBRARY DESTINATION lib # 动态库安装路径
#ARCHIVE DESTINATION lib # 静态库安装路径
RUNTIME DESTINATION bin # 可执行文件安装路径
#PUBLIC_HEADER DESTINATION include # 头文件安装路径
)
endif()
4、cmake生成
1)配置
cmake -S ./.. -B ./ -DCMAKE_INSTALL_PREFIX="/D/yourdir/test" -G "Unix Makefiles"
在visual studio平台生成,需要配置VS环境
"%VS142COMNTOOLS%\Common7\Tools\VsDevCmd.bat"
cmake -S . -B build_vs16 -G "Visual Studio 16 2019"
2)生成
cmake --build . --target install