我的C ++程序无法在Visual Studio代码上编译 [英] My C++ program won't compile on Visual Studio code
问题描述
我第一次尝试使用Visual Studio Code,但我的C ++无法编译.
I'm attempting to use Visual Studio Code for the first time and my C++ won't compile.
我已经从MSYS2将mingw的bin和bash.exe添加到了我的PATH中.我所有的代码都位于同一目录中,直接来自Microsoft指南 https://code. visualstudio.com/docs/cpp/config-mingw (我确实更改了我的路径).我所有的文件也都在同一个目录中.
I have already added mingw's bin and bash.exe from MSYS2 to my PATH. All of my code is in the same directory and straight from microsoft's guide https://code.visualstudio.com/docs/cpp/config-mingw (I did change the paths to mine). All of my files are also in the same directory.
我已包含文件
helloworld.cpp:
helloworld.cpp:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World";
}
task.json:
tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"-g",
"-o",
"helloworld",
"helloworld.cpp"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
c_cpp_properties.json:
c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"defines": [
"_DEBUG",
"UNICODE"
],
"compilerPath": "C:\\Mingw-w64\\mingw32\\bin\\g++.exe",
"intelliSenseMode": "gcc-x64",
"browse": {
"path": [
"${workspaceFolder}"
],
"limitSymbolsToIncludedHeaders": true,
"databaseFilename": ""
}
}
],
"version": 4
}
launch.json:
launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/helloworld.exe",
"args": [],
"stopAtEntry": true,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\Mingw-w64\\mingw32\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
该文件无法生成,并且我不断出现相同的错误消息:
The file wouldn't build and I am continuously the same error message:
g ++.exe:错误:helloworld.cpp:没有这样的文件或目录g ++.exe: 致命错误:没有输入文件编译终止.终点站 进程终止,退出代码:1
g++.exe: error: helloworld.cpp: No such file or directory g++.exe: fatal error: no input files compilation terminated. The terminal process terminated with exit code: 1
推荐答案
似乎编译器无法找到源文件,更新task.json以编译具有完整路径的程序,
Seems like compiler isn't able to locate the source files , update the tasks.json to compile programs with complete path ,
{
"version": "2.0.0",
"tasks": [
{
"label": "build hello world",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",//Just
"-o",//edit
"${workspaceFolder}\\${fileBasenameNoExtension}"//these
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
此处${file}
给出了具有扩展名(.cpp)的文件的完整路径,${workspaceFolder}
和${fileBasenameNoExtension}
也几乎是不言自明的.
here ${file}
gives complete path of the file with extension(.cpp) , ${workspaceFolder}
and ${fileBasenameNoExtension}
are pretty much self-explanatory too .
这篇关于我的C ++程序无法在Visual Studio代码上编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!