CMake 是一个强大的构建工具,也是C/C++程序员应该必备的技能之一
参考视频:CMake入门教程_哔哩哔哩_bilibili
找了好多教学资料不是基于linux就是跳跃性较大。偶然发现一个不错的好资源就学习了一波。
本文参考上面b站的教学视频,这个教学十分推荐,新手保姆级的,up也分享了自己的笔记和demo
本文主要记录库的生成和链接
前一篇博客:(CMake) 从下载到构建第一个CMake应用_
在src下创建calc文件夹,并编写加法和减法两份文件
加法用户生成静态库
减法用于生成动态库
src下的cmake
# 设置运行时目标文件(exe、dll)的输出位置
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# # 设置存档目标文件(lib、a)的输出位置
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# 添加子目录
add_subdirectory(calc)
# (目标文件) 头文件的搜索目录
# 记得添加源文件的指定路径
target_include_directories(demo PUBLIC
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/calc
)
calc下的cmake
# 库名 静态库 原文件
add_library(addition STATIC addition.cpp)
正常执行命令
cmake -G "MinGW Makefiles" ../src
cmake --build .
查看是否在build中有calc文件夹,其中是否有.a
的静态库
注意,这是未设置静态库路径的情况下set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
使用
# 链接生成的静态库
target_link_libraries(demo PUBLIC addition)
设置静态库的生成路径
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
calc下的cmake
# 库名 动态库 原文件
add_library(subtract SHARED subtract.cpp)
src下的cmake
# 链接生成的动态库
target_link_libraries(demo PUBLIC subtract)
注意:运行程序的时候,要将.dll放在.exe同路径下
首先我们有这些外部项目的库
# (目标文件) 头文件的搜索目录
target_include_directories(demo PUBLIC
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/calc
${PROJECT_SOURCE_DIR}/../other_lib
)
# 外部的静态库
# 使用绝对路径链接
target_link_libraries(demo PUBLIC C:/Users/windows/Desktop/demo/other_lib/libmultiply.a)
# 外部的动态库
target_link_libraries(demo PUBLIC C:/Users/windows/Desktop/demo/other_lib/libdivision.dll)
注意:运行程序的时候,还要将动态库复制一份到.exe的同目录下
main.cpp
#include
#include "addition.h"
#include "subtract.h"
#include "multiply.h"
#include "division.h"
int main() {
std::cout << "Hello World" << std::endl;
std::cout << __cplusplus << std::endl;
std::cout << "1 + 2 = " << add(1 ,2) << std::endl;
std::cout << "3 - 1 = " << sub(3, 1) << std::endl;
std::cout << "2 * 3 = " << mult(2, 3) << std::endl;
std::cout << "6 / 2 = " << divide(6, 2) << std::endl;
return 0;
}
CMakefiles.txt
# 设置最低本版号
cmake_minimum_required(VERSION 3.10)
# 项目名 版本 描述 主页 语言
project(demo LANGUAGES CXX VERSION 1.0 )
# 设置C++标准
set(CMAKE_CXX_STANDARD 11)
# 设置运行时目标文件(exe、dll)的输出位置
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# # 设置存档目标文件(lib、a)的输出位置
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
# 添加子目录
add_subdirectory(calc)
# 添加可执行程序
add_executable(demo main.cpp)
# 链接生成的静态库
target_link_libraries(demo PUBLIC addition)
# 链接生成的动态库
target_link_libraries(demo PUBLIC subtract)
# 外部的静态库
target_link_libraries(demo PUBLIC ${PROJECT_SOURCE_DIR}/../other_lib/libmultiply.a)
# 外部的动态库
target_link_libraries(demo PUBLIC ${PROJECT_SOURCE_DIR}/../other_lib/libdivision.dll)
# (目标文件) 头文件的搜索目录
target_include_directories(demo PUBLIC
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/calc
${PROJECT_SOURCE_DIR}/../other_lib
)
# binary_dir 指定编译结果存放的位置
add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
# STATIC 静态库
# SHARED 动态库
# 生成的库文件名为 lib.xxx
add_library(<name> [STATIC | SHARED | MODULE]
[EXCLUDE_FROM_ALL]
[<source>...])
# item 可以是target名、绝对路径(必须保证文件存在)
target_link_libraries(<target>
<PRIVATE|PUBLIC|INTERFACE> <item>...
[<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
虽然官方说target_link_libraries
说是要指定绝对路径,但我测了相对路径也没问题
# 外部的静态库
target_link_libraries(demo PUBLIC ${PROJECT_SOURCE_DIR}/../other_lib/libmultiply.a)
# 外部的动态库
target_link_libraries(demo PUBLIC ${PROJECT_SOURCE_DIR}/../other_lib/libdivision.dll)
这里说的安装其实就是类似与打包发布
这里基本就是固定的指令,就直接放代码了
install(TARGETS <target> DESTINATION <dir>)
install(FILES <file> DESTINATION <dir>)
install(PROGRAMS <非目标文件的可执行程序> DESTINATION <dir>) # 如脚本
install(DIRECTORY <dir> DESTINATION <dir>) # 安装目录
# 安装到默认目录 CMAKE_INSTALL_PREFIX
cmake --install .
# 安装到指定目录
cmake --install . --prefix <dir>
cmake_minimum_required(VERSION 3.10)
set(CMAKE_CXX_STANDARD 11)
# 动态bin 静态lib
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
project(demo VERSION 1.0 LANGUAGES CXX)
option(DATE_ENABLE "output date" ON)
if(DATE_ENABLE)
set(DATE "2022")
endif()
# 配置文件
configure_file(config.h.in config.h)
# 子目录
add_subdirectory(calc)
add_executable(demo main.cpp)
# 本项目的内部库
target_link_libraries(demo PUBLIC addition)
target_link_libraries(demo PUBLIC subtract)
# 外部库
target_link_libraries(demo PUBLIC C:/Users/windows/Desktop/demo/other_lib/libdivision.dll)
target_link_libraries(demo PUBLIC C:/Users/windows/Desktop/demo/other_lib/libmultiply.a)
# include_directories(${PROJECT_BINARY_DIR})
target_include_directories(demo PUBLIC
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/calc
${PROJECT_SOURCE_DIR}/../other_lib
)
# 目标程序到bin
install(TARGETS demo DESTINATION bin)
# 配置到include中
install(FILES "${PROJECT_BINARY_DIR}/config.h" DESTINATION include)
# 外部库,静态到lib 动态到bin
# 头文件都到include
install(FILES "C:/Users/windows/Desktop/demo/other_lib/libmultiply.a" DESTINATION lib)
install(FILES "C:/Users/windows/Desktop/demo/other_lib/libdivision.dll" DESTINATION bin)
install(FILES "C:/Users/windows/Desktop/demo/other_lib/multiply.h" DESTINATION include)
install(FILES "C:/Users/windows/Desktop/demo/other_lib/division.h" DESTINATION include)
add_library(addition STATIC addition.cpp)
# 静态到lib 对于当前来说,这个静态库就是target
# 头文件到include
install(TARGETS addition DESTINATION lib)
install(FILES addition.h DESTINATION include)
add_library(subtract SHARED subtract.cpp)
# 动态到bin 对于当前来说,这个静动态库就是target
# 头文件到include
install(TARGETS subtract DESTINATION bin)
install(FILES subtract.h DESTINATION include)
#include
#include "addition.h"
#include "config.h"
#include "division.h"
#include "multiply.h"
#include "subtract.h"
using namespace std;
int main() {
cout << "Hello World" << endl;
cout << "version " << PROJECT_VERSION_MAJOR << "." << PROJECT_VERSION_MINOR
<< endl;
#ifdef DATE
cout << "date " << DATE << endl;
#endif
cout << "1 + 2 = " << add(1, 2) << endl;
cout << "3 - 1 = " << sub(3, 1) << endl;
cout << "2 * 3 = " << mult(2, 3) << endl;
cout << "6 / 2 = " << divide(6, 2) << endl;
return 0;
}
# 最好先重新构建一遍
# 安装到外层目录的install中
cmake --install . --prefix ../install