1、本文内容
vscode+cmake配置普通c++项目
2、平台
ubuntu vscode
3、转载请注明出处:
https://blog.csdn.net/qq_41102371/article/details/125890166
mkdir cmake_demo
将hello.cpp与CMakeLists.txt放在cmake_demo文件夹下
hello.cpp
#include
int main(){
std::cout<<"hello"<<std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(Cmake_Hello)
add_executable(hello hello.cpp)
这里使用Release
cd cmake_demo
cmake -DCMAKE_BUILD_TYPE=Release -S ./ -B ./build
cmake --build ./build --config Release --parallel 4
./build/hello
vscode 打开cmake_demo目录,configure tasks选择cmake build
1、生成task.json
{
"version": "2.0.0",
"tasks": [
{
"type": "cmake",
"label": "CMake: build",
"command": "build",
"targets": [
"all"
],
"group": "build",
"problemMatcher": [],
"detail": "CMake template build task"
}
]
}
ctrl+shift+B可以编译,只不过默认是Debug
2、修改type为shell,可加入上述命令行编译命令以配置CMAKE_BUILD_TYPE等,命令使用分号隔开
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "CMake: build",
"command": "cmake -DCMAKE_BUILD_TYPE=Debug -S ./ -B ./build;cmake --build ./build --config Debug --parallel 4",
"group": "build",
"problemMatcher": [],
"detail": "CMake template build task"
}
]
}
3、上述2也可以分开写
label自己定义,下面的dependsOn对应上就行,ctrl+shift+B之后选择后一个c0mpile_project就行,因为有dependsOn,build_project会一起执行
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "build_project",
"command": "cmake -DCMAKE_BUILD_TYPE=Debug -S ./ -B ./build",
"group": "build",
"problemMatcher": [],
"detail": "CMake template build task"
},
{
"type": "shell",
"label": "compile_project",
"command": "cmake --build ./build --config Debug --parallel 4",
"dependsOn":["build_project"],
"group": "build",
"problemMatcher": [],
"detail": "CMake template build task"
},
]
}
在代码上加断点
create launch.json
修改program的路径,hello就是编译生成的可执行文件
F5调试进入断点
vscode + cmake调试配置 https://da1234cao.blog.csdn.net/article/details/124238534