我本身整理了一份:javascript
cygwin64/home/xxx/.bash_profile ,末尾加上以下代碼(後面vscodeMake.bat要用到環境變量"_T"):java
export _T=${_T//\\//} # replace backslash to fowardslash if [[ $_T == "" ]];then export _T="c:/" fi cd "$_T"
vscodeMake.batshell
:changedir @echo off set _T=%1 set _makefile="Makefile" set "a=%2" if "%_T%"=="" ( set _T=%cd% ) if "%a%"=="" ( echo %a% is not assigned ) else ( set _makefile=%a% ) bash --login -i -c "make -f %_makefile%"
launch.jsonjson
{ "name": "makeCustome", "preLaunchTask": "makecustome", "type": "cppdbg", "request": "launch", "program": "${fileDirname}/${fileBasenameNoExtension}.exe", "args": [], // 程序調試時傳遞給程序的命令行參數,通常設爲空便可 "stopAtEntry": false, "cwd": "${workspaceFolder}", "environment": [], "externalConsole": true, "MIMode": "gdb", "miDebuggerPath": "gdb.exe", "setupCommands": [ { "description": "Enable pretty-printing for gdb", "text": "-enable-pretty-printing", "ignoreFailures": true } ] }
tasks.json:小程序
{ "label": "makecustome", "command": "${workspaceFolder}/vscodeMake.bat", "args": [ "\"${fileDirname}/\"", "make${fileBasenameNoExtension}.txt", ], "type": "shell", "group": { "kind": "build", "isDefault": true }, "presentation": { "echo": true, "reveal": "always", // 在「終端」中顯示編譯信息的策略,能夠爲always,silent,never "focus": false, "panel": "shared" // 不一樣的文件的編譯信息共享一個終端面板 }, },
maketest.txt:windows
.SUFFIXES:.c .o CC=gcc SRCS=test.c\ xxx.c OBJS=$(SRCS:.c=.o) EXEC=test build:$(OBJS) $(CC) -o $(EXEC) $(OBJS) @echo '---------------OK---------------' .c.o: $(CC) -Wall -g -o $@ -c $< clean: rm -f $(OBJS) rm -f $(EXEC)
或更簡單的(-g 表明具備調試信息,不然沒法斷點):bash
testcapstone: testcapstone.o ${CC} $< -o3 -Wall capstone.lib -o $@ %.o: %.c ${CC} -c $< -g -o $@ -I ../src/capstone/include/
test.c文件:架構
#include <stdio.h> int g_var = 0; void print_line(char *str) { if (str != NULL) printf("%s\r \n", str); else printf("null string\r\n"); } int main (int argc, char **argv) { int l_var = 1; print_line(" error hello world!"); printf("g_var = %d, l_var = %d.\r\n", g_var, l_var); return 0; }
//下面的是轉載別人的,能夠不看////////////////////////////////////////////////////////////////////////////測試
有時寫寫小程序,又不想啓動2013,vscode就成了個人首選。剛接觸vscode的時候,跟着網上配置了一堆東西,總算能編譯C/C++了,但一涉及到多文件,我還得乖乖的打開vs2013。前些天在配置Linux上的vscode的時候,忽然發現有網友在tasks.json中的command是make,忽然來了興致,想到既然用make,那我只要一個makefile,而後Ctrl+Shift+B,在vscode上多文件編譯鏈接這個問題不就能解決了嗎。因而動手開始按着那位網友的配置寫好了tasks.json。但最終make命令執行失敗,說是找不到target什麼的(忘了),但我不甘心,因而又是百度又是google,搜索了差很少兩個小時都沒有找到有效的解決方法。ui
當再次仔細看個人配置的時候,光標移到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一下就行了。
如下測試經過
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
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的配置圖
如下代碼是對的,先經過bash從dos切換到cygwin
:changedir @echo off ::set tmppath=%1 ::set _T=%1 ::在cygwin的 /home/yourID/.bash_profile 末尾加上以下代碼 ::export _T=${_T//\\//} # replace backslash to fowardslash ::if [[ $_T == "" ]];then ::export _T="c:/" ::fi ::cd "$_T" ::set _T=%cd% set _T=%1 set _makefile="Makefile" set "a=%2" if "%_T%"=="" ( set _T=%cd% ) if "%a%"=="" ( echo %a% is not assigned ) else ( set _makefile=%a% ) ::%tmppath:~0,2% ::cd %tmppath% ::如下bash命令是爲了從Dos環境進入cygwin環境 ::bash --login -i bash --login -i -c "make -f %_makefile%" ::運行編譯腳本MakeFile ::make
最後順便貼一下個人makefile和launch.json吧
Linux下makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Linux下launch.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
|
Windows下makefile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Windows下launch.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|