vscode+cmake配置普通c++项目

目录

  • 写在前面
  • 代码
  • 命令行编译与运行
  • vscode配置编译与调试
  • 调试
  • 参考

写在前面

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配置编译与调试

vscode 打开cmake_demo目录,configure tasks选择cmake build
vscode+cmake配置普通c++项目_第1张图片
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"
		},
	]
}

vscode+cmake配置普通c++项目_第2张图片

调试

在代码上加断点
vscode+cmake配置普通c++项目_第3张图片
create launch.json
vscode+cmake配置普通c++项目_第4张图片
vscode+cmake配置普通c++项目_第5张图片
vscode+cmake配置普通c++项目_第6张图片
修改program的路径,hello就是编译生成的可执行文件
vscode+cmake配置普通c++项目_第7张图片
F5调试进入断点
vscode+cmake配置普通c++项目_第8张图片

参考

vscode + cmake调试配置 https://da1234cao.blog.csdn.net/article/details/124238534

你可能感兴趣的:(cmake,tools,c++,vscode,c++,ide)