c++ - 如何将 g++ 从 MinGW 添加到 PATH

标签 c++ g++ visual-studio-code mingw

我在 Windows 10 上使用 Visual Studio Code,并尝试使用 MinGW 的 g++ 构建一个程序(在源文件 Bus.cpp 中称为 Bus)。我使用从之前的线程中读取的各种方法修改了tasks.json 文件。我提供了tasks.json 文件的屏幕截图以及构建后的结果(ctrl + shift + b)。我还在下面提出了我的问题。

Method 1

Method 2

Method 3

Method 4

问题1)我希望能够使用方法1构建和创建一个名为“bus”的程序。但是,我必须将“g++”替换为MinGW的g++编译器的目录路径(如方法2所示) 。我需要做什么才能只输入“g++”而不是 MinGW 的 g++.exe 的路径?

问题 2) 看起来它是使用方法 2 和 4 进行编译的,方法 2 和 4 提供了 MinGW 的 g++ 编译器的目录路径,但我没有看到名为“bus”的构建程序,即使我在命令中使用了“-obus”。为什么我看不到它?

编辑以显示代码:

第一种方法tasks.json文件:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++ -g bus.cpp -o bus",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

第一种方法结果输出:

> Executing task: g++ -g bus.cpp -o bus <

g++ : The term 'g++' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ g++ -g bus.cpp -o bus
+ ~~~
    + CategoryInfo          : ObjectNotFound: (g++:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

第二种方法tasks.json文件:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "C:/MinGW/bin/g++.exe -g bus.cpp -o bus",
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

第二种方法的结果输出:

> Executing task: C:/MinGW/bin/g++.exe -g bus.cpp -o bus <

The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

Third Method tasks.json file:
{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g", "bus.cpp", "-o", "bus"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

第三种方法的结果输出:

    > Executing task: g++ -g bus.cpp -o bus <

g++ : The term 'g++' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ g++ -g bus.cpp -o bus
+ ~~~
    + CategoryInfo          : ObjectNotFound: (g++:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

第四种方法tasks.json文件:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "C:/MinGW/bin/g++",
            "args": [
                "-g", "bus.cpp", "-o", "bus"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

第四种方法结果输出:

    > Executing task: C:/MinGW/bin/g++ -g bus.cpp <

The terminal process terminated with exit code: 1

Terminal will be reused by tasks, press any key to close it.

最佳答案

总而言之,您需要将 mingW-gcc 添加到您的路径中。

最简单的方法:

开始菜单 -> 计算机 -> 右键单击​​ -> 属性 -> 高级系统设置(左侧) -> 环境变量

然后在末尾添加分号后将您的目录添加到 Path 变量中。

关于c++ - 如何将 g++ 从 MinGW 添加到 PATH,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48612744/

相关文章:

c++ - 没有参数的构造函数不起作用,但有参数的构造函数起作用

C++ ELF 目标文件符号表有两次列出的函数

linux - 在 Ubuntu 上为 mpi.h 设置 G++ 或 ICC

java - g++、ld 和 JNI - 链接问题

typescript - 如何修复 Typescript linting 在 VSCode 中不起作用的问题?

c++ - glBufferData 因尺寸过大而无声地失败

c++ - 析构函数 : virtual or not?

c++ - pthread_cond_timedwait 与 Solaris 10 上的 clock_gettime 链接错误

angular - 如何在 VS Code 上设置 AngularDart5

c++ - 在 VSCode Insiders 中运行 C++ Jupyter Notebook