請問vscode可以執行c++多文件編譯嗎?如何實現?
霧色:VS code tasks.json和launch.json的設置?zhuanlan.zhihu.com
裡面講了c++的編譯配置
寫makefile
是可以的,但是,你需要設置tasks.json與launch.json。
在開始之前,你需要熟悉vscode的內建變數,以便提供對上面兩個文件的修改。
具體可以參看下面這個官方文檔鏈接,我不在此具體介紹:
https://code.visualstudio.com/docs/editor/variables-reference?code.visualstudio.com首先,我在D盤新建了一個名為Multifiles Compile Instance的文件夾,裡面放了我寫的三個源代碼文件(如下圖,分別是port.h、port.cpp和testing.cpp),這是《C++ Primer Plus》第十三章課後編程習題的第4題(13.11.4)
這裡我把這三個文件的內容貼出來:
port.h:
#ifndef PORT_H_
#define PORT_H_
#include&
using namespace std;
class Port{
char *brand;
char style[20];
int bottles;
public:
Port(const char *br = "none", const char *st = "none", int b = 0);
Port(const Port p);
virtual ~Port() { delete[] brand; }
Port operator=(const Port p);
Port operator+=(int b);
Port operator-=(int b);
int BottleCount() const { return bottles; }
virtual void show() const;
friend ostream operator&
port.cpp:
#include&
#include&
using namespace std;
#include"port.h"
//Port methods
Port::Port(const char *br,const char *st,int b)
{
brand = new char[strlen(br) + 1];
strcpy(brand, br);
strncpy(style, st, 20);
bottles = b;
}
Port::Port(const Portp)
{
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strncpy(style, p.style, 20);
bottles = p.bottles;
}
Port Port::operator=(const Portp)
{
if(this==p)
return *this;
delete[] brand;
brand = new char[strlen(p.brand) + 1];
strcpy(brand, p.brand);
strncpy(style, p.style, 20);
bottles = p.bottles;
return *this;
}
Port Port::operator+=(int b)
{
bottles += b;
return *this;
}
Port Port::operator-=(int b)
{
if(bottles&<=b)
bottles = 0;
else
bottles -= b;
return *this;
}
void Port::show()const
{
cout &
testing.cpp
下面配置.vscode文件夾下的tasks.json與launch.json 我用的是minGW64,因此編譯參數會用到gdb與g++(如果你用了LLVM-clang的話應該是gdb與clang++) 配置tasks.json文件:可以按照我在裡面加的注釋具體操作 配置launch.json文件:同樣地,可以按照我在裡面加的注釋具體操作 按照上面的配置,我在main函數加一斷點如下圖,選擇菜單的Debug-Start Debugging,就可以愉快地調試多文件項目啦~#include &
#include"port.h"
using namespace std;
int main(void)
{
Port a("gua1", "ho", 200),b("gua2","ho",50);
cout &
{
"version": "2.0.0",
"tasks": [
{
"label": "Compile", // 這是項目執行名稱,必須與launch.json的preLaunchTask相同
"command": "G:/mingw64/bin/g++", // 指定編譯器,我用的mingw64編譯,安裝目錄我放在G盤,你需要根據自己的情況更改
"args":["${workspaceFolder}\*.cpp", "-o", "${workspaceFolder}\${workspaceFolderBasename}.exe","-g"], //這裡的*.cpp是關鍵
// args是編譯指令的參數,注意"-o"後面的那個參數,指定了輸出可執行文件的存放位置,這個參數必須與launch.json的program相同
"type": "shell", // process和shell,我用了shell,具體參見VSCode官方文檔
"options": {
"cwd": "G:/mingw64/bin"//指定編譯器工作目錄,和minGW配置的環境變數相同
},
"problemMatcher":["$gcc"], // (可選)用於捕捉編譯時終端的報錯信息
"group": {
"kind": "build",
"isDefault": true
},
"presentation": {
"echo": true,
"reveal": "always", // 執行任務時跳轉到終端
"focus": false, // C/C++必須設為false
"panel": "shared" // 編譯信息只使用一個終端面板
}
}
]
}{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++ GO!", // 配置啟動名稱(我隨便取了一個)
"type": "cppdbg",
"request": "launch", // 請求配置類型,可以為launch(啟動)或attach(附加)
"program": "${workspaceFolder}\${workspaceFolderBasename}.exe", // 必須和tasks.json里的args "-o"參數後面的參數一致
"args": [], // main函數沒有使用參數的話,為空即可
"stopAtEntry": false, // 程序是否暫停在入口,不要,手動打斷點吧
"cwd": "${workspaceFolder}", // 源文件的工作目錄
"environment": [], // 環境變數,空著即可
"externalConsole": true, // 是否使用外部控制台
"internalConsoleOptions": "neverOpen", // 是否輸入gdb命令
"MIMode": "gdb", // 指定連接的調試器,minGW用gdb吧
"miDebuggerPath": "G:/mingw64/bin/gdb.exe", // 調試器存放位置
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
],
"preLaunchTask": "Compile" // 必須和tasks.json的label相同
}
]
}
使用Step Into看看:
誒,就進入port.cpp去執行Port類的構造函數啦~
到此,在vscode里,對C++多文件編譯的目的達成了,只要配置妥當,就可以媲美Visual Studio。
希望這篇回答對大家有幫助哦~
VSCode寫C/C++,個人覺得不完全合適。既然是要編譯多個文件了,應該用IDE。當然我更喜歡IDE+VSCode一起用,VSCode寫CMakeLists.txt和shell/bat腳本,有高亮提示;cmake生成的工程文件,Win下用Visual Studio打開,Linux/Mac下用CLion打開,挺好用的。
Make、CMake、MSBuild 都可以,想用哪個用哪個。
當然可以。最好的方案就是CMake+cquery,配上CMake-tools以及vscode-cquery插件,幾乎就可以擁有補全、高亮、編譯等等幾乎是一個IDE了。
謝邀
可以去學習一下makefile
或者簡單點寫個腳本
用循環或者多個編譯命令
g++ ***.cpp -o ***
然後。。我猜你可能想做IDE的事情。。建議去搜搜vscode的c++插件,應該有類似功能不過我沒有用過。大工程一般直接上vs studio了。
xmake或cmake,vscode都有對應的插件
vsc不能進行c++編譯。c++編譯器才能進行編譯。所以這個問題和vsc沒有關係。
推薦閱讀: