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