CMakeLists.txt write commonly used commands

Note: This article quoted from https://www.cnblogs.com/hbccdf/p/introduction_of_cmake.html

1. Set the minimum version cmake

cmake_minimum_required (VERSION 2.8)

2. Set the project name

project(demo)

3. Compile target type setting

  • add_executable: An executable file
  • add_library: Create a library file

add_libraryThe default generate static library, you can specify the type of display generated library:

#静态库
add_library(test STATIC test.cpp)

#动态库
add_library(test SHARED test.cpp)

the resulting file is executable under windows *.exe, static library is a *.libdynamic library is *.liband *.dllthere, but the *.libfile is very small, just point to *.dllthe file, the equivalent of soft connections under Linux.

Executable under Linux no extension, static library is a lib*.afile, dynamic libraries lib*.sofiles.

4. Specify the source file containing the compiled

1. clearly indicate the source files included

add_executable(demo main.cpp test.cpp util.cpp)

2. Search the specified directory of all the cpp files

aux_source_directory(. SRC_LIST) #搜索当前目录下的所有.cpp文件
add_executable(demo ${SRC_LIST})

3. Custom search rules

file(GLOB SRC_LIST "*.cpp" "*.cc")
add_executable(demo ${SRC_LIST})

GLOBDoes not support recursively traverse subdirectories, you want to achieve recursively traverse subdirectories, useGLOB_RECURSE

4. Include multiple folders in the file

file(GLOB SRC_LIST "*.cpp" "protocol/*.cpp")
add_executable(demo ${SRC_LIST})
# 或者
file(GLOB SRC_LIST "*.cpp")
file(GLOB SRC_PROTOCOL_LIST "protocol/*.cpp")
add_executable(demo ${SRC_LIST} ${SRC_PROTOCOL_LIST})
# 或者
aux_source_directory(. SRC_LIST)
aux_source_directory(protocol SRC_PROTOCOL_LIST)
add_executable(demo ${SRC_LIST} ${SRC_PROTOCOL_LIST})

The directory is provided comprising

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}
    ${CMAKE_CURRENT_BINARY_DIR}
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)

Linux can also be provided by the flag:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I${CMAKE_CURRENT_SOURCE_DIR}")

6. Set link library search directory

link_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/libs64
)

Linux can also be provided by the flag:

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -L${CMAKE_CURRENT_SOURCE_DIR}/libs64")

7. Set the required link libraries

Link Library Catalog Search

target_link_libraries(demo test)

This command will link library catalog (including the default library directory and specified a custom library directory) under the Search for files:

  • Windows will search for the next test.libfile;
  • Under Linux searches libtest.aand libtest.so.

When the dynamic and static libraries exist, the default priority dynamic link library, you can specify dynamic or static library when the library link:

target_link_libraries(demo test.a)  # 链接静态库
target_link_libraries(demo test.so) # 链接动态库

Specify the full path

target_link_libraries(demo ${CMAKE_CURRENT_SOURCE_DIR}/lib/libtest.a)
target_link_libraries(demo ${CMAKE_CURRENT_SOURCE_DIR}/lib/test.so)

Specify multiple link library

target_link_librariesYou can add more than one link library:

target_link_libraries(demo 
    ${CMAKE_CURRENT_SOURCE_DIR}/libs64/libtest.a 
    pthread)

8. Set Variables

1. set the variable values ​​set directly

set(SRC_LIST main.cpp test.cpp)
add_executable(demo ${SRC_LIST})

2. set the value of additional variables

set(SRC_LIST main.cpp)
set(SRC_LIST ${SRC_LIST} test.cpp)
add_executable(demo ${SRC_LIST})

3. list value addition or deletion of variables

set(SRC_LIST main.cpp)
list(APPEND SRC_LIST test.cpp)
list(REMOVE_ITEM SRC_LIST main.cpp)
add_executable(demo ${SRC_LIST})

9. Control Condition

if...else...elseif...endif

if(MSVC)
    set(LINK_LIBS common)
else()
    set(boost_thread boost_log.a boost_system.a)
endif()

target_link_libraries(demo ${LINK_LIBS})


if(${CMAKE_BUILD_TYPE} MATCHES "debug")
    ...
else()
    ...
endif()

while break continue foreach endwhile endforeach

while(TRUE)
  message(STATUS "While true")
  break()
endwhile()

foreach(project_file ${COMMON_PROJECT_FILES})
        message(STATUS "project file found -- ${project_file}")
        include("${project_file}")
    endforeach()

10. Print message

message(${MY_VAR})
message("build with debug mode")
message(WARNING "this is warnning message")
message(FATAL_ERROR "this build has many error") # 会导致生成失败

11. Other cmake file contains

include(./common.cmake) #指定包含文件的全路径
include(def) #在搜索路径中搜索def.cmake文件
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) #设置include的搜索路径

12. Multiple directory

Can be used add_subdirectoryto add a subdirectory of the way, attention also needs a subdirectory under the CMakeLists.txtdocument.

Take https://www.cnblogs.com/hbccdf/p/introduction_of_cmake.html The example shows that the project directory

./demo
    |
    +--- main.cc
    |
    +--- math/
          |
          +--- MathFunctions.cc
          |
          +--- MathFunctions.h

After adding cmake file structure is as follows:

./demo
    |
    +--- CMakeLists.txt
    |
    +--- main.cc
    |
    +--- math/
          |
          +--- CMakeLists.txt
          |
          +--- MathFunctions.cc
          |
          +--- MathFunctions.h

demo under CMakeLists.txt file as follows:

cmake_minimum_required (VERSION 2.8)
project(demo)
aux_source_directory(. DIR_SRCS)

# 添加math子目录
add_subdirectory(math)

# 指定生成目标
add_executable(demo ${DIR_SRCS})

# 添加链接库
target_link_libraries(demo MathFunctions)

math CMakeLists.txt directory under the file as follows:

aux_source_directory(. DIR_LIB_SRCS)
# 生成链接库
add_library(MathFunctions ${DIR_LIB_SRCS})

When using this mode, we should pay attention to when writing code modules planned.

13. Common variable

1. Build type

CMAKE_BUILD_TYPE C variable corresponding compiler option C ++ compiler options corresponding variable
None CMAKE_C_FLAGS CMAKE_CXX_FLAGS
Debug CMAKE_C_FLAGS_DEBUG CMAKE_CXX_FLAGS_DEBUG
Release CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_RELEASE
RelWithDebInfo CMAKE_C_FLAGS_RELWITHDEBINFO CMAKE_CXX_FLAGS_RELWITHDEBINFO
MinSizeRel CMAKE_C_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_MINSIZEREL

2. Specify the type of compiler

Widnows

  1. the windows arranged in VS may be selected, the default configuration is generated in four.
  2. To change the default configuration, you need the cmake configuration file:
set(CMAKE_CONFIGURATION_TYPES "Debug;RelWithDebInfo")
  1. The default type is generated platform Win32, x64 type if required, you need to execute the following command:
cmake -G "Visual Studio 14 2015 Win64" ..

linux

  1. By default, Linux will generate none type of configuration;
  2. Can be configured via the command line specifies:
cmake -DCMAKE_BUILD_TYPE=Debug ..
  1. CMake also be specified in the configuration file:
set(CMAKE_BUILD_TYPE DEBUG)

3. Variables

  • Custom Variables used to define the set, such as:
set(OBJ_NAME, xxxxx)
  • Used used ${}, and similar shell, such as${OBJ_NAME}
  • If the commands, variable names can be used directly, no need to add ${}.

4. built-in variables

  • CMAKE_BINARY_DIR, PROJECT_BINARY_DIR, _BINARY_DIR: these three variables the same content, if it is an internal compiler, it refers to the top-level directory of the project, if it is an external compiler, refers to the project directory compiled occurred.

  • CMAKE_SOURCE_DIR, PROJECT_SOURCE_DIR, _SOURCE_DIR: the same three variables content, all referring to the top-level directory of the project.

  • CMAKE_CURRENT_BINARY_DIR: External compile time, referring to the target directory, when an internal compiler, referring to the top-level directory

  • CMAKE_CURRENT_SOURCE_DIR: directory where CMakeList.txt

  • CMAKE_CURRENT_LIST_DIR: the full path CMakeList.txt

  • CMAKE_CURRENT_LIST_LINE: Line current location

  • CMAKE_MODULE_PATH: If the project complex and may need to write some cmake modules - where specified by this variable SET

  • LIBRARY_OUTPUT_DIR, BINARY_OUTPUT_DIR: libraries and executables final storage directory

5. Environment Variables

  • Use environment variable:$ENV{Name}
  • Write environment variables: set(ENV{Name} value) # There is no "$" symbol

14. The system information

  • CMAKE_MAJOR_VERSION, CMAKE major version number, such as in 2.4.6 2
  • CMAKE_MINOR_VERSION, CMAKE minor version number, such as 2.4.6 4
  • CMAKE_PATCH_VERSION, CMAKE patch level, such as 2.4.6 6
  • CMAKE_SYSTEM, system name, such as Linux-2.6.22
  • CMAKE_SYSTEM_NAME, does not contain a version of the system name, such as Linux
  • CMAKE_SYSTEM_VERSION, system version, such as 2.6.22
  • CMAKE_SYSTEM_PROCESSOR, processor name, such as i686
  • UNIX, UNIX platforms in all classes is TRUE, including OS X and cygwin
  • WIN32, all win32 platform to TRUE, including cygwin

15. Switch Options

  • BUILD_SHARED_LIBS: Used to control the default library compilation mode, if not set to use add_libraryin the absence of a specified type of library, static libraries are generated by default. If you set set(BUILD_SHARED_LIBS ON)the default to generate dynamic library.
  • CMAKE_C_FLAGSSet C compiler options, you can also add_definitions()add
  • CMAKE_CXX_FLAGSSetting C ++ compiler options, you can also command add_definitions()added.
  • optionYou can add cmake build options.
    If we want to add a macro in the code:
...

#ifdef USE_MACRO

...

#endif

We can open and close control code by adding the following code in the project in CMakeLists.txt.

option(USE_MACRO
"Build the project using macro"
OFF)

IF(USE_MACRO)

    add_definitions("-DUSE_MACRO")

endif(USE_MACRO)

add_definitionsThe effect is to add a code that can be used in a macro, but optionthe effect is to add a cmake parameters can be used when building project, you can use the following control commands:

cmake  -DUSE_MACRO=on ..
cmake  -DUSE_MACRO=off ..

Guess you like

Origin www.cnblogs.com/xl2432/p/11225276.html