ubuntu上VSCode构建cmake工程

阅读: 评论:0

ubuntu上VSCode构建cmake工程

ubuntu上VSCode构建cmake工程

1.下载Visual Studio Code 编译器

可以去Ubuntu自带的应用商店下载,或者使用命令行下载

2.设置中文显示

直接下载的是英文版本,需要设置成中文显示
先去VS Code自带的商店下载插件,快捷键:Ctrl+Shift+x,搜索Chinese (Simplified) Language Pack for Visual Studio Code,点击下载;
然后配置语言,快捷键:Ctrl+Shift+p,在选框中输入configure display language,点击确定,在出现的locale.json文件中,将"locale":“en” 改为“locale”:“zh-CN”;
重启VSCode;

3.下载构建CMake工程的各种依赖包

和第二步类似去VS Code自带的商店下载插件,快捷键:Ctrl+Shift+x,下载各种依赖包,包括:c/c++,c/c++ clang command adapter,c++ intellisense,CMake和CMake Tools

4.各种json文件配置

打开一个含有的完整的Cmake工程文件夹
在.vscode文件夹中要建立三个json文件才能对Cmake工程进行编译和调试,分别是c_cpp_properties.json,launch.json和tasks.json

4.1 c_cpp_properties.json的配置

ctrl+shift+p→C/Cpp: Edit Configurations(JSON),生成c_cpp_properties.json

{"configurations": [{"name": "Linux","includePath": ["${workspaceFolder}/**"],"defines": [],"compilerPath": "/usr/bin/gcc","cStandard": "c11","cppStandard": "c++17","intelliSenseMode": "clang-x64""compileCommands": "${workspaceFolder}/build/compile_commands.json"}],"version": 4
}

4.2 launch.json配置

按F5,生成launch.json

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: /?linkid=830387"version": "0.2.0","configurations": [{"name": "(gdb) 启动","type": "cppdbg","request": "launch","program": "${workspaceFolder}/build/application",  //输入程序名称,例如 ${workspaceFolder}/a.out"args": ["test1", "test2"],  //输入程序运行参数"stopAtEntry": false,"cwd": "${workspaceFolder}/build",  //设置程序运行初始路径"environment": [],"externalConsole": false,"MIMode": "gdb","setupCommands": [{"description": "为 gdb 启用整齐打印","text": "-enable-pretty-printing","ignoreFailures": true}]}]
}

4.3 tasks.json配置

ctrl+shift+B→生成tasks.json

{// See /?LinkId=733558// for the documentation about the tasks.json format"version": "2.0.0","tasks": [{"label": "make build",//编译的项目名,build"type": "shell","command": "cd ./build ;cmake ../ ;make",//编译命令"group": {"kind": "build","isDefault": true}},{"label": "clean","type": "shell","command": "make clean",}]
}

5. 编写

例如:

cmake_minimum_required(VERSION 2.8)set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -Wall -g -ggdb")
add_compile_options(-std=c++11)set(source ./block.h ./graph.h ./graph.cpp ./maxflow.cpp ./main.cpp)add_executable(maxflow ${source})

6.执行

ctrl+shift+B 编译程序
F5 执行程序

7.调试

在文件中添加

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -Wall -g -ggdb")

即可通过断点进行调试

一个通过cmake组织的工程通过以上步骤可以变成一个可debug的vscode工程

本文发布于:2024-01-30 17:36:40,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170660740421702.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:工程   ubuntu   VSCode   cmake
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23