A concise tutorial on efficiently managing c++ projects using vscode and cmake under linux

  For installation of vscode and c++ environment configuration, please refer to: https://blog.csdn.net/fangshuo_light/article/details/123635576
  First, create a project directory, open the folder in vscode, and create the following folders inside:

  • include: used to store .h files
  • src: used to store .cpp files
  • build: storage path of cmake generated files
  • CMakeLists.txt: cmake configuration file

  The created directory is shown in the figure below:
insert image description here

  Then, we create a file named comp, which declares and defines a function compute that calculates the square of the input data x; then we call this method in main. Then, the directory structure and the contents of each file to realize this function are as follows:

  • Directory Structure:
    insert image description here
  • CMakeLists.txt content:
cmake_minimum_required(VERSION 3.16)                # 说明要求的最低cmake版本,可不需要这行,但是每次cmake后会有warning,所以还是加上
project(MOTION_AVERAGING)                           # 指定工程名,可以自己随便取,不重要
set(SRC_LIST src/main.cpp src/comp.cpp)             # 设置变量SRC_LIST。内容为src/main.cpp src/comp.cpp,用于后面添加src文件
set(CMAKE_BUILD_TYPE Debug)                         # 设置编译类型,可以是Debug,也可以是Relase
include_directories(${CMAKE_SOURCE_DIR}/include)    # 添加头文件搜索路径,${CMAKE_SOURCE_DIR}指代当前cmake文件的路径,这样写后在include自己编写的头文件时就不用再加前缀include了
add_executable(ma ${SRC_LIST})                      # 指定生成的可执行文件名,以及需要编译的所有cpp文件,也就是上面定义的SRC_LIST变量内容,如果添加新的文件,则修改上面变量指代的内容即可
  • comp.h content:
int compute(int x);
  • comp.cpp content:
# include <iostream>
# include "comp.h"
using namespace std;

int compute(int x){
    
    
    return x*x;
}
  • main.cpp content:
# include <iostream>
# include "comp.h"
using namespace std;

int main (int argc, char **argv){
    
    
    int x=2;
    int x2 = compute(x);
    cout << "The square of " << x << " is " << x2 << endl;
    return 0;
}

  At the beginning, there is nothing in the build directory, then we cd to the build directory, and execute the compilation command in the terminal:

$ cmake ..
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xxx/motionaveraging/build

$ make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xxx/motionaveraging/build
Scanning dependencies of target ma
[ 33%] Building CXX object CMakeFiles/ma.dir/src/main.cpp.o
[ 66%] Building CXX object CMakeFiles/ma.dir/src/comp.cpp.o
[100%] Linking CXX executable ma
[100%] Built target ma

  After executing the above two commands, we can see the following content generated in the build directory:
insert image description here
  ma is the name of the generated executable file we specified, and other files do not need to be managed. Then, to get the output, just execute the executable in the terminal:

$ ./ma
The square of 2 is 4

  Seeing this output indicates that our project has been built successfully and everything is running normally.

  Under such management, we will gradually expand and build the project, and it will become very organized. For example, if you want to create a new .h and .cpp file readfile, which is specially used to read file data, you need to add it to the project, just modify CMakeLists.txt to:

cmake_minimum_required(VERSION 3.16)               
project(MOTION_AVERAGING)                        
set(SRC_LIST src/main.cpp src/comp.cpp src/readfile.cpp)            
set(CMAKE_BUILD_TYPE Debug)                                        
include_directories(${CMAKE_SOURCE_DIR}/include)               
add_executable(ma ${SRC_LIST})

  In fact, just add readfile.cpp to the SRC_LIST variable, and then cmake and make. But in this case, you need to manually cmake it every time you modify the code, which is very troublesome. However, we can implement automated cmake by setting the task.json file in vscode. You can refer to: C++ project development using vscode under linux, joint configuration of CMakeLists.txt, launch.json, and tasks.json
needs attention

Guess you like

Origin blog.csdn.net/weixin_44120025/article/details/129228287