Multi-file compilation and connection of c/c++ based on Linux and Windows

Keywords: JSON Linux Makefile Windows

    Sometimes writing small programs, but do not want to start 2013, vscode has become my first choice. When I first came into contact with vscode, I was able to compile C/C++ by configuring a lot of things on the internet, but when it comes to many files, I had to open vs2013 obediently. The other day when I was configuring vscode on Linux, I suddenly noticed that the command of netizens in tasks.json was make, and I suddenly got interested. I thought that since I use make, I only need a makefile, and then Ctrl+Shift+B, the problem of compiling and connecting multiple files on vscode could not 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 it was impossible to find a target (forgotten), but I was not satisfied, so Baidu was google again, and searched for almost two hours without finding an effective solution.

    When I looked at my configuration again, the cursor moved to the command, and a prompt appeared: "The command to be executed. Can be an external program or a shell command.". When I see the shell command, I feel like I'm going to vomit blood. I feel like I've wasted two precious hours. It's easy to use the shell script. How simple is it? Look at the picture

tasks.json

    .make.sh

Simply put, the directory of the file ${fileDirName} is passed as a parameter to. make.sh through vscode. Once you enter the directory in the script, just make it again.

    The following tests passed

    

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

Before Ctrl+Shift+B

After Ctrl+Shift+B


debug

So far, the vscode configuration on Linux has been successful. In Windows, it is the same as writing a simple batch processing, but first of all, mingw32, install and configure the gcc/g++ environment. In addition, there is no make.exe under the bin of mingw32, but there is a mingw32-make.exe, which can be changed to make or not change, but the corresponding batch file is written in mingw32-instead of make. Needless to say, paste a Windows configuration map

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

makefile under Linux

.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)


launch.json under Linux

{
    // 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
                }
            ]
        }
    ]
}


makefile under Windows

.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)


launch.json on Windows

{
"version": "0.2.0",
"configurations": [

{
"name": "C++ Launch (GDB)",                 // Configuration name, which will be displayed in the drop-down menu of boot configuration
"type": "cppdbg",                           // Configuration type, cppdbg only
"request": "launch",                        // Request configuration type, which can be launch (start) or attach (attach)
"targetArchitecture": "x86",                // Generate target architecture, generally x86 or x64, for x86, arm, arm64, mips, x64, amd64, x86_64
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",  // The path of the program to be debugged
"miDebuggerPath":"D:/MinGW32/mingw32/bin/gdb.exe", // miDebugger's path, note that this corresponds to MinGw's path
"args": ["blackkitty",  "1221", "# #Command-line parameters passed to the program during debugging are usually set to null.
"stopAtEntry": false,                       // When set to true, the program will pause at the program entry, usually set to false.
"cwd": "${fileDirname}",                  // The working directory for debugging programs is generally ${workspace Root}, which is the directory where the code resides.
"externalConsole": true                   // Whether the console window is displayed during debugging is generally set to true display console
}
]
}


Posted by dennyx on Fri, 21 Dec 2018 23:33:05 -0800