CMake practice 2: multiple source files, the same or multiple directories

Posted by s_ainley87 on Sat, 25 Dec 2021 02:22:04 +0100

title: CMake practice 2: multiple source files, the same or multiple directories

categories: [actual combat II]

tags:[CMake]

date: 2021/12/23

Author: hackett

WeChat official account: overtime ape

1. Multiple source files in the same directory

CMake has only a single source file. Now write the add function to mymath In the source file of CPP, put the declaration in mymath H source file

The project tree view is as follows:

demo2/
├── CMakeLists.txt
├── main.cpp
├── myMath.cpp
└── myMath.h

At this time, cmakelists Txt can be changed to the following form:

# CMake minimum version number requirements
cmake_minimum_required (VERSION 2.8)

# Project information
project (Demo2)

# Specify build target
add_executable(Demo main.cpp myMath.cpp)

main.cpp

#include <stdio.h>
#include <stdlib.h>
#include "myMath.h"
int main(int argc, char *argv[]) {
    if (argc < 3) {
        printf("Usage: %s argv[1] argv[2] \n", argv[0]);
        return 1;
    }
    int a = atof(argv[1]);
    int b = atoi(argv[2]);
    int result = add(a, b);
    printf("%d + %d = %d\n", a, b, result);
    return 0;
}

myMath.cpp

#include "myMath.h"
int add(int a, int b) {
    return (a + b);
}

myMath.h

int add(int a, int b);

The only change is in add_ A mymath. Is added to the executable command CPP source file. Of course, it's no problem to write this, but if there are many source files, it will be annoying to add the names of all source files. A more convenient way is to use aux_source_directory command, which will find all source files in the specified directory, and then save the results into the specified variable name. The syntax is as follows:

aux_source_directory(<dir> <variable>)

Therefore, you can modify cmakelists Txt as follows:

# CMake minimum version number requirements
cmake_minimum_required (VERSION 2.8)

# Project information
project (demo2)

# Find all source files in the current directory
# And save the name to DIR_SRCS variable
aux_source_directory(. DIR_SRCS)

# Specify build target
add_executable(demo ${DIR_SRCS})

In this way, CMake assigns the file names of all source files in the current directory to the variable DIR_SRCS, and then indicate the variable dir_ The source file in SRCs needs to be compiled into an executable file named demo.

Creating a new build directory is convenient for us to clean up the cache files generated by cmake. You can directly delete the build directory when you don't need it

[root@hackett build]# cmake ..
-- The C compiler identification is GNU 8.4.1
-- The CXX compiler identification is GNU 8.4.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/workspace/cmake/demo2/build
[root@hackett build]# make
[ 33%] Building CXX object CMakeFiles/demo.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/demo.dir/myMath.cpp.o
[100%] Linking CXX executable demo
[100%] Built target demo
[root@hackett build]# ./demo 2 3
2 + 3 is 5

2. Multiple directories, multiple source files

Set mymath H and mymath Move CC file to math directory

../demo3
├── CMakeLists.txt
├── main.cpp
└── math
    ├── myMath.cpp
    └── myMath.h

In this case, you need to write cmakelists in the project root directory demo3 and math directory respectively Txt file. For convenience, we can compile the files in the math directory into a static library, and then call the main function.

Cmakelists. In the root directory txt :

# CMake minimum version number requirements
cmake_minimum_required (VERSION 2.8)

# Project information
project (demo3)

# Find all source files in the current directory
# And save the name to DIR_SRCS variable
aux_source_directory(. DIR_SRCS)

# Add math subdirectory
add_subdirectory(math)

# Specify build target 
add_executable(demo main.cpp)

# Add link library
target_link_libraries(demo MathFunctions)

This file adds the following content: add_subdirectory indicates that the project contains a subdirectory math, so that cmakelists under the math directory Txt files and source code will also be processed. Using the command target_link_libraries indicates that the executable main needs to be connected to a linked library called MathFunctions.

Cmakelists. In the subdirectory txt:

# Find all source files in the current directory
# And save the name to DIR_LIB_SRCS variable
aux_source_directory(. DIR_LIB_SRCS)

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

Use the command add in this file_ Library compiles the source files in the src directory into static link libraries.

[build] - [compile] - [execute]

[root@hackett demo3]# mkdir build
[root@hackett demo3]# cd build/
[root@hackett build]# cmake ..
-- The C compiler identification is GNU 8.4.1
-- The CXX compiler identification is GNU 8.4.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/workspace/cmake/demo3/build
[root@hackett build]# make
[ 25%] Building CXX object math/CMakeFiles/MathFunctions.dir/myMath.cpp.o
[ 50%] Linking CXX static library libMathFunctions.a
[ 50%] Built target MathFunctions
[ 75%] Building CXX object CMakeFiles/demo.dir/main.cpp.o
[100%] Linking CXX executable demo
[100%] Built target demo
[root@hackett build]# ./demo 2 3 
2 + 3 is 5

If you think the article is good, you can give a "three links", and the article is synchronized to the personal WeChat official account.

I'm hackett. I'll see you next time

Reference documents:

CMake introduction practice

CMake Tutorial

Topics: cmake Alibaba Cloud centos7