国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

cmake 03 一個可用的 cmake 工程應當是什么樣的

這篇具有很好參考價值的文章主要介紹了cmake 03 一個可用的 cmake 工程應當是什么樣的。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

cmake 學習筆記
代碼地址:
https://gitcode.net/u014254963/cmake-study/-/tree/master/hello_cmake_project
https://gitcode.net/u014254963/cmake-study/-/tree/master/hello_cmake_project_vs

本文目標

  1. 多目錄構建
  2. 引用自己寫的動態(tài)庫
  3. 關于單元測試的一些實踐
  4. 使用 python 腳本控制構建的生命周期的目標描述

注意

  1. 本文不涉及任何靜態(tài)庫的操作
  2. 本文不涉及任何第三方庫的 find_package 操作
  3. 本文不涉及任何 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>

cmake 03 一個可用的 cmake 工程應當是什么樣的

多目錄生成動態(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>

cmake 03 一個可用的 cmake 工程應當是什么樣的

如何在 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 中使用

這一部分稍顯繁瑣

  1. vs 的 debug 和 release 生成路徑不一樣
  2. 需要復制 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 生成完畢。

要點

  1. 從 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
  2. 與之前的使用方式相同, 構建 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ù)之外, 還應該有

  1. 類似git 的 hook 一樣, 可以在構建前后或測試前后做一些自定義的動作
  2. 類似 npm init 一樣初始化一個項目
  3. 自定義命令別名
  4. windows 和 linux 均需打包測試 提供 python 源碼而不是二進制(暫時沒搞定)

有哪些功能沒有做

構建之前的檢查
通常來說, 構建工具都有檢查功能, 比如校驗構建腳本是否正確, 但是目前沒啥必要

依賴管理
比如和 vcpkg 或者 Conan 的整合. 這部分做起來難度太高

和 ide 的集成
比如寫個 vscode 或者 vs 的插件, 鼠標點一點就能直接構建, 測試, 生成文檔, 發(fā)布等等. 這個難度也不小.
參考: VSCode 插件開發(fā)教程

下一篇:
cmake 04 使用 python 管理 cmake 工程文章來源地址http://www.zghlxwxcb.cn/news/detail-446754.html

到了這里,關于cmake 03 一個可用的 cmake 工程應當是什么樣的的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • IPD是什么?適合什么樣的團隊?

    IPD是什么?適合什么樣的團隊?

    IPD,全稱為“Integrated Project Delivery”,即集成產(chǎn)品開發(fā)。它是一種全新的項目管理方法,最初源于建筑行業(yè),隨著時間的推移,已經(jīng)應用到了各行各業(yè)的項目管理中。IPD是一種以協(xié)同合作為核心的工作模式,在這種模式下,項目中的所有相關方在項目的整個生命周期中進行更

    2024年02月13日
    瀏覽(28)
  • 云計算學習需要什么樣的電腦

    云計算學習需要什么樣的電腦

    一、認識電腦 1. 電腦是如何組成的 2. 云計算學習需要配置什么樣的電腦 3. 學習云計算為什么對CPU、硬盤、內存性能要求高 二、電腦升級加裝 1. 如何提高舊電腦性能 2. 拆裝步驟 電腦的組成 硬件系統(tǒng):電源、主板、CPU、內存、硬盤、聲卡和網(wǎng)卡等 軟件系統(tǒng):系統(tǒng)軟件、操作

    2024年02月10日
    瀏覽(20)
  • 抖客是種什么樣的盈利方式

    抖客是什么?怎么推廣?如何設置?抖客聯(lián)盟app有什么用,類似抖客聯(lián)盟的app有哪些,抖客是種什么樣的盈利方式 抖音直播間引流、增長,抖客來幫忙!助力MCN機構培養(yǎng)達人,提升達人等級,這時候也需要一大批抖客,大家是否知道抖客是什么呢?應該要怎么進行推廣呢? “抖客”

    2023年04月08日
    瀏覽(20)
  • 法線貼圖可以實現(xiàn)什么樣的3D效果

    法線貼圖可以實現(xiàn)什么樣的3D效果

    在線工具推薦: 3D數(shù)字孿生場景編輯器 ?-? GLTF/GLB材質紋理編輯器 ?-? 3D模型在線轉換 ?-? Three.js AI自動紋理開發(fā)包 ?-? YOLO 虛幻合成數(shù)據(jù)生成器 ?-? 三維模型預覽圖生成器 ?-? 3D模型語義搜索引擎 在 3D 建模中,曲面由多邊形表示。照明計算是基于這些多邊形的幾何形狀執(zhí)

    2024年02月03日
    瀏覽(25)
  • C++ 編程需要什么樣的開發(fā)環(huán)境?

    C++ 編程需要什么樣的開發(fā)環(huán)境?

    在開始前我有一些資料,是我根據(jù)網(wǎng)友給的問題精心整理了一份「C++的資料從專業(yè)入門到高級教程」, 點個關注在評論區(qū)回復“888”之后私信回復“888”,全部無償共享給大家?。?!C++的開發(fā)環(huán)境需要: ·nbsp;操作系統(tǒng) ·nbsp;編譯器 鏈接器 ·nbsp;調試器 我大學里用的電腦是

    2024年01月16日
    瀏覽(25)
  • 百萬贊同:網(wǎng)絡安全為什么缺人? 缺什么樣的人?

    百萬贊同:網(wǎng)絡安全為什么缺人? 缺什么樣的人?

    1.網(wǎng)絡安全為什么缺人? 缺人的原因是有了新的需求 以前的時候,所有企業(yè)是以產(chǎn)品為核心的,管你有啥漏洞,管你用戶信息泄露不泄露,我只要做出來的產(chǎn)品火爆就行。 這一切隨著《網(wǎng)絡安全法》、《數(shù)據(jù)安全法》、《網(wǎng)絡安全審查辦法》等一系列有關網(wǎng)絡安全的法律法規(guī)

    2023年04月23日
    瀏覽(31)
  • CRM系統(tǒng)如何搭建?流程是什么樣的?

    CRM系統(tǒng)如何搭建?流程是什么樣的?

    ? CRM系統(tǒng)可以提高企業(yè)的銷售效率和客戶滿意度,從而增加企業(yè)的收入和利潤。但是,要想成功地上線CRM系統(tǒng),需要經(jīng)過一系列的步驟和流程,下面說說, 企業(yè)如何上線CRM系統(tǒng)?CRM系統(tǒng)搭建流程。 需求分析是CRM系統(tǒng)搭建的第一步,也是最重要的一步。在這個階段,企業(yè)需要

    2024年02月13日
    瀏覽(19)
  • 工具在接口測試中發(fā)揮什么樣的作用?

    工具在接口測試中發(fā)揮什么樣的作用?

    接口測試究竟是什么?為什么要用接口測試?它有哪些工具呢?這一連串的問題敲擊著我們,請帶著這些問題,在本文中尋找答案,我將為您打開接口測試的大門。 1 初探接口測試 接口測試是什么。它檢查數(shù)據(jù)的交換,傳遞和控制管理過程,它繞過了移動端,對服務端進行測

    2023年04月08日
    瀏覽(25)
  • 元宇宙時代的HTTP應該是什么樣?

    元宇宙時代的HTTP應該是什么樣?

    運行整個Web世界的HTTP協(xié)議即將進行一次更新!互聯(lián)網(wǎng)工程任務組(IETF)最近發(fā)布了業(yè)內諸多機構多年合作的產(chǎn)物:HTTP/3。HTTP/3使用QUIC這種全新傳輸協(xié)議可以在最具挑戰(zhàn)性的網(wǎng)絡上帶來更好的性能。同時,這些更新也讓我們在HTTP的文檔方面有更多問題需要注意。 QUIC的RFC于去

    2024年02月19日
    瀏覽(22)
  • 家電CRM是什么樣的?系統(tǒng)功能解析

    家電CRM是什么樣的?系統(tǒng)功能解析

    ? ? CRM 系統(tǒng)管理軟件出現(xiàn)以來按照企業(yè)規(guī)模和行業(yè)劃分出現(xiàn)了不同的細分類型,家電 CRM 就是其中一種。本文將簡要向您介紹, 家電 CRM 是什么,有什么功能、作用及其價格。 一、家電 CRM 概念 家電 CRM 是客戶管理軟件供應商為家電行業(yè)量身打造的一種客戶關系管理系統(tǒng)。

    2024年02月05日
    瀏覽(23)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包