My C++ program won't compile on Visual Studio code

廉价感情. 提交于 2021-02-04 08:40:31

问题


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



回答1:


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
            }
        }
    ]
}

here ${file} gives complete path of the file with extension(.cpp) , ${workspaceFolder} and ${fileBasenameNoExtension} are pretty much self-explanatory too .




回答2:


 That eventually worked after I exited the shell and re-opened it

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


来源:https://stackoverflow.com/questions/57019588/my-c-program-wont-compile-on-visual-studio-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!