Some records used by CMakeLists.txt

CMakeLists common commands

CMAKE_MINIMUM_REQUIRED(VERSION ...)
project(ProjectName)
configure_file(...)
include(...)
add_subdirectory(...)
set(...)
find_package(...)
include_directories(...)
link_directories(...)
link_libraries(...)
add_library(myLib ...)
set_property(TARGET ... PROPERTY ...)
aux_source_directory(...)
target_link_libraries(...)
add_executable(...)
add_dependencies(...)

Use self-check, official documents , or there are many translations on the Internet.

In general, the above commands are sufficient for projects (probably).
Below are a few of the situations encountered.

Set the compilation parameters of a project separately, etc.

For example, there is a dynamic library named LIB1, and the compilation options of this library need to be set separately: (Actually, I am not sure, should it be set separately?)

add_libraries(LIB1 SHARED ${src_lib1})
set_property(TARGET LIB1 PROPERTY POSITION_INDEPENDENT_CODE ON)         #代表-fPIC
set_property(TARGET LIB1 PROPERTY COMPILE_FLAGS " -DMACRO1 -DMACRO2")   #定义一些宏

properties can be viewed from cmake --help-property-list.

How to install the library found by find_package

For example, when the package name is PaC, first look for FindPac.cmake and the module directory, if not, look for PaCConfig.cmake or pac-config.cmake.
In the latter case, you generally need to use cmake to install the library to the default path before it can be searched.
Some libraries such as jsoncpp need to specify the generation of Config during cmake configuration to save the configuration file

# build 文件夹内
cmake .. -DJSONCPP_WITH_CMAKE_PACKAGE=ON
make
sudo make install

The find_package command in cmake can add parameters such as QUITE REQUIRED EXACT for self-checking.

Add other flags to CC_FLAGS

For example, if you want to add some flags to the flags of nvcc, because the type itself is list, you can use list(APPEND) to add it directly.

file(GLOB PATH_SRCS dir1/*.cu dir2/*.cpp)                               #查找文件path
set(CUDA_PROPAGATE_HOST_FLAGS OFF)
list(APPEND CUDA_NVCC_FLAGS "-DMACRO1" "std=c++11" "-rdc=true" "-O3")   #添加flags

Reason:
I used to write set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -DMACRO1")
wrong as set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAG} -DMACRO1")
The result was missing a lot of flags, and this error will not be reported.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325732266&siteId=291194637