vscode is based on multi-file compilation and linking of c/c++ under Linux and Windows

Sometimes I write small programs, but I don't want to start 2013, and vscode has become my first choice. When I first came into contact with vscode, I configured a bunch of things online, and finally I could compile C/C++, but when multiple files were involved, I had to open vs2013 obediently. When I was configuring vscode on Linux a few days ago, I suddenly found that the command of some netizens in tasks.json was make, and I suddenly became interested. I thought that since make is used, I only need a makefile, and then Ctrl+Shift+B, in Can't the problem of multi-file compilation and connection on vscode be solved? So I started to write tasks.json according to the configuration of the netizen. But in the end, the make command failed to execute, saying that the target could not be found (forgot), but I was not reconciled, so Baidu and google searched for almost two hours without finding an effective solution.

    When I looked at my configuration carefully again, when the cursor moved to the command, a prompt " The command to be executed. Can be an external program or a shell command. " appeared. I felt like vomiting blood when I saw that shell commands were also available. I felt like I wasted two precious hours. Obviously, it became very simple to use shell scripts. How simple? look at the picture

tasks.json

fe5136e9a44664f158830732927b1db8.png-wh_

    .make.sh

d284830aa348c9dbed7e21e53bcaa015.png-wh_

Simple, just pass the file directory ${fileDirName} as a parameter to .make.sh through vscode. After entering this directory in the script, make it again.

    The following tests pass

    

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_

At this point, the vscode configuration on Linux is complete. In Windows, it is good to write a simple batch process in the same way, but first, you need to install and configure the gcc/g++ environment for mingw32. In addition, there is no make.exe under the bin of mingw32. But there is a mingw32-make.exe, just change it to make, or not, but write mingw32-make instead of make in the corresponding batch file, not much to say, paste the configuration diagram of windows

98a701c9be7ae0fadf7dc60d26d313d2.png-wh_

6f72fde3022d39d0eda39dd2c25cf505.png-wh_

Finally, post my makefile and launch.json by the way

Linux under 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

                }

            ]

        }

    ]

}

 

Under 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显示控制台

}

]

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324999424&siteId=291194637