cmake的基本使用
1. cmake的常用指令
cmake是一个跨平台的安装编译软件,可以用简单的语法规则描述所有平台的安装编译过程,下面介绍cmake的常用指令
cmake_minimum_required
用于指定CMake要求的最低版本
eg:cmake_minimum_required(VERSION 3.10)
project
用于定义工程的名称并指定工程支持的语言
eg:project(HELLOWORD CXX)
set
用于定义变量
eg:set(SOURCE main.cpp)
include_directories
添加工程需要的头文件
eg:include_directories(./include ./include2 ./include3)
link_directories
用于向工程中添加多个特定的库文件搜索路径
eg:link_directories(./lib ./lib1 ./lib2)
add_library
生成库文件
eg:add_library(hello SHARED/STATIC helper.cpp helper2.cpp)
add_compile_options
添加编译参数
eg:add_compile_options(-Wall -std=c++11 -w -o2)
add_executable
生成可执行文件
eg:add_executable(main main.cpp)
target_link_libraries
为target添加需要连接的共享库文件
eg:target_link_libraries(main hello)
add_subdirectory
添加子目录
eg:add_subdirectory(./src)
aux_source_directory
发现目录下是所有源文件
eg:aux_source_directory(./src srccpp) add_executable(main ${srccpp})
2. cmake的常用变量
CMAKE_C_FLAGS
gcc编译选项CMAKE_CXX_FLAGS
g++编译选项
eg:set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -o2 -w ")
CMAKE_BUILD_TYPE
编译选项
eg:set(CMAKE_BUILD_TYPE Release/Debug)
3. cmake的常见使用场景
- 单个文件外部编译:
最简单的一种情况:文件目录结构如下所示:
文件中仅有一个mian.cpp
cmake编写如下:
cmake_minimum_required(VERSION 3.10)
project(HELLOCMAKE)
add_compile_options(-w -o2)
add_executable(compile_with_cmake main.cpp)
shell执行如下:
mkdir build
cd build
cmake ..
make
- 多文件外部编译
考虑复杂一点的情况,通常我们有多个cpp文件需要编译,考虑下面一种情况,文件目录如下所示:
cmake编写如下:
shell执行如下:cmake_minimum_required(VERSION 3.10) project(HELLOCMAKE2) include_directories(include) aux_source_directory("${CMAKE_SOURCE_DIR}/src" srccpp) add_compile_options(-std=c++11 -o2 -w) add_executable(main_with_cpp main.cpp ${srccpp})
考虑更复杂一点的情况,我们可以将以来源文件打包成库文件mkdir build cd build cmake .. make
或者打包成动态库cmake_minimum_required(VERSION 3.10) project(HELLOCMAKE) add_compile_options(-std=c++11 -o2) include_directories(${CMAKE_SOURCE_DIR}/include) add_library(srclib STATIC ${CMAKE_SOURCE_DIR}/src/helper.cpp) link_directories(${CMAKE_SOURCE_DIR}/build) link_libraries(srclib) add_executable(main main.cpp)
考虑更复杂一点的情况,我们可以在子文件中添加cmake,在主文件夹中使用add_subdirectories进行合并cmake_minimum_required(VERSION 3.10) project(HELLOCMAKE) add_compile_options(-std=c++11 -o2) include_directories(${CMAKE_SOURCE_DIR}/include) add_library(srclib SHARED ${CMAKE_SOURCE_DIR}/src/helper.cpp) link_directories(${CMAKE_SOURCE_DIR}/build) # link_libraries(srclib) add_executable(main main.cpp) target_link_libraries(main srclib)
主文件Cmake
子文件Cmakecmake_minimum_required(VERSION 3.10) project(HELLOCMAKE) add_subdirectory(src) link_directories(src) link_libraries(srclib) include_directories(include) add_executable(main main.cpp)
add_compile_options(-std=c++11 -o2) include_directories(${CMAKE_SOURCE_DIR}/include) add_library(srclib STATIC helper.cpp)
4. VSCODE和CMAKE的组合拳
vscode本身只是个编辑器,他的编译和调试功能由额外安装的编译器和调试器支持,在.vscode文件中,通过配置launch.json 和tasks.json进行编辑器和调试器的调用
- 调试cmake编译完成文件的注意点
- 首先需要将program选项指定到编译好的可执行文件的为止
- 其次配置prelaunchTask进行自动编译,避免每次调试都手动编译,
具体代码如下:
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "Launchfdgsd", "cwd": "${workspaceFolder}", "request": "launch", "type":"cppdbg", "program" : "/home/program/build/main_cpp", "preLaunchTask":"mybuild" }, ] }
- tasks.json 对prelaunchTask指定的任务进行定义,具体如下:
{ "tasks": [ { "type": "shell", "label": "cmake", "command": "cmake", "args": [ ".." ], "options": { "cwd":"${workspaceFolder}/build" }, }, { "type": "shell", "label": "make", "command":"make", "args": [], "options": { "cwd": "${workspaceFolder}/build" } }, { "label": "mybuild", "dependsOn":["cmake","make"] } ], "version": "2.0.0" }
本文是我在参考了b站up主的视频后写的学习记录,链接为:https://www.bilibili.com/video/BV1fy4y1b7TC/?spm_id_from=333.337.search-card.all.click&vd_source=f2f6059a5dbe8f2c10ef6c9ab70b57d6
标签:cmake,VScode,eg,add,cpp,CMAKE,main,组合拳 From: https://www.cnblogs.com/hhyandcpp/p/17058430.html