ubuntu中使用VsCode+Eigen创建Eiegn应用程序
Visual studio code是微軟發(fā)布的一個(gè)運(yùn)行于 Mac OS X、Windows和 Linux 之上的,針對(duì)于編寫現(xiàn)代 Web 和云應(yīng)用的跨平臺(tái)源代碼編輯器。
1. VsCode安裝
VScode官網(wǎng)下載.deb文件,網(wǎng)址鏈接如下:https://code.visualstudio.com/#alt-downloads
執(zhí)行下面命令安裝VsCode: sudo dpkg -i code_1.48.2-1598353430_amd64.deb
在應(yīng)用程序中搜索vscode可以看到已經(jīng)安裝成功。
2. Vscode環(huán)境配置
2.1 安裝c/c++插件
通過左側(cè)的Extensions欄目安裝C/C++插件,這里我已經(jīng)安裝。
2.2 建立工程
1. 由于VScode是以文件夾的形式管理工程的,因此我們首先新建一個(gè)文件夾,我這里取名叫eigen_VsCode,表示創(chuàng)建基于Vs_Code創(chuàng)建一個(gè)Eigen應(yīng)用程序,文件夾內(nèi)容是空的。
2. 使用VsCode打開文件夾eigen_VsCode,點(diǎn)擊左側(cè)的Explorer,然后點(diǎn)擊Open Folder。
3. 在eigen_VsCode目錄下新建.cpp文件,這里使用已經(jīng)存在的eigenMatrix.cpp文件,如下所示:
2.3 更改配置文件(launch.json)
1. 點(diǎn)擊左側(cè)的Run, 點(diǎn)擊創(chuàng)建一個(gè)launch.json文件,C++(GDB/LLDB),將自動(dòng)生成launch.json文件
2. 生成的默認(rèn)launch.json文件如下。
3. 將默認(rèn)launch.json修改成為如下,launch.json中各個(gè)字段的含義,后面學(xué)習(xí)中會(huì)逐個(gè)講解。
{
? ? // 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) 啟動(dòng)",
? ? ? ? ? ? "type": "cppdbg",
? ? ? ? ? ? "request": "launch",
? ? ? ? ? ? "program": "${workspaceFolder}/${fileBasenameNoExtension}",
? ? ? ? ? ? "args": [],
? ? ? ? ? ? "stopAtEntry": false,
? ? ? ? ? ? "cwd": "${workspaceFolder}",
? ? ? ? ? ? "environment": [],
? ? ? ? ? ? "externalConsole": false,
? ? ? ? ? ? "MIMode": "gdb",
? ? ? ? ? ? "preLaunchTask": "build",
? ? ? ? ? ? "setupCommands": [
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? "description": "為 gdb 啟用整齊打印",
? ? ? ? ? ? ? ? ? ? "text": "-enable-pretty-printing",
? ? ? ? ? ? ? ? ? ? "ignoreFailures": true
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ]
? ? ? ? }
? ? ]
}
2.4 添加構(gòu)建(編譯、鏈接等)任務(wù)(tasks.json)
為了方便在VScode里編譯C++代碼,我們可以將類似g++ -g main.cpp等g++命令寫入VScode的任務(wù)系統(tǒng)。首先,利用快捷鍵ctrl+shift+p打開命令行,輸入Tasks: Run task,會(huì)出現(xiàn)如下提示:
1. 選擇No Configured tasks. Configure Tasks...
2. 選擇Create task.json file from template
3. 選擇Others
4. 生成的默認(rèn)tasks.json文件如下
這里的label為任務(wù)名,我們將”label"= "echo"改為”label"= "build"。由于我們的指令是g++,這里將”command“=”echo Hello“改為”command“=”g++“。然后添加g++的參數(shù)args。如果我們的g++指令為:g++ -g main.cpp,這里可以把參數(shù)設(shè)置為如下
如果我們想配置g++指令為:g++ -g main.cpp -std=c++11 -o main.out,則參數(shù)可設(shè)置為:
{
? ? // 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}"]
? ? ? ? }
? ? ]
}
2.5?添加配置庫文件支持
使用快捷鍵ctrl+shift+p調(diào)出命令行,選擇執(zhí)行我們的build任務(wù),但是發(fā)現(xiàn)build出現(xiàn)錯(cuò)誤,提示:
?fatal error: Eigen/Core: 沒有那個(gè)文件或目錄
?#include <Eigen/Core>
? ? ? ? ? ^~~~~~~~~~~~
compilation terminated.
由于代碼中使用了eigen庫文件,需要配置包含庫文件路徑。ctrl+shift+p調(diào)出命令行,選擇C/C++: Edit Configurations(UI)
點(diǎn)擊c_cpp_properties.json打開配置文件。
配置如下:
我們只需要再紅框的 IncludePath 內(nèi)加上需要的頭文件路徑即可,
這里提示下,常用庫的頭文件常見安裝位置如下:
- /usr/include/
- /usr/local/include
所以這兩個(gè)基本要加上的,如果你不知道你安裝的庫的頭文件在哪,但是知道關(guān)鍵的頭文件名稱,則可以用?locate?命令來查找:
locate?ros.h| grep include
這個(gè)命令的意思是查找所有ros.h的位置,并且找出路徑中帶有 include 字段的路徑。最終的c_cpp_properties.json配置如下:
{
? ? "configurations": [
? ? ? ? {
? ? ? ? ? ? "name": "Linux",
? ? ? ? ? ? "includePath": [
? ? ? ? ? ? ? ? "${workspaceFolder}/**",
? ? ? ? ? ? ? ? "/usr/include/",
? ? ? ? ? ? ? ? "/usr/local/include/",
? ? ? ? ? ? ? ? "/usr/include/eigen3/"
? ? ? ? ? ? ],
? ? ? ? ? ? "defines": [],
? ? ? ? ? ? "compilerPath": "/usr/bin/gcc",
? ? ? ? ? ? "cStandard": "c11",
? ? ? ? ? ? "cppStandard": "c++11",
? ? ? ? ? ? "intelliSenseMode": "clang-x64"
? ? ? ? }
? ? ],
? ? "version": 4
}
?
2.6 編譯
使用快捷鍵ctrl+shift+p調(diào)出命令行,選擇執(zhí)行我們的build任務(wù),生成eigenMatrix可執(zhí)行文件。
?
2.7?調(diào)式運(yùn)行
首先點(diǎn)擊產(chǎn)生斷點(diǎn),然后啟動(dòng)調(diào)式。
總結(jié)
以上是生活随笔為你收集整理的ubuntu中使用VsCode+Eigen创建Eiegn应用程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SLAM-ch2-cmake中使用库
- 下一篇: ubuntu+VsCode+Cmake+