cmake is used to teach the construction of multiple files in multiple directories

1. Use out of source to build multiple directories and multiple files

        The file storage method here is as follows:

         

Where build is the build directory, that is, the build results and intermediate products are in this directory

Include is the include directory

src is the directory of sub functions or dependent files

main.cpp main function

From the right side of the figure above, you can find that add.cpp and a corresponding cmakelists.txt file exist in src, and a cmakelists.txt file also exists in the root directory

main.cpp

#include<iostream>
#include"include/add.h"
using namespace std;
int main()
{

    cout<<" hello world"<<endl;
    int a = 3;
    int b = 5;
    cout<<" 3 + 5 = "<<add(a,b)<<endl;
    return 0;
}


#########################################
add.cpp

#include"add.h"

int add(int& a, int& b)
{
    return a+b;
}

      The above code is the code of the two source files. It is very simple. The main function calls the sub function add of another file to complete the operation and output. Let's see how to build it using cmake

CMakeLists.txt in the root directory

# Multiple directories multiple source files
cmake_minimum_required(VERSION 2.8)

project(test1)
# Traverse all files in the current directory and use DIR_SRCS save can also be specified through SET(SRC_LIST main.c)
aux_source_directory(. DIR_SRCS)

# Add a directory to save the generated executable file. The intermediate product will not be saved. If this directory is not specified, it will be saved together with the intermediate product
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin )
# Add header file
include_directories(${PROJECT_SOURCE_DIR}/include)
# Here, the subdirectory conditions are added to the project, and the compiled output products are specified. The default output products are saved to the build/src folder,
# If no feasible file saving path is specified, it will also be saved to this path
add_subdirectory(src)

# The above definition adds the src subdirectory to the project, and specifies the path of the compilation output (including the compilation intermediate results) as
# Bin directory. If the bin directory is not specified, the compilation results (including intermediate results) will be stored in the
# build/src directory (this directory corresponds to the original src directory). After specifying the bin directory, it is equivalent to at compile time
# Rename src to bin, and all intermediate results and target binaries will be stored in the bin directory.

# Specify build target
add_executable(test1 main.cpp)

# Add link library
# Link the library file compiled from the subdirectory set src here
target_link_libraries(test1 add)

message(STATUS  ${PROJECT_SOURCE_DIR} "------Compile and link to generate executable file-----")

The above functions are explained in detail. Here are several commands to be emphasized:

The syntax of the set instruction is:
set(VAR [VALUE] [CACHE TYPE DOCSTRING [FORCE]])
At this stage, you only need to understand that the SET instruction can be used to explicitly define variables.
For example, we use SET(SRC_LIST main.c). If there are multiple source files, they can also be defined as:
set(SRC_LIST main.c t1.c t2.c).

The syntax of the message instruction is:
message([SEND_ERROR | STATUS | FATAL_ERROR] "message to display"...)
This instruction is used to output user-defined information to the terminal, including three types:
SEND_ERROR, an error was generated and the generation process was skipped.
SATUS - outputs information prefixed with.

FATAL_ERROR to terminate all cmake processes immediately
We use the STATUS information output here to demonstrate two implicit variables defined by the PROJECT instruction
HELLO_BINARY_DIR and HELLO_SOURCE_DIR.

add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
This instruction is used to add a subdirectory for storing source files to the current project, and specify intermediate binary and target binary storage
Where to put it. EXCLUDE_ FROM_ The all parameter means to exclude this directory from the compilation process, for example, the project
You may need to enter the example directory to build separately after the project is built (of course, you
You can also solve this problem by defining dependencies).
The above example defines adding the src subdirectory to the project, and specifies the path of the compilation output (including the compilation intermediate results) as
Bin directory. If the bin directory is not specified, the compilation results (including intermediate results) will be stored in the
build/src directory (this directory corresponds to the original src directory). After specifying the bin directory, it is equivalent to at compile time
Rename src to bin, and all intermediate results and target binaries will be stored in the bin directory.

CMakeLists.txt in src directory

# Find all source files in the current directory and save the names to DIR_LIB_SRCS variable

aux_source_directory(. DIR_LIB_SRCS)

# Add directory
include_directories(../include)

# Here is the path to save the library file. The intermediate product is not saved. The library file is finally saved in build/lib
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

# Generate link library
add_library(add ${DIR_LIB_SRCS})

message(STATUS "----------Generate static library complete----------" ${PROJECT_SOURCE_DIR}/add)

The final compilation result is:

Enter the build directory and type: cmake in the linux directory line   ..    Then make

  After the build, the bin directory, lib directory and corresponding src will be created in the build. After observation, it will be found that src is an intermediate product. The final executable file is in the bin folder and the library file generated by src is in the Lib folder. You can understand by trying to modify it.

Keywords: C C++ cmake

Added by Alphamonkey on Fri, 03 Dec 2021 02:52:16 +0200