cmake 學習筆記
代碼地址:
https://gitcode.net/u014254963/cmake-study/-/tree/master/hello_cmake_project
https://gitcode.net/u014254963/cmake-study/-/tree/master/hello_cmake_project_vs
本文目標
- 多目錄構建
- 引用自己寫的動態(tài)庫
- 關于單元測試的一些實踐
- 使用 python 腳本控制構建的生命周期的目標描述
注意
- 本文不涉及任何靜態(tài)庫的操作
- 本文不涉及任何第三方庫的 find_package 操作
- 本文不涉及任何 install 操作
簡單的多目錄
linux 與 vs 不需要測試
目錄結構
F:\2023\code\cmake\hello_cmake_project>tree /f
卷 dox 的文件夾 PATH 列表
卷序列號為 34D2-6BE8
F:.
│ CMakeLists.txt
│
├─.vscode
│ launch.json
│ tasks.json
│
├─include
│ person.h
│
└─src
CMakeLists.txt
main.cpp
person.cpp
F:\2023\code\cmake\hello_cmake_project>
c++
person.h
#ifndef _PERSON_H_
#define _PERSON_H_
#include <string>
class Person
{
public:
Person();
~Person();
std::string SayHello();
};
#endif // _PERSON_H_
person.cpp
#include "person.h"
Person::Person()
{
}
Person::~Person()
{
}
std::string Person::SayHello()
{
return "hello world";
}
main.cpp
#include <iostream>
#include "person.h"
int main()
{
Person *p = new Person();
std::cout << p->SayHello() << std::endl;
delete p;
std::cout << "hello world" << std::endl;
return 0;
}
CMakeLists.txt
CMakeLists.txt
# 該項目所需 cmake 的最小版本, 如果 cmake 版本小于設置的版本, cmake 將停止處理并報錯
cmake_minimum_required(VERSION 3.0)
# 設置項目名稱和語言
project(hello_cmake_project CXX)
set(CMAKE_BUILD_TYPE DEBUG)
# 指定 c++ 11
set (CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
MESSAGE(STATUS "源碼目錄: " ${PROJECT_SOURCE_DIR})
MESSAGE(STATUS "構建目錄: " ${PROJECT_BINARY_DIR})
add_subdirectory(src)
src/CMakeLists.txt
# 頭文件目錄
include_directories(${PROJECT_SOURCE_DIR}/include)
# 源文件列表
aux_source_directory(. SRCS)
# 可執(zhí)行文件目錄
# set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
# 使用指定的源文件向項目添加可執(zhí)行文件
add_executable(${PROJECT_NAME} ${SRCS})
使用過程
F:\2023\code\cmake\hello_cmake_project>cmake -S . -G "Unix Makefiles" -B build && cmake --build build --config debug
-- The CXX compiler identification is GNU 10.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/program/tdmgcc/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- 源碼目錄: F:/2023/code/cmake/hello_cmake_project
-- 構建目錄: F:/2023/code/cmake/hello_cmake_project/build
-- Configuring done
-- Generating done
-- Build files have been written to: F:/2023/code/cmake/hello_cmake_project/build
[ 33%] Building CXX object src/CMakeFiles/hello_cmake_project.dir/main.cpp.obj
[ 66%] Building CXX object src/CMakeFiles/hello_cmake_project.dir/person.cpp.obj
[100%] Linking CXX executable hello_cmake_project.exe
[100%] Built target hello_cmake_project
F:\2023\code\cmake\hello_cmake_project>.\build\src\hello_cmake_project.exe
hello world
hello world
F:\2023\code\cmake\hello_cmake_project>
多目錄生成動態(tài)庫并使用
linux 和 vs 均需測試
源碼
目錄結構
F:\2023\code\cmake\hello_cmake_project>tree /f
卷 dox 的文件夾 PATH 列表
卷序列號為 34D2-6BE8
F:.
│ CMakeLists.txt
│
├─.vscode
│ launch.json
│ tasks.json
│
├─include
│ └─human
│ person.h
│
└─src
│ CMakeLists.txt
│
├─human
│ CMakeLists.txt
│ person.cpp
│
└─main
CMakeLists.txt
main.cpp
F:\2023\code\cmake\hello_cmake_project>
c++
只需要修改一下 person.h
引入的路徑即可
#include "human/person.h"
CMakeLists.txt
CMakeLists.txt
# 該項目所需 cmake 的最小版本, 如果 cmake 版本小于設置的版本, cmake 將停止處理并報錯
cmake_minimum_required(VERSION 3.0)
# 設置項目名稱和語言
project(hello_cmake_project CXX)
set(CMAKE_BUILD_TYPE DEBUG)
# 指定 c++ 11
set (CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# human 庫名稱
set(lib_human human)
add_subdirectory(src)
src/CMakeLists.txt
add_subdirectory(main)
add_subdirectory(human)
src/human/CMakeLists.txt
# 頭文件目錄
include_directories(${PROJECT_SOURCE_DIR}/include)
# 源文件列表
aux_source_directory(. HUMAN_SRCS)
# 設置生成目錄
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/dist)
# 生成動態(tài)庫, 默認生成的是靜態(tài)庫
add_library(${lib_human} SHARED ${HUMAN_SRCS})
# 指定動態(tài)庫版本
# VERSION 動態(tài)庫版本
# SOVERSION API版本
set_target_properties(${lib_human} PROPERTIES VERSION 1.0 SOVERSION 1)
src/main/CMakeLists.txt
# 頭文件目錄
include_directories(${PROJECT_SOURCE_DIR}/include)
# 源文件列表
aux_source_directory(. MAIN_SRCS)
# 可執(zhí)行文件目錄
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/dist)
# 使用指定的源文件向項目添加可執(zhí)行文件
add_executable(${PROJECT_NAME} ${MAIN_SRCS})
# 聲明鏈接時需要參與的依賴庫名稱
target_link_libraries(${PROJECT_NAME} ${lib_human})
命令行驗證
注意: 缺失 dll 的提示需要在 cmd 下才能驗證出來, 當然直接雙擊 exe 也可以驗證
F:\2023\code\cmake\hello_cmake_project>cmake -S . -G "Unix Makefiles" -B build && cmake --build build --config debug
-- The CXX compiler identification is GNU 10.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/program/tdmgcc/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: F:/2023/code/cmake/hello_cmake_project/build
[ 25%] Building CXX object src/human/CMakeFiles/human.dir/person.cpp.obj
[ 50%] Linking CXX shared library F:/2023/code/cmake/hello_cmake_project/dist/libhuman.dll
[ 50%] Built target human
[ 75%] Building CXX object src/main/CMakeFiles/hello_cmake_project.dir/main.cpp.obj
[100%] Linking CXX executable F:/2023/code/cmake/hello_cmake_project/dist/hello_cmake_project.exe
[100%] Built target hello_cmake_project
F:\2023\code\cmake\hello_cmake_project>cd dist
F:\2023\code\cmake\hello_cmake_project\dist>dir
驅動器 F 中的卷是 dox
卷的序列號是 34D2-6BE8
F:\2023\code\cmake\hello_cmake_project\dist 的目錄
2023/01/21 18:09 <DIR> .
2023/01/21 18:09 <DIR> ..
2023/01/21 18:09 2,931,195 hello_cmake_project.exe
2023/01/21 18:09 752,047 libhuman.dll
2023/01/21 18:09 3,976 libhuman.dll.a
3 個文件 3,687,218 字節(jié)
2 個目錄 92,873,236,480 可用字節(jié)
F:\2023\code\cmake\hello_cmake_project\dist>hello_cmake_project.exe
hello world
hello world
F:\2023\code\cmake\hello_cmake_project\dist>move libhuman.dll libhuman.dll.bak
移動了 1 個文件。
F:\2023\code\cmake\hello_cmake_project\dist>hello_cmake_project.exe
F:\2023\code\cmake\hello_cmake_project\dist>
如何在 vscode 中使用
由于默認的 exe 和 dll 生成的目錄比較深, 所以重新設置了生成路徑
# src/human/CMakeLists.txt
# 設置生成目錄
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/dist)
# src/main/CMakeLists.txt
# 可執(zhí)行文件目錄
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/dist)
相應的 launch.json
也要修改
"program": "${workspaceFolder}/dist/hello_cmake_project.exe"
linux 驗證
這一部分沒有什么特殊的
ubuntu@ubuntu-cpp:~/code/cmake/hello_cmake_project$ ll
total 20
drwxrwxr-x 4 ubuntu ubuntu 4096 Jan 21 19:27 ./
drwxrwxr-x 4 ubuntu ubuntu 4096 Jan 21 19:27 ../
-rw-rw-r-- 1 ubuntu ubuntu 402 Jan 21 19:27 CMakeLists.txt
drwxrwxr-x 3 ubuntu ubuntu 4096 Jan 21 19:27 include/
drwxrwxr-x 4 ubuntu ubuntu 4096 Jan 21 19:27 src/
ubuntu@ubuntu-cpp:~/code/cmake/hello_cmake_project$ cmake -S . -B build && cmake --build build --config debug
-- The CXX compiler identification is GNU 11.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/ubuntu/code/cmake/hello_cmake_project/build
[ 25%] Building CXX object src/human/CMakeFiles/human.dir/person.cpp.o
[ 50%] Linking CXX shared library /home/ubuntu/code/cmake/hello_cmake_project/dist/libhuman.so
[ 50%] Built target human
[ 75%] Building CXX object src/main/CMakeFiles/hello_cmake_project.dir/main.cpp.o
[100%] Linking CXX executable /home/ubuntu/code/cmake/hello_cmake_project/dist/hello_cmake_project
[100%] Built target hello_cmake_project
ubuntu@ubuntu-cpp:~/code/cmake/hello_cmake_project$ ls -l dist
total 124
-rwxrwxr-x 1 ubuntu ubuntu 63384 Jan 21 19:27 hello_cmake_project
lrwxrwxrwx 1 ubuntu ubuntu 13 Jan 21 19:27 libhuman.so -> libhuman.so.1
lrwxrwxrwx 1 ubuntu ubuntu 15 Jan 21 19:27 libhuman.so.1 -> libhuman.so.1.0
-rwxrwxr-x 1 ubuntu ubuntu 60968 Jan 21 19:27 libhuman.so.1.0
ubuntu@ubuntu-cpp:~/code/cmake/hello_cmake_project$ ./dist/hello_cmake_project
hello world
hello world
ubuntu@ubuntu-cpp:~/code/cmake/hello_cmake_project$
如何在 vs 2019 中使用
這一部分稍顯繁瑣
- vs 的 debug 和 release 生成路徑不一樣
- 需要復制 dll 到 exe 所在目錄
注意: CMakeLists.txt 腳本與源碼均有變動
目錄結構
F:\2023\code\cmake\hello_cmake_project_vs>tree /f
Folder PATH listing for volume dox
Volume serial number is 34D2-6BE8
F:.
│ CMakeLists.txt
│
├───include
│ └───human
│ framework.h
│ pch.h
│ person.h
│
└───src
│ CMakeLists.txt
│
├───human
│ CMakeLists.txt
│ dllmain.cpp
│ pch.cpp
│ person.cpp
│
└───main
CMakeLists.txt
main.cpp
F:\2023\code\cmake\hello_cmake_project_vs>
c++
include/human/framework.h
#pragma once
#define WIN32_LEAN_AND_MEAN // 從 Windows 頭文件中排除極少使用的內容
// Windows 頭文件
#include <windows.h>
include/human/pch.h
// pch.h: 這是預編譯標頭文件。
// 下方列出的文件僅編譯一次,提高了將來生成的生成性能。
// 這還將影響 IntelliSense 性能,包括代碼完成和許多代碼瀏覽功能。
// 但是,如果此處列出的文件中的任何一個在生成之間有更新,它們全部都將被重新編譯。
// 請勿在此處添加要頻繁更新的文件,這將使得性能優(yōu)勢無效。
#ifndef PCH_H
#define PCH_H
// 添加要在此處預編譯的標頭
#include "framework.h"
#endif //PCH_H
include/human/person.h
#ifndef _PERSON_H_
#define _PERSON_H_
#define DllExport __declspec(dllexport)
#include <string>
class DllExport Person
{
public:
Person();
~Person();
std::string SayHello();
};
#endif // _PERSON_H_
src/human/dllmain.cpp
// dllmain.cpp : 定義 DLL 應用程序的入口點。
#include "human/pch.h"
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
src/human/pch.cpp
#include "human/pch.h"
src/human/person.cpp
#include "human/pch.h"
#include "human/person.h"
Person::Person()
{
}
Person::~Person()
{
}
std::string Person::SayHello()
{
return "hello world";
}
src/main/main.cpp
#include <iostream>
#include "human/person.h"
int main()
{
Person *p = new Person();
std::cout << p->SayHello() << std::endl;
delete p;
std::cout << "hello world" << std::endl;
return 0;
}
CMakeLists.txt
CMakeLists.txt
# 該項目所需 cmake 的最小版本, 如果 cmake 版本小于設置的版本, cmake 將停止處理并報錯
cmake_minimum_required(VERSION 3.0)
# 設置項目名稱和語言
project(hello_cmake_project CXX)
set(CMAKE_BUILD_TYPE DEBUG)
# 指定 c++ 11
set (CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# 設置編譯類型為 Debug
# set(CMAKE_BUILD_TYPE "Debug")
# set(CMAKE_BUILD_TYPE "Release")
# -------------------------------------------------------------------------------------------
#[[
將 CMAKE_BUILD_TYPE 保存到 project_debug , 避免重復判斷
設置 debug 庫后綴
]]
if(CMAKE_BUILD_TYPE)
string(TOLOWER ${CMAKE_BUILD_TYPE} BUILD_TYPE)
if(${BUILD_TYPE} STREQUAL "debug")
set(project_debug 1)
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_DEBUG_POSTFIX d)
elseif(${BUILD_TYPE} STREQUAL "release")
set(project_debug 0)
set(CMAKE_BUILD_TYPE "Release")
else()
set(project_debug 1)
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_DEBUG_POSTFIX d)
endif()
else()
set(project_debug 1)
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_DEBUG_POSTFIX d)
endif()
# -------------------------------------------------------------------------------------------
#[[
設置構建目錄
]]
if(project_debug)
set(build_dir out/build/x64-Debug)
else()
set(build_dir out/build/x64-Release)
endif()
# -------------------------------------------------------------------------------------------
#[[
全局引入的頭文件
]]
# 當前項目目錄
include_directories(${PROJECT_SOURCE_DIR}/include)
# -------------------------------------------------------------------------------------------
#[[
全局變量
]]
# 每個目標源文件所在目錄(相對于根目錄)
set(main_dir src/main)
set(human_dir src/human)
######### 主程序 變量
# 主程序可執(zhí)行文件路徑
set(main_binary_dir ${CMAKE_INSTALL_PREFIX}/${main_dir})
string(REPLACE install build main_binary_dir ${main_binary_dir})
######### human 庫 變量
# human 庫路徑
set(human_library_dir ${CMAKE_INSTALL_PREFIX}/${human_dir})
string(REPLACE install build human_library_dir ${human_library_dir})
# human 庫名稱, 不帶后綴
set(human_lib_name human)
# human 庫名稱, 帶后綴
set(human_lib_file_name ${human_lib_name}${CMAKE_DEBUG_POSTFIX}${CMAKE_SHARED_LIBRARY_SUFFIX})
# -------------------------------------------------------------------------------------------
#[[
輸出信息
]]
message(STATUS "CMAKE_BUILD_TYPE : ${CMAKE_BUILD_TYPE}")
message(STATUS "main_binary_dir ${main_binary_dir}")
message(STATUS "human_library_dir ${human_library_dir}")
message(STATUS "human_lib_name ${human_lib_name}")
message(STATUS "human_lib_file_name ${human_lib_file_name}")
if(project_debug)
message(STATUS "current project compile type is debug")
else()
message(STATUS "current project compile type is release")
endif()
add_subdirectory(src)
src/CMakeListst.txt
add_subdirectory(main)
add_subdirectory(human)
src/human/CMakeListst.txt
# 頭文件目錄
include_directories(${PROJECT_SOURCE_DIR}/include)
# 源文件列表
aux_source_directory(. HUMAN_SRCS)
# 設置生成目錄
# set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/dist)
# 生成動態(tài)庫, 默認生成的是靜態(tài)庫
add_library(${human_lib_name} SHARED ${HUMAN_SRCS})
# 指定動態(tài)庫版本
# VERSION 動態(tài)庫版本
# SOVERSION API版本
set_target_properties(${human_lib_name} PROPERTIES VERSION 1.0 SOVERSION 1)
# 指定 msvc 編譯模式
if(project_debug)
target_link_options(${human_lib_name} PRIVATE /DEBUG)
else()
target_link_options(${human_lib_name} PRIVATE /RELEASE)
endif()
# 構建后復制到主程序 exe 所在路徑
if(project_debug)
add_custom_command(TARGET ${human_lib_name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${human_library_dir}/${human_lib_file_name} ${main_binary_dir}/${human_lib_file_name}
)
else()
add_custom_command(TARGET ${human_lib_name} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${human_library_dir}/${human_lib_file_name} ${main_binary_dir}/${human_lib_file_name}
)
endif()
src/man/CMakeListst.txt
# 頭文件目錄
include_directories(${PROJECT_SOURCE_DIR}/include)
# 源文件列表
aux_source_directory(. MAIN_SRCS)
# 可執(zhí)行文件目錄
# set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/dist)
# 使用指定的源文件向項目添加可執(zhí)行文件
add_executable(${PROJECT_NAME} ${MAIN_SRCS})
# 聲明鏈接時需要參與的依賴庫名稱
target_link_libraries(${PROJECT_NAME} ${human_lib_name})
# 指定 msvc 編譯模式
if(project_debug)
target_link_options(${PROJECT_NAME} PRIVATE /DEBUG)
else()
target_link_options(${PROJECT_NAME} PRIVATE /RELEASE)
endif()
cmake 生成
debug 生成如下
1> 已為默認配置“x64-Debug”啟動 CMake 生成。
1> 命令行: "C:\WINDOWS\system32\cmd.exe" /c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "D:\PROGRAM\MICROSOFT\VS2019\IDE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="Debug" -DCMAKE_INSTALL_PREFIX:PATH="F:\2023\code\cmake\hello_cmake_project_vs\out\install\x64-Debug" -DCMAKE_CXX_COMPILER:FILEPATH="D:/program/microsoft/vs2019/ide/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe" -DCMAKE_MAKE_PROGRAM="D:\PROGRAM\MICROSOFT\VS2019\IDE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "F:\2023\code\cmake\hello_cmake_project_vs" 2>&1"
1> 工作目錄: F:\2023\code\cmake\hello_cmake_project_vs\out\build\x64-Debug
1> [CMake] -- The CXX compiler identification is MSVC 19.29.30133.0
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Check for working CXX compiler: D:/program/microsoft/vs2019/ide/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] -- CMAKE_BUILD_TYPE : Debug
1> [CMake] -- main_binary_dir F:/2023/code/cmake/hello_cmake_project_vs/out/build/x64-Debug/src/main
1> [CMake] -- human_library_dir F:/2023/code/cmake/hello_cmake_project_vs/out/build/x64-Debug/src/human
1> [CMake] -- human_lib_name human
1> [CMake] -- human_lib_file_name humand.dll
1> [CMake] -- current project compile type is debug
1> [CMake] -- Configuring done
1> [CMake] -- Generating done
1> [CMake] -- Build files have been written to: F:/2023/code/cmake/hello_cmake_project_vs/out/build/x64-Debug
1> 已提取 CMake 變量。
1> 已提取源文件和標頭。
1> 已提取代碼模型。
1> 已提取工具鏈配置。
1> 已提取包含路徑。
1> CMake 生成完畢。
release 生成如下
1> 已為配置“x64-Release”啟動 CMake 生成。
1> 命令行: "C:\WINDOWS\system32\cmd.exe" /c "%SYSTEMROOT%\System32\chcp.com 65001 >NUL && "D:\PROGRAM\MICROSOFT\VS2019\IDE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\CMake\bin\cmake.exe" -G "Ninja" -DCMAKE_BUILD_TYPE:STRING="RelWithDebInfo" -DCMAKE_INSTALL_PREFIX:PATH="F:\2023\code\cmake\hello_cmake_project_vs\out\install\x64-Release" -DCMAKE_CXX_COMPILER:FILEPATH="D:/program/microsoft/vs2019/ide/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe" -DCMAKE_MAKE_PROGRAM="D:\PROGRAM\MICROSOFT\VS2019\IDE\COMMON7\IDE\COMMONEXTENSIONS\MICROSOFT\CMAKE\Ninja\ninja.exe" "F:\2023\code\cmake\hello_cmake_project_vs" 2>&1"
1> 工作目錄: F:\2023\code\cmake\hello_cmake_project_vs\out\build\x64-Release
1> [CMake] -- The CXX compiler identification is MSVC 19.29.30133.0
1> [CMake] -- Detecting CXX compiler ABI info
1> [CMake] -- Detecting CXX compiler ABI info - done
1> [CMake] -- Check for working CXX compiler: D:/program/microsoft/vs2019/ide/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
1> [CMake] -- Detecting CXX compile features
1> [CMake] -- Detecting CXX compile features - done
1> [CMake] -- CMAKE_BUILD_TYPE : Release
1> [CMake] -- main_binary_dir F:/2023/code/cmake/hello_cmake_project_vs/out/build/x64-Release/src/main
1> [CMake] -- human_library_dir F:/2023/code/cmake/hello_cmake_project_vs/out/build/x64-Release/src/human
1> [CMake] -- human_lib_name human
1> [CMake] -- human_lib_file_name human.dll
1> [CMake] -- current project compile type is release
1> [CMake] -- Configuring done
1> [CMake] -- Generating done
1> [CMake] -- Build files have been written to: F:/2023/code/cmake/hello_cmake_project_vs/out/build/x64-Release
1> 已提取 CMake 變量。
1> 已提取源文件和標頭。
1> 已提取代碼模型。
1> 已提取工具鏈配置。
1> 已提取包含路徑。
1> CMake 生成完畢。
要點
- 從 vs 的 cmake 輸出可看出, vs 設置了
-DCMAKE_CXX_COMPILER:FILEPATH=".../cl.exe"
,-DCMAKE_MAKE_PROGRAM="...\ninja.exe"
. 關于ninja
的文檔可在https://ninja-build.org/找到. 簡單來說,ninja
是個Makefile/make
的替代品, 與 linux 對比的話, 就是把gcc
換成了cl.exe
, 把make
換成了ninja
- 與之前的使用方式相同, 構建 release 版本, 需要手動設置
set(CMAKE_BUILD_TYPE "Release")
關于單元測試
參考
淺談c++單元測試
C++單元測試工具——doctest
doctest
cmake 整合 doctest 示例
修改的是 vscode 的版本
目錄結構
F:\2023\code\cmake\hello_cmake_project>tree /f
卷 dox 的文件夾 PATH 列表
卷序列號為 34D2-6BE8
F:.
│ build.txt
│ CMakeLists.txt
│
├─.vscode
│ launch.json
│ tasks.json
│
├─include
│ └─human
│ person.h
│
├─src
│ │ CMakeLists.txt
│ │
│ ├─human
│ │ CMakeLists.txt
│ │ person.cpp
│ │
│ └─main
│ CMakeLists.txt
│ main.cpp
│
├─test
│ CMakeLists.txt
│ hellocmaketest.cpp
│
└─third
└─doctest
doctest.h
F:\2023\code\cmake\hello_cmake_project>
頂層 CMakeLists.txt
# 該項目所需 cmake 的最小版本, 如果 cmake 版本小于設置的版本, cmake 將停止處理并報錯
cmake_minimum_required(VERSION 3.0)
# 設置項目名稱和語言
project(hello_cmake_project CXX)
set(CMAKE_BUILD_TYPE DEBUG)
# 指定 c++ 14
set (CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# human 庫名稱
set(lib_human human)
add_subdirectory(src)
# 下面的就是測試相關的
# 傳遞項目根目錄
set(proj_dir ${PROJECT_SOURCE_DIR})
# 添加測試項目所在目錄
add_subdirectory(test)
# 必須添加這兩行, 而且必須在頂層 CMakeLists.txt 添加
enable_testing()
add_test(NAME hello_cmake_test COMMAND hello_cmake_test)
test 項目
CMakeLists.txt
# 該項目所需 cmake 的最小版本, 如果 cmake 版本小于設置的版本, cmake 將停止處理并報錯
cmake_minimum_required(VERSION 3.0)
# 設置項目名稱和語言
project(hello_cmake_test CXX)
set(CMAKE_BUILD_TYPE DEBUG)
# 指定 c++ 14
set (CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED True)
include_directories(${proj_dir}/include)
include_directories(${proj_dir}/third)
aux_source_directory(. TEST_SRCS)
set(EXECUTABLE_OUTPUT_PATH ${proj_dir}/dist)
add_executable(${PROJECT_NAME} ${TEST_SRCS})
target_link_libraries(${PROJECT_NAME} ${lib_human})
hellocmaketest.cpp
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include "doctest/doctest.h"
#include "human/person.h"
int add(int x , int y )
{
return x + y;
}
TEST_CASE("test add")
{
CHECK(3 == add(1,2));
}
TEST_CASE("test person")
{
Person *p = new Person();
std::string s = "hello world";
CHECK(0 == p->SayHello().compare(s));
}
使用過程
F:\2023\code\cmake\hello_cmake_project>cmake -S . -G "Unix Makefiles" -B build && cmake --build build --config debug
-- The CXX compiler identification is GNU 10.3.0
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: D:/program/tdmgcc/bin/g++.exe - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: F:/2023/code/cmake/hello_cmake_project/build
[ 12%] Building CXX object src/human/CMakeFiles/human.dir/person.cpp.obj
[ 25%] Linking CXX shared library F:/2023/code/cmake/hello_cmake_project/dist/libhuman.dll
[ 25%] Built target human
[ 37%] Building CXX object src/main/CMakeFiles/hello_cmake_project.dir/main.cpp.obj
[ 50%] Linking CXX executable F:/2023/code/cmake/hello_cmake_project/dist/hello_cmake_project.exe
[ 50%] Built target hello_cmake_project
[ 62%] Building CXX object src/human/CMakeFiles/human_static.dir/person.cpp.obj
[ 75%] Linking CXX static library F:/2023/code/cmake/hello_cmake_project/dist/libhuman.a
[ 75%] Built target human_static
[ 87%] Building CXX object test/CMakeFiles/hello_cmake_test.dir/hellocmaketest.cpp.obj
[100%] Linking CXX executable F:/2023/code/cmake/hello_cmake_project/dist/hello_cmake_test.exe
[100%] Built target hello_cmake_test
F:\2023\code\cmake\hello_cmake_project>.\dist\hello_cmake_project.exe
hello world
hello world
F:\2023\code\cmake\hello_cmake_project>.\dist\hello_cmake_test.exe
[doctest] doctest version is "2.4.9"
[doctest] run with "--help" for options
===============================================================================
[doctest] test cases: 2 | 2 passed | 0 failed | 0 skipped
[doctest] assertions: 2 | 2 passed | 0 failed |
[doctest] Status: SUCCESS!
F:\2023\code\cmake\hello_cmake_project>
使用 python 控制 cmake 構建的生命周期
使用的時候應當是什么樣的
使用方式應該是這樣:
F:\2023\code\cmake\hello_cmake_project> cm help
build 編譯
run 運行
test 測試
doxygen 生成 doxygen 文檔
F:\2023\code\cmake\hello_cmake_project> cm build
項目編譯中...
項目編譯完畢
F:\2023\code\cmake\hello_cmake_project> cm run
啟動項目...
hello world
hello world
項目運行結束
F:\2023\code\cmake\hello_cmake_project> cm test
開始測試
[doctest] doctest version is "2.4.9"
[doctest] run with "--help" for options
===============================================================================
[doctest] test cases: 2 | 2 passed | 0 failed | 0 skipped
[doctest] assertions: 2 | 2 passed | 0 failed |
[doctest] Status: SUCCESS!
測試結束
F:\2023\code\cmake\hello_cmake_project> cm doxygen
生成 doxygen 文檔
應當有什么功能
除了上述的幾個命令行參數(shù)之外, 還應該有
- 類似git 的 hook 一樣, 可以在構建前后或測試前后做一些自定義的動作
- 類似
npm init
一樣初始化一個項目 - 自定義命令別名
- windows 和 linux 均需打包測試
提供python
源碼而不是二進制(暫時沒搞定)
有哪些功能沒有做
構建之前的檢查
通常來說, 構建工具都有檢查功能, 比如校驗構建腳本是否正確, 但是目前沒啥必要
依賴管理
比如和 vcpkg
或者 Conan
的整合. 這部分做起來難度太高
和 ide 的集成
比如寫個 vscode 或者 vs 的插件, 鼠標點一點就能直接構建, 測試, 生成文檔, 發(fā)布等等. 這個難度也不小.
參考: VSCode 插件開發(fā)教程文章來源:http://www.zghlxwxcb.cn/news/detail-446754.html
下一篇:
cmake 04 使用 python 管理 cmake 工程文章來源地址http://www.zghlxwxcb.cn/news/detail-446754.html
到了這里,關于cmake 03 一個可用的 cmake 工程應當是什么樣的的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!