1 CMakeLists.txt编写
cmake_minimum_required(VERSION 3.12)project(djl_plm)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -g")add_executable(simple simple.cpp)
add_executable(main main.cpp)include_directories(include)
相当于如下gcc命令:
g++ simple.cpp -std=c++17 -Iinclude -o simple -g
2 Cmake构建方法
根目录下创建build目录
mkdir build
进入build目录
cd build
执行cmake构建命令
cmake ..
执行make编译命令
make
注意:一旦代码被修改了,就需要重新构建
3 VScode 调试方法
task.json配置
{ "version": "2.0.0","options": {"cwd": "${workspaceFolder}/build"},"tasks": [{"type": "shell","label": "cmake","command": "cmake","args": [".."]},{"label": "make","group": {"kind": "build","isDefault": true},"command": "make","args": []},{"label": "Build","dependsOrder": "sequence", // 按列出的顺序执行任务依赖项"dependsOn":["cmake","make"]}]}
launch.json配置
{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "C/C++: g++ build and debug active file","type": "cppdbg","request": "launch","program": "${workspaceFolder}/build/main", //main:被调试的程序文件"args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "/usr/bin/gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "Build"}]
}
优点:按下F5即可调试,并且每次修改代码后不再需要手动cmake编译构建。
项目目录结构如下: