本篇內容主要講解“Ubuntu18.04如何配置VSCode+CMake的C++開發環境”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“Ubuntu18.04如何配置VSCode+CMake的C++開發環境”吧!
首先,介紹自己電腦:Ubuntu18.04、VS Code 1.46版
本文目的:為VS Code配置好C++ 開發環境,以及VS Code +CMake的配置
對于C++ 工程,有四個必要的json配置文件,先ctrl+shift+p打開輸入指令分別是:
c_cpp_properties.json :配置項目結構,自動生成和更新,輸入C/C++:Edit configuration
task.json: 構建和編譯運行項目,輸入Task:Configure Task,模板,Others
launch.json: 調試,讀取可執行文件
setting.json: 輸入setting
針對兩種情況分別進行介紹,最后根據十四講中使用Eigen進行實驗。
摘要:
1.新建C/C++工程,VScode以文件夾為管理工程的方式,因此需要建立一個文件夾來保存工程。
2.配置launch.json文件,讀取可執行文件。需要進行修改地方的是指定運行的文件,其次我們還可以在里面添加build任務,用于調試。
3.配置tasks.json文件,這個文件用來方便用戶自定義任務,我們可以通過這個文件來添加g++/gcc或者是make命令,方便我們編譯程序。
4.之后就可以進行基礎的C/C++開發與調試了。
新建一個工作區文件夾,然后在VScode中打開這個文件夾。VScode調試必須在工作區文件夾下,單獨打開一個文件調試會報錯。VScode不支持中文路徑,文件夾名稱不能有空格。
#include <iostream>
using namespace std;
int main(){
cout<<"Hello World"<<endl;
getchar();
return 0;
}
launch.json目的:讀取執行out文件
點擊左側的Debug按鈕,選擇添加配置(Add
configuration),然后選擇C++(GDB/LLDB),然后點擊默認生成,將自動生成launch.json文件,具體操作如下:

{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 啟動",// 配置名稱
"type": "cppdbg",// 配置類型
"request": "launch",// 請求配置類型,launch或者attach
"program": "輸入程序名稱,例如 ${workspaceFolder}/a.out",// 進行調試程序的路徑,程序生成文件.out
"args": [],// 傳遞給程序的命令行參數,一般為空
"stopAtEntry": false,// 調試器是否在目標的入口點停止,
"cwd": "${workspaceFolder}",// 項目目錄
"environment": [],
"externalConsole": false,// 調試時是否顯示控制臺窗口,一般為true顯示控制臺
"MIMode": "gdb",// 指定連接的調試器
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}更改:
將program內容改為調試時運行的程序。
"program": "輸入程序名稱,例如 ${workspaceFolder}/a.out"改為
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out"新增,preLaunchTask 使得每次調試之前會自動進行build:
"preLaunchTask": "build",
最終版本為:
{
// 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",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}task.json:定義編譯方法,轉為計算機可識別的語言,生成out文件。
快捷鍵ctrl+shift+p打開命令行,輸入:
Task:Configure Task使用模版創建Tasks.json文件 →
Others:

{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",// 任務名
"type": "shell",
"command": "echo Hello" // 指令
}
]
}更改為:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": ["-g", "${file}", "-std=c++11", "-o", "${fileBasenameNoExtension}.out"]
}
]
}以上工作完成后即可編譯運行C/C++程序。不過在調試之前最好先CTRL+SHIFT+B編譯一下,選擇執行我們的build任務,build成功后,點擊開始調試。

在文件夾內創建文件
~$ touch main.cpp ~$ touch CMakeLists.txt
CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
# 工程vscode_cmake
project(vscode_cmake)
#dubug 模式
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
set(SRC_LIST main.cpp)
# 可執行程序 result
add_executable(result ${SRC_LIST})main.cpp
#include<iostream>
using namespace std;
int main(){
int a = 2+3;
int b = a+3;
for(int i = 0; i<10; i++){
cout<<"hello vs code & cmake..."<<endl;
}
return 0;
}其中, 需要在CMakeLists.txt 里加set (CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -g”)
開啟debug 不然斷點調試是無效的
首先要build生成可執行文件result,有了可執行文件才能進行debug操作,然后再設置斷點,按下F5,進行調試。
在圖中最左側第四個小蜘蛛形狀的圖標(調試),點擊左上方的小齒輪,添加配置(C++GDB/LLDB),修改launch.json文件為:
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 啟動",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/result",// 更改
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}更改了
"program": "${workspaceFolder}/build/result",// 更改是為了生成的可執行文件result到build文件夾內。
之后按下最下方的Build按鍵,生成可執行文件。
接下來設置斷點,按下F5,進行調試

Ctrl+shift+p打開命令選項,選擇C/C++:Edit configuration ,自動生成 c_cpp_properties.json配置文件。

{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**"
],
"defines": [],
"compilerPath": "/usr/bin/clang",
"cStandard": "c11",
"cppStandard": "c++14",
"intelliSenseMode": "clang-x64",
"configurationProvider": "ms-vscode.cmake-tools"
}
],
"version": 4
}最主要的事includePath的引用和庫的路徑,根據引用內容進行配置。
打開《視覺SLAM十四講》的ch4的useGeometry文件夾
CmakeLists.txt:
cmake_minimum_required( VERSION 2.8 ) project( geometry ) # 添加Eigen頭文件 include_directories( "/usr/include/eigen3" ) add_executable( eigenGeometry eigenGeometry.cpp )
eigenGeometry.cpp:
#include <iostream>
#include <cmath>
using namespace std;
#include <Eigen/Core>
// Eigen 幾何模塊
#include <Eigen/Geometry>
/****************************
* 本程序演示了 Eigen 幾何模塊的使用方法
****************************/
int main ( int argc, char** argv )
{
// Eigen/Geometry 模塊提供了各種旋轉和平移的表示
// 3D 旋轉矩陣直接使用 Matrix3d 或 Matrix3f
Eigen::Matrix3d rotation_matrix = Eigen::Matrix3d::Identity();
// 旋轉向量使用 AngleAxis, 它底層不直接是Matrix,但運算可以當作矩陣(因為重載了運算符)
Eigen::AngleAxisd rotation_vector ( M_PI/4, Eigen::Vector3d ( 0,0,1 ) ); //沿 Z 軸旋轉 45 度
cout .precision(3);
cout<<"rotation matrix =\n"<<rotation_vector.matrix() <<endl; //用matrix()轉換成矩陣
// 也可以直接賦值
rotation_matrix = rotation_vector.toRotationMatrix();
// 用 AngleAxis 可以進行坐標變換
Eigen::Vector3d v ( 1,0,0 );
Eigen::Vector3d v_rotated = rotation_vector * v;
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;
// 或者用旋轉矩陣
v_rotated = rotation_matrix * v;
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;
// 歐拉角: 可以將旋轉矩陣直接轉換成歐拉角
Eigen::Vector3d euler_angles = rotation_matrix.eulerAngles ( 2,1,0 ); // ZYX順序,即roll pitch yaw順序
cout<<"yaw pitch roll = "<<euler_angles.transpose()<<endl;
// 歐氏變換矩陣使用 Eigen::Isometry
Eigen::Isometry3d T=Eigen::Isometry3d::Identity(); // 雖然稱為3d,實質上是4*4的矩陣
T.rotate ( rotation_vector ); // 按照rotation_vector進行旋轉
T.pretranslate ( Eigen::Vector3d ( 1,3,4 ) ); // 把平移向量設成(1,3,4)
cout << "Transform matrix = \n" << T.matrix() <<endl;
// 用變換矩陣進行坐標變換
Eigen::Vector3d v_transformed = T*v; // 相當于R*v+t
cout<<"v tranformed = "<<v_transformed.transpose()<<endl;
// 對于仿射和射影變換,使用 Eigen::Affine3d 和 Eigen::Projective3d 即可,略
// 四元數
// 可以直接把AngleAxis賦值給四元數,反之亦然
Eigen::Quaterniond q = Eigen::Quaterniond ( rotation_vector );
cout<<"quaternion = \n"<<q.coeffs() <<endl; // 請注意coeffs的順序是(x,y,z,w),w為實部,前三者為虛部
// 也可以把旋轉矩陣賦給它
q = Eigen::Quaterniond ( rotation_matrix );
cout<<"quaternion = \n"<<q.coeffs() <<endl;
// 使用四元數旋轉一個向量,使用重載的乘法即可
v_rotated = q*v; // 注意數學上是qvq^{-1}
cout<<"(1,0,0) after rotation = "<<v_rotated.transpose()<<endl;
return 0;
}launch.json配置為:
{
// 使用 IntelliSense 了解相關屬性。
// 懸停以查看現有屬性的描述。
// 欲了解更多信息,請訪問: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) 啟動",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/eigenGeometry",// 更改
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "為 gdb 啟用整齊打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}task.json配置為:
{
// See https://go.microsoft.com/fwlink/?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",
}
]
}c_cpp_properties.json
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**", // 更改
"/usr/include",
"/usr/local/include"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "clang-x64",
"compileCommands": "${workspaceFolder}/build/compile_commands.json"// 更改
}
],
"version": 4
}按下build生成可執行文件eigenGeometry

生成可執行文件后,按下F5,進行調試

到此,相信大家對“Ubuntu18.04如何配置VSCode+CMake的C++開發環境”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。