Vscode 在一个文件夹里编译多个文件

文章目录

一、tasks.json 配置

二、settings.json 配置

三、示例


一、tasks.json 配置

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc.exe 生成活动文件",
            //注意这里填你自己 mingw-w64 下的 gcc.exe 目录
            "command": "E:\\software\\mingw-w64\\mingw64\\bin\\gcc.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                //"${file}",
                "${fileDirname}\\*.c",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

        可以看到,我把原本的 "${file}" 注释掉了,换成  "${fileDirname}\\*.c",这是在同一个文件夹中支持编译多个 c 文件,如果要编译多个 cpp 文件,则改成  "${fileDirname}\\*.cpp" 即可。

Vscode 在一个文件夹里编译多个文件_第1张图片

二、settings.json 配置

        这个怎么找到呢?按下 ctrl+shift+p 或者 F1,打开控制面板,输入 settings.json,点击即可。

 settings.json:

{
    "editor.fontSize": 17,
    "workbench.iconTheme": "vscode-icons",
    "workbench.colorTheme": "Noctis Viola",
    "editor.minimap.enabled": true,
    "C_Cpp.autocomplete": "Default",
    "[cpp]": {
        "editor.quickSuggestions": true
    },
    "[c]": {
        "editor.quickSuggestions": true
    },
    "files.autoSave": "afterDelay",
    "code-runner.executorMap": {
        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        //"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "c": "cd $dir && gcc *.c -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        //"cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "python -u",
        "perl": "perl",
        "perl6": "perl6",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "scheme": "csi -script",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runhaskell",
        "nim": "nim compile --verbosity:0 --hints:off --run",
        "lisp": "sbcl --script",
        "kit": "kitc --run",
        "v": "v run",
        "sass": "sass --style expanded",
        "scss": "scss --style expanded",
        "less": "cd $dir && lessc $fileName $fileNameWithoutExt.css",
        "FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt"
    },
    "terminal.integrated.defaultProfile.windows": "PowerShell"
}

        注意看我注释部分,如果你想编译多个 c 文件,把 "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" 改成 "c": "cd $dir && gcc *.c -o $fileNameWithoutExt && $dir$fileNameWithoutExt" 即可,如果你想编译多个 cpp 文件,把 "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt" 改成 "cpp": "cd $dir && g++ *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt" 即可。

三、示例

        我的 c-project 文件夹里包含 main.c、test.c 和 test.h 文件。 

        test.h:

#ifndef __TEST_H_
#define __TEST_H_

#include 

void PrintfHelloWorld();

#endif

        test.c:

#include "test.h"

void PrintfHelloWorld()
{
	printf("Hello World!\n");
}

         main.c:

#include "test.h"

int main(void)
{
    PrintfHelloWorld();
    return 0;
}

        输出结果如下,正确输出 Hello World! 

Vscode 在一个文件夹里编译多个文件_第2张图片

         当然,main.exe 也正常生成了。

Vscode 在一个文件夹里编译多个文件_第3张图片

         OK,完成。

你可能感兴趣的:(#,Vscode的使用,Vscode)