cmake practice summarizes the construction process and basic instructions of cmake

Basic grammar rules 0.1 variable The     variable is valued in $, but the variable name is directly used in th...
Basic grammar rules
1, Basic instruction
3, Construction and use of static library and dynamic library

Basic grammar rules

0.1 variable

The     variable is valued in $, but the variable name is directly used in the IF control statement.

  1. CMAKE_BINARY_DIR or PROJECT_BINARY_DIR or < ProjectName >_ BINARY_ Dir: all represent the compilation directory. If it is built internally, it refers to the top-level directory of the project; If it is an external build, it refers to the directory where the project compilation takes place.
  2. CMAKE_SOURCE_DIR or PROJECT_SOURCE_DIR or < ProjectName >_ SOURCE_ Dir: all represent the top-level directory of the project.
  3. CMAKE_CURRENT_SOURCE_DIR: represents the path of CMakeLists.txt currently processed.
  4. CMAKE_CURRENT_BINARY_DIR: if it is built internally, it is the same as cmake_ CURRENT_ SOURCE_ Same as dir; If it is an external build, it represents the target compilation directory.
  5. CMAKE_CURRENT_LIST_FILE: the full path of CMakeLists.txt where this variable is located.
  6. CMAKE_CURRENT_LIST_LINE: the line where this variable is located.
  7. CMAKE_MODULE_PATH: defines the path where the cmake module is located
  8. EXECUTABLE_OUTPUT_PATH and LIBRARY_OUTPUT_PATH: redefine the storage directory of the final results respectively.
  9. PROJECT_NAME: project name
0.2 instruction rules

   1. The basic syntax is: instruction (parameter 1, parameter 2...)
   2. Use spaces or semicolons between parameters, such as ADD_EXECUTABLE(hello main.c;func.c)
   3. The instruction is not case sensitive, and the parameters and variables are case sensitive, but it is recommended to use all uppercase instructions
   4. When the file name contains spaces, double quotation marks must be used, such as SET(SRC_LIST "fu nc.c")

0.3 basic construction process

  1. Write the program and CMakeLists.txt file
  2. Create an external compilation Directory: mkdir build
  3. Enter the external compilation Directory: cd build
   4. Construction Engineering: cmake
  5. Actual build: make
   6. Running program:. / < executable filename >
  7. Cleaning works: make clean

1, Basic instruction

1. PROJECT instruction
PROJECT(projectname [CXX] [C] [Java]) # Example: PROJECT(HELLO)

   define the project name, and formulate the languages supported by the project. All languages are supported by default. This instruction implicitly defines two variables PROJECT_BINARY_DIR and PROJECT_SOURCE_DIR.

2. SET instruction
SET(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]]) # Example 1: SET(SRC_LIST main.c) # Example 2: set (executable_output_path $ / bin)

  used to explicitly define variables.
  in add_ Add the statement in example 2 to the CMakeLists.txt file where executabl is located. You can modify the output path of the final target binary file to build/bin

3. MESSAGE instruction
MESSAGE([SEND_ERROR | STATUS | FATAL_ERROR] "message to display") # Example: MESSAGE(STATUS "This is BINARY dir $")

  it is used to output user-defined information to the terminal, including three types

  • SEND_ERROR: an error occurred and the generation process was skipped
  • STATUS: outputs information prefixed with -
  • FATAL_ERROR: terminate all cmake procedures immediately
4. ADD_EXECUTABLE instruction
ADD_EXECUTABLE(<Executable Filename> $) # Example: ADD_EXECUTABLE(hello $)

   the definition project will generate an executable file named < executable filename >, and the relevant source file is SRC_LIST of source files defined in list.

5. ADD_SUBDIRECTORY instruction
ADD_SUBDIRECTORY(source_dir [binary_dir] [EXCLUDE_FROM_ALL]) # Example: ADD_SUBDIRECTORY(src bin)

   it is used to add subdirectories to the current project and specify the location where their binary files are stored. EXCLUDE_FROM_ALL means to exclude this directory from compilation. (example: add the src subdirectory to the project and set the compilation output path to bin, then the compilation results will be placed in build/bin)

6. INSTALL instruction
INSTALL(TARGETS targets... [EXPORT <export-name>] [[ARCHIVE|LIBRARY|RUNTIME|OBJECTS|FRAMEWORK|BUNDLE| PRIVATE_HEADER|PUBLIC_HEADER|RESOURCE] [DESTINATION <dir>] [PERMISSIONS permissions...] [CONFIGURATIONS [Debug|Release|...]] [COMPONENT <component>] [NAMELINK_COMPONENT <component>] [OPTIONAL] [EXCLUDE_FROM_ALL] [NAMELINK_ONLY|NAMELINK_SKIP] ] [...] [INCLUDES DESTINATION [<dir> ...]] ) # Example: install (files copy right read destination share / Doc / cmake / test) # Example: INSTALL(PROGRAMS runprog.sh DESTINATION bin)

   the INSTALL instruction is used to INSTALL various types of files. TARGETS in the parameter is the file to be installed, which can be binary files, dynamic libraries and static libraries. After writing the INSTALL instruction in each CMakeLists.txt, you can start the installation.
  the installation process is as follows:

cmake -DCMAKE_INSTALL_PREFIX=<Install Path> make make install
7. ADD_ Requirements Directive
ADD_DEPENDENCIES(target-name depend-target1 depend-target2 ...)

   other targets since target is defined to ensure that other targets have been built before the project is compiled

8. ADD_TEST and ENABLE_TESTING instruction
ADD_TEST(testname Exename arg1 arg2 ...) ENABLE_TEST()

    used to create test targets. After generating makefile s, you can test through make test.

9. EXEC_PROGRAM
EXEC_PROGRAM(Executable [directory in which to run] [ARGS <arguments to executable>] [OUTPUT_VARIABLE <var>] [RETURN_VALUE <var>])

  used to specify a program to run in a specific directory.

3, Construction and use of static library and dynamic library

3.1 construction method of static library and dynamic library

   1. Create a new lib folder under the project directory and add it to the project directory.

# CMakeLists.txt under the project directory PROJECT(HELLOLIB) ADD_SUBDIRECTORY(lib)

   2. Create the source file in the lib folder.
   3. Create CMakeLists.txt in the lib directory.

SET(LIBHELLO_SRC hello.c) #Create dynamic library ADD_LIBRARY(hello SHARED $) #Create static library ADD_LIBRARY(hello_static STATIC $) SET_TARGET_PROPERTIES(hello_static PROPERTIES OUTPUT_NAME "hello") #Implementation dynamic library version number SET_TARGET_PROPERTIES(hello PROPERTIES VERSION 1.2 SOVERSION 1) #Install shared libraries and header files INSTALL(TARGETS hello hello_static LIBRARY DESTINATION lib ARCHIVE DESTINATION lib) INSTALL(FILES hello.h DESTINATION include/hello)

  4. Install shared libraries and header files

cd build cmake -DCMAKE_INSTALL_PREFIX=/usr .. make make install
3.2 use of external shared libraries and header files

   1. Create the src directory under the new project directory and write the source file main.c in it
  2. Write CMakeLists.txt under the project directory

PROJECT(NEWHELLO) ADD_SUBDIRECTORY(src)

  3. Write CMakeLists.txt under src directory

ADD_EXECUTABLE(main main.c) # Add header file search path INCLUDE_DIRECTORIES(/usr/include/hello) # Link the target file to a shared library TARGET_LINK_LIBRARIES(main libhello.so) # Or: TARGET_LINK_LIBRARIES(main hello) # Or link to static library: TARGET_LINK_LIBRARIES(main libhello.a)

  4. Build and run

cd build cmake .. make

Reference from <cmake practice>

14 September 2021, 21:09 | Views: 3408

Add new comment

For adding a comment, please log in
or create account

0 comments