Ubuntu+VsCode++搭建C++开发环境
Ubuntu下使用VsCode搭建C++开发环境1、根本工具的安装
起首Ubuntu下安装好C++开发的一个些根本工具g++、gdb、make、cmake等,安装方式点这里
查抄一下安装环境
$ g++ --version
g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gdb --version
GNU gdb (Ubuntu 12.1-0ubuntu1~22.04.2) 12.1
Copyright (C) 2022 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ make --version
GNU Make 4.3
Built for x86_64-pc-linux-gnu
Copyright (C) 1988-2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
$ cmake --version
cmake version 3.22.1
CMake suite maintained and supported by Kitware (kitware.com/cmake).
2、VsCode环境搭建
打开vscode(未安装则自行安装vscode),点击extension,搜索C++,安装c/c++插件
https://i-blog.csdnimg.cn/direct/f3a747be1ac049d890e3e97e1cce35ad.png
安装完成后,测试一下,创建一个新目次cpp_proj_test,用vscode打开,创建一个main.cpp文件,内容如下:
// main.cpp
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!!!!!" << endl;
return 0;
}
https://i-blog.csdnimg.cn/direct/650218dde5434445b185ae0322fe686c.png
生存后,按ctrl+F5 不调试直接实行步伐
https://i-blog.csdnimg.cn/direct/c660d376b7d548f59e59734fdc49e6eb.png
这里选择上面第二个c/c++这个编译器,点击后如下
https://i-blog.csdnimg.cn/direct/9ccbfc46ce2a4d45bcb21061c2bd89d2.png
正常编译,查看一下下面TERMINAL终端面板,乐成编译并实行了
https://i-blog.csdnimg.cn/direct/d1789af7c9d44dd29b0714d0d986fc01.png
这时,我们的cpp_proj_test项目目次下,多了一个编译结果文件main,以及vscode工程项目的专用配置文件.vscode/tasks.json ,如下所示
https://i-blog.csdnimg.cn/direct/da388d8b17774de0a28b070490c5478a.png
tasks.json的内容如下
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.out"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
3、vscode编译调试环境配置
vscode中的重要的配置文件重要有三个:c_cpp_properties.json,launch.json,tasks.json
这个几个文件的关系是什么呢?起首c_cpp_properties.json文件负责配置编译器最根本的参数,如编译器标准及版本、一些依赖include的位置等;tasks.json则是默认的一次编译任务,使用些什么编译命令及编译参数实行编译任务;而launch.json则是默认的一次启动,的一些环境变量、启动参数等;
launch.json是启动目的文件的配置,该文件中,通常可以定义启动目的文件之前,可以做一些处理,如:每次启动前,先编译一遍;
实际上,可以如许区分:launch.json管实行编译结果的一些默认配置、而task.json管实行编译的默认配置,而c_cpp_properties.json则管编译相干的一些最根本的配置
1)c_cpp_properties.json配置
该文件是编译器的配置文件,配置包含:gcc/g++路径、include头文件路径、C++标准等。
按下ctrl+shift+P ,输入c/c++:Edit Configurations ,出现如下,选择下面第二个,自动创建一个配置文件
https://i-blog.csdnimg.cn/direct/3e99cfb0dc8f48b9b4adb81a5edcc73f.png
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
2)launch.json 配置
该文件是debug调试C/C++步伐(实行out文件)的配置文件,配置包含:debug类型等;tasks.json 文件告诉vscode如何编译cpp步伐。这会调用 g++ 编译器将源文件编译成可实行文件。为了方便VScode编译C++代码,可以将include头文件、lib动态链接库等路径写入 tasks.json配置文件里。
点击左侧的运行与调试 ,出现下面的面板
https://i-blog.csdnimg.cn/direct/7f6ee67b997140c1b6236db7d4f4611a.png
点击create a launch.json file 创建一个启动配置文件 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": []
}
将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": "(gdb) Launch",
/* 配置类型,cppdbg类型 */
"type": "cppdbg",
/* 请求配置类型,可以为launch(启动)或attach(附加) */
"request": "launch",
/* 将要进行调试的程序的路径 */
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
/* 程序调试时传递给程序的命令行参数,一般设为空即可 */
"args": [],
/* 设为true时程序将暂停在程序入口处,一般设置为false */
"stopAtEntry": false,
/* 调试程序时的工作目录 */
"cwd": "${workspaceFolder}",
"environment": [],
/* 调试时是否显示控制台窗口,一般设置为true显示控制台 */
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
生存配置,进入main.cpp页面,再次按下ctrl+F5,弹出如下界面,提示未找到任务build
https://i-blog.csdnimg.cn/direct/9e02e79530fd41ec99b247564200e52d.png
我们点击Debug Aanyway后,出现这个调试窗口,则表现乐成;
https://i-blog.csdnimg.cn/direct/72fb96ce2d0549cdb788594701851cba.png
如需要消除上面的的task任务缺失弹框,可进一步配置Tasks.json,
3)tasks.json 配置
该文件是编译C/C++步伐(生成out文件)的配置文件,配置包含:include头文件路径、lib链接库路径等
按键输入ctrl+shift+P,输入搜索Tasks: Run Task如下,
https://i-blog.csdnimg.cn/direct/6930d1fa70ee4753a55fe48aa535555f.png
点击Tasks: Run Task,进入后,鼠标移动到c/c++: g++ build activate file后面的齿轮配置按钮
https://i-blog.csdnimg.cn/direct/094eb1e0e0104d92823c38e3c03e92d2.png
点击齿轮按钮Configure Task,打开Tasks.json配置文件,如下所示
https://i-blog.csdnimg.cn/direct/a52088ba5cee4a159dcbcd92fb988baf.png
前面launch.json中的"preLaunchTask": "build",属性,预启动的任务设置为build,则,我们需要修改tasks.json的label同样也为build即可;
修改后如下:
{
"tasks": [
{
"type": "cppbuild",
"label": "build",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g","${file}",
"-o","${fileDirname}/${fileBasenameNoExtension}",
/* 项目所需的头文件路径 */
"-I","${workspaceFolder}/include",
"-I", "/usr/include",
"-I", "/usr/local/include",
/* 项目所需的库文件路径 */
"-L", "/usr/local/lib",
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
再次按下ctrl+F5,则不会再提示了
https://i-blog.csdnimg.cn/direct/193167a87b3947cda9df136453732abd.png
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]