CMake常用指令

CMake文件符號的意思

Square Brackets [ ]
Angle Brackets < >

https://stackoverflow.com/a/23242584/22299707

建置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
cmake -S . -B build
cmake --build build -j 8
``````



# Linux下用pkg_check_modules尋找套件
```cmake
cmake_minimum_required(VERSION 3.15)
project(fy-version)

set(CMAKE_C_STANDARD 11)

find_package (PkgConfig REQUIRED)
pkg_check_modules(FYAML IMPORTED_TARGET libfyaml)
if (FYAML_FOUND)
include_directories(${FYAML_INCLUDE_DIRS})
link_directories(${FYAML_LIBRARY_DIRS})
list(APPEND LINK_LIB_LIST ${FYAML_LIBRARIES})

message(STATUS "found library:${FYAML_LIBRARIES}")
endif ()

# 建立執行檔
add_executable(fy-version yaml_reader.c)

target_link_libraries(${PROJECT_NAME} PkgConfig::FYAML)

https://cmake.org/cmake/help/latest/module/FindPkgConfig.html

https://stackoverflow.com/questions/42634710/how-to-use-pkg-config-in-cmake-juci

https://blog.csdn.net/zhizhengguan/article/details/111826697

Debug模式編譯

1
set(CMAKE_BUILD_TYPE Debug)

引用CUDA

Declare CUDA as a LANGUAGE in your project

1
project(GTC LANGUAGES CUDA CXX)
  • 連接library
    利用FindCUDAToolkit(cmake版本>3.17)
    1
    2
    3
    4
    5
    6
    find_package(CUDAToolkit)
    add_executable(
    binary_linking_to_cudart
    my_cpp_file_using_cudart.cpp
    )
    target_link_libraries(binary_linking_to_cudart PRIVATE CUDA::cudart)
    利用FindCUDAToolkit

https://cliutils.gitlab.io/modern-cmake/chapters/packages/CUDA.html

https://developer.download.nvidia.com/video/gputechconf/gtc/2019/presentation/s9444-build-systems-exploring-modern-cmake-cuda-v2.pdf

cmake 加入編譯好的.o檔

1
2
3
4
5
6
7
8
9
10
11
12
SET(OBJS
${CMAKE_CURRENT_SOURCE_DIR}/libs/obj.o
)

ADD_EXECUTABLE(myProgram ${OBJS} <other-sources>)

SET_SOURCE_FILES_PROPERTIES(
${OBJS}
PROPERTIES
EXTERNAL_OBJECT true
GENERATED true
)

https://stackoverflow.com/a/38610428

編譯不是cmake的專案或函式庫

1
2
3
4
5
6
7
8
9
10
ExternalProject_Add(Qt
DOWNLOAD_DIR ${CMAKE_CURRENT_BINARY_DIR}
URL ${qt_file}
UPDATE_COMMAND ""
SOURCE_DIR ${qt_source}
BUILD_IN_SOURCE 1
CONFIGURE_COMMAND ${qt_configure}
BUILD_COMMAND ${qt_build}
INSTALL_COMMAND "${qt_install}"
)

https://stackoverflow.com/a/3493578/22299707

編譯專案外的makefile專案資料夾

以下指令可以在外部資料夾下make指令

1
2
3
4
5
6
7
8
9
10
include(ExternalProject)
ExternalProject_Add(deepstreamapp
SOURCE_DIR ${DEEPSTREAMAPP}/sample_apps/deepstream-app # This is not likely to be CMAKE_CURRENT_LIST_DIR
CONFIGURE_COMMAND ""
INSTALL_COMMAND ""
DOWNLOAD_COMMAND ""
BUILD_COMMAND make
BUILD_IN_SOURCE true
)

接下來可以用以下指令找出所有編譯好的.o檔並且加到專案內

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
file(GLOB_RECURSE  DEEPSTREAMAPPCONFIGPARSER "/opt/nvidia/deepstream/deepstream/sources/apps/apps-common/src/deepstream-yaml/*.o")

SET(OBJS
${DEEPSTREAMAPPCONFIGPARSER}
)

SET_SOURCE_FILES_PROPERTIES(
${OBJS}
PROPERTIES
EXTERNAL_OBJECT true
GENERATED true
)

add_executable(${PROJECT_NAME}
${OBJS}
main.cpp
)

參考:https://discourse.cmake.org/t/external-project-using-makefile/2692/5

增加函式庫

https://cmake.org/cmake/help/latest/guide/tutorial/Adding%20a%20Library.html#step-2-adding-a-library
target_include_directories()target_link_libraries()