本文介绍: CMake 是一个强大的构建工具,也是C/C++程序员应该必备的技能之一CMake入门教程_哔哩哔哩_bilibili找了好多教学资料不是基于linux就是跳跃性较大。偶然发现一个不错的好资源就学习了一波。本文参考上面b站的教学视频,这个教学十分推荐,新手保姆级的,up也分享了自己的笔记和demo本文主要记录库的生成和链接(CMake) 从下载到构建第一个CMake应用_main.cppreturn 0;}
前言
CMake 是一个强大的构建工具,也是C/C++程序员应该必备的技能之一
找了好多教学资料不是基于linux就是跳跃性较大。偶然发现一个不错的好资源就学习了一波。
本文参考上面b站的教学视频,这个教学十分推荐,新手保姆级的,up也分享了自己的笔记和demo
前置准备
当前项目的库
# 设置运行时目标文件(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
)
静态库
# 库名 静态库 原文件
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)
动态库
# 库名 动态库 原文件
add_library(subtract SHARED subtract.cpp)
# 链接生成的动态库
target_link_libraries(demo PUBLIC subtract)
外部项目的库
# (目标文件) 头文件的搜索目录
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)
库的总结
总code
#include <iostream>
#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;
}
# 设置最低本版号
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
)
函数
add_subdirectory() 添加源文件目录
# binary_dir 指定编译结果存放的位置
add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
add_library() 指定的源文件生成库
# STATIC 静态库
# SHARED 动态库
# 生成的库文件名为 lib<name>.xxx
add_library(<name> [STATIC | SHARED | MODULE]
[EXCLUDE_FROM_ALL]
[<source>...])
target_link_libraries() 为目标链接库
# 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>
完整code
src cmake
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)
calc cmake
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)
main.cpp
#include <iostream>
#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
成果
END
原文地址:https://blog.csdn.net/CUBE_lotus/article/details/127168050
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_13495.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。