vscode基於Linux和Windows下c/c++的多文件編譯與鏈接

我本身整理了一份: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

fe5136e9a44664f158830732927b1db8.png-wh_

    .make.sh

d284830aa348c9dbed7e21e53bcaa015.png-wh_

簡單吧,就是經過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

//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前

d368a97c89b24edfbe3c93a2f8cb1a6f.png-wh_

Ctrl+Shift+B後

43309c45db4bdbdc34376471be223aa2.png-wh_

debug

3d57969dbc0bcfa34d34a2e7b6a7f19e.png-wh_

至此,Linux上的vscode配置大功告成啦,在Windows是其實同理寫一個簡單的批處理就好,不過首先要mingw32,安裝而且配置好gcc/g++環境,另外,mingw32的bin下沒有make.exe,但有一個mingw32-make.exe,將它改爲make就好,不改也行,不過相應的批處理文件裏就寫mingw32-make而不是make,很少說,貼上windows的配置圖

98a701c9be7ae0fadf7dc60d26d313d2.png-wh_

6f72fde3022d39d0eda39dd2c25cf505.png-wh_

如下代碼是對的,先經過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

.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

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

{

    // 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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

.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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

{

"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顯示控制檯

}

]

}

相關文章
相關標籤/搜索