作者:tongqingliu
轉(zhuǎn)載請注明出處:http://www.cnblogs.com/liutongqing/p/7069091.html
Ubuntu下安裝并配置VS Code編譯C++
網(wǎng)上看了很多教程,寫的都不細(xì)致,或者我理解不夠透徹,一步一步操作下來,總是錯誤百出。好不容易成功一次,現(xiàn)將完整過程記錄如下
安裝VS Code
sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-makesudo apt-get updatesudo apt-get install ubuntu-makesudo umake web visual-studio-code
然后按a直接默認(rèn)同意就可以。
安裝插件
打開VS Code后,按crtl + shift + P調(diào)出命令行,然后搜索C++,安裝微軟自己開發(fā)的那個。
同樣可以安裝C++ Intellisense插件,用于自動補全代碼。
配置launch.json和tasks.json
注意VS Code只能打開源碼所在的文件夾,而不是直接打開源碼文件,否則下面將無法進行!
打開源碼所在文件夾后,在該文件夾中打開源碼。按F5鍵,選擇C++,
然后會自動生成launch.json文件,下面只需要修改兩個地方
將
"program": "enter program name, for example \${workspaceRoot}/a.out",
改為
"program": "${workspaceRoot}/a.out",
將
"cwd": "\${workspaceRoot}",
改為
"cwd": "${workspaceRoot}",
完整的launch.json
{ "version": "0.2.0", "configurations": [ { "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", "program": "${workspaceRoot}/a.out", "args": [], "stopAtEntry": false, "cwd": "${workspaceRoot}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
然后,調(diào)出命令行,輸入Task Runner,選擇others
此時將自動生成tasks.json
將其中的
"command": "echo",
改為
"command": "g++",
將
"args": ["Hello World"],
改為
"args": ["-g","${workspaceRoot}/main.cpp"],
注意這里的main.cpp要和你當(dāng)前路徑的源碼名稱一致。
完整的tasks.json
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "0.1.0", "command": "g++", "isShellCommand": true, "args": ["-g","${workspaceRoot}/main.cpp"], "showOutput": "always"}
運行測試
隨便編寫個代碼
#include<iostream>using namespace std;int main(){ cout<<"hello VS Code"<<endl; return 0;}
按crtl + shift + B構(gòu)建,按F5運行,發(fā)現(xiàn)終端一閃而過,什么都沒有輸出。于是考慮Windows下的辦法。
#include<iostream>#include<stdlib.h>using namespace std;int main(){ cout<<"hello VS Code"<<endl; system("pause"); return 0; }
同樣并沒有卵用。那就換一種方式。
#include<iostream>#include<stdio.h>using namespace std;int main(){ cout<<"hello VS Code"<<endl; getchar(); return 0;}
按crtl + shift + B構(gòu)建,按F5運行,程序完美輸出。有圖為證,哈哈
后記:
期間在終端里執(zhí)行了以下操作
sudo apt-get install clang
如果提示Clang有錯可以運行該命令,安裝clang。
那么問題來了,是不是換個文件夾每次寫個代碼都得配置lauch.json和task.json文件呢?或者將.vscode文件夾復(fù)制到當(dāng)前文件夾下?這樣豈不是很麻煩,細(xì)思極恐
參考:
作者:tongqingliu
轉(zhuǎn)載請注明出處:http://www.cnblogs.com/liutongqing/
http://www.cnblogs.com/liutongqing/p/7069091.html