c++ - My C++ program won't compile on Visual Studio code
问题描述
I'm attempting to use Visual Studio Code for the first time and my C++ won't compile.
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.
I've included the file
helloworld.cpp:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World";
}
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:
{
"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:
{
"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: 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
解决方案
似乎编译器无法找到源文件,更新 tasks.json 以编译具有完整路径的程序,
{
"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}
几乎是不言自明的。
推荐阅读
- javascript - 更新订单项目总计中的增量数量?
- android - Crashlytics 不报告 Firebase 中的崩溃
- typescript - 打字稿中的剂量[索引:字符串]是什么意思
- python - 如何配置 python mysql.connector 以转换没有字节数组 b'' 的长文本字段
- javascript - 如何在 React Navigation 中更新 initialParams?
- javascript - Safari 中的 JavaScript 问题低于 14
- php - 是否可以在 Laravel 中执行 orderBy() 或 orderBy()?
- razor-pages - 使用 asp-page TagHelper 进行表单元素操作呈现“”而不是正确的 URL
- http - 使用 ArduinoHTTPClient 通过 ESP32 发送 GET 请求时出现问题
- mysql - MySQL JSON 查询错误“字符串中缺少右引号”