vscode基於Linux和Windows下c/c++的多文件編譯與連接
有時寫寫小程序,又不想啟動2013,vscode就成了我的首選。剛接觸vscode的時候,跟著網上配置了一堆東西,總算能編譯C/C++了,但一涉及到多文件,我還得乖乖的打開vs2013。前些天在配置Linux上的vscode的時候,突然發現有網友在tasks.json中的command是make,突然來了興致,想到既然用make,那我只要一個makefile,然後Ctrl+Shift+B,在vscode上多文件編譯連接這個問題不就能解決了嗎。於是動手開始按著那位網友的配置寫好了tasks.json。但最終make命令執行失敗,說是找不到target什麽的(忘了),但我不甘心,於是又是百度又是google,搜索了差不多兩個小時都沒有找到有效的解決方法。
當再次仔細看我的配置的時候,光標移到command上的時候,出現了一個提示“The command to be executed. Can be an external program or a shell command.”。看到shell命令也可以的時候感覺要吐血了,感覺我浪費了寶貴的兩個小時,明明用shell腳本就變得很簡單了,有多簡單?看圖
tasks.json
.make.sh
簡單吧,就是通過vscode將文件的目錄${fileDirName}作為參數傳給.make.sh,在腳本裏進入這個目錄後,再make一下就好了。
以下測試通過
//test.h #ifndef _MULTI_FILE_TEST_ #define _MULTI_FILE_TEST_ #include <stdio.h> void print(); #endif //tesh.c #include "test.h" void print() { printf("hello world!\n"); } //main.c #include "test.h" int main() { print(); return 0; }
Ctrl+Shift+B前
Ctrl+Shift+B後
debug
至此,Linux上的vscode配置大功告成啦,在Windows是其實同理寫一個簡單的批處理就好,不過首先要mingw32,安裝並且配置好gcc/g++環境,另外,mingw32的bin下沒有make.exe,但有一個mingw32-make.exe,將它改成make就好,不改也行,不過相應的批處理文件裏就寫mingw32-make而不是make,不多說,貼上windows的配置圖
最後順便貼一下我的makefile和launch.json吧
Linux下makefile
.SUFFIXES:.c .o CC=gcc SRCS=main.c test.c OBJS=$(SRCS:.c=.o) EXEC=main build:$(OBJS) $(CC) -o $(EXEC) $(OBJS) @echo ‘---------------OK---------------‘ .c.o: $(CC) -Wall -g -o $@ -c $< clean: rm -f $(OBJS) rm -f $(EXEC)
Linux下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", "type": "cppdbg", "request": "launch", "program": "${workspaceFolder}/${fileBasenameNoExtension}", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] } ] }
Windows下makefile
.SUFFIXES:.c .o CC=gcc SRCS=main.c test.c OBJS=$(SRCS:.c=.o) EXEC=main.exe build:$(OBJS) $(CC) -o $(EXEC) $(OBJS) @echo ‘---------------OK---------------‘ .c.o: $(CC) -Wall -g -o $@ -c $< clean: del $(OBJS) del $(EXEC)
Windows下launch.json
{ "version": "0.2.0", "configurations": [ { "name": "C++ Launch (GDB)", // 配置名稱,將會在啟動配置的下拉菜單中顯示 "type": "cppdbg", // 配置類型,這裏只能為cppdbg "request": "launch", // 請求配置類型,可以為launch(啟動)或attach(附加) "targetArchitecture": "x86", // 生成目標架構,一般為x86或x64,可以為x86, arm, arm64, mips, x64, amd64, x86_64 "program": "${fileDirname}/${fileBasenameNoExtension}.exe", // 將要進行調試的程序的路徑 "miDebuggerPath":"D:/MinGW32/mingw32/bin/gdb.exe", // miDebugger的路徑,註意這裏要與MinGw的路徑對應 "args": ["blackkitty", "1221", "# #"], // 程序調試時傳遞給程序的命令行參數,一般設為空即可 "stopAtEntry": false, // 設為true時程序將暫停在程序入口處,一般設置為false "cwd": "${fileDirname}", // 調試程序時的工作目錄,一般為${workspaceRoot}即代碼所在目錄 "externalConsole": true // 調試時是否顯示控制臺窗口,一般設置為true顯示控制臺 } ] }
本文出自 “11662416” 博客,請務必保留此出處http://11672416.blog.51cto.com/11662416/1981689
vscode基於Linux和Windows下c/c++的多文件編譯與連接