Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "g++: error: helloworld.cpp: No such file or directory" in visual Studio Code with WSL installed?

I installed Visual Studio Code on W10 to run some code with WSL installed ( Ubuntu).

I followed the steps in the following article :

https://code.visualstudio.com/docs/cpp/config-wsl

But I keep receiving the following error message when trying to compile the code in visual Studio Code :

"g++: error: helloworld.cpp: No such file or directory"

There are the configuration for the 3 .json file:

c_cpp_properties.json

{
"configurations": [
    {
        "name": "Win32",
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE"
        ],
        "compilerPath": "/usr/bin/g++",
        "cStandard": "c11",
        "cppStandard": "c++17",
        "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": "/home/marc/projects/helloworld/helloworld.out",
            "args": ["-fThreading"],
            "stopAtEntry": true,
            "cwd": "/home/marc/projects/helloworld/",
            "environment": [],
            "externalConsole": true,
            "windows": {
                "MIMode": "gdb",
                "miDebuggerPath": "/usr/bin/gdb",
                "setupCommands": [
                    {
                        "description": "Enable pretty-printing for gdb",
                        "text": "-enable-pretty-printing",
                        "ignoreFailures": true
                    }
                ]
            },
            "pipeTransport": {
                "pipeCwd": "",
                "pipeProgram": "c:\\windows\\sysnative\\bash.exe",
                "pipeArgs": ["-c"],
                "debuggerPath": "/usr/bin/gdb"
            },
            "sourceFileMap": {
                "/mnt/c": "${env:systemdrive}/",
                "/usr": "C:\\Users\\Marc\\AppData\\Local\\Packages\\CanonicalGroupLimited.Ubuntu18.04onWindows_79rhkp1fndgsc\\LocalState\\rootfs\\usr\\"
            }
        }
    ]
}

tasks.json

{
"version": "2.0.0",
"windows": {
    "options": {
        "shell": {
            "executable": "c:\\windows\\sysnative\\bash.exe",
            "args": ["-c"]
        }
    }
},
"tasks": [
    {
        "label": "build hello world on WSL",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g",
            "-o",
            "/home/marc/projects/helloworld/helloworld.out",
            "helloworld.cpp"
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

}

My path projects on WSL Ubuntu is :

/home/marc/projects/helloworld/

This folder is empty as Visual Studio Code is supposed to run through WSL in the folder C:\Users\Marc\projects\helloworld.vscode that currently contains:

c_cpp_properties.json

helloworld.cpp

launch.json

tasks.json

How can this be fixed?

Thank you

like image 319
qwerty1805 Avatar asked May 24 '19 10:05

qwerty1805


People also ask

How do I add G ++ code to Visual Studio?

Make sure you have a C++ compiler installed before attempting to run and debug helloworld.cpp in VS Code. Open helloworld.cpp so that it is the active file. Press the play button in the top right corner of the editor. Choose C/C++: g++.exe build and debug active file from the list of detected compilers on your system.

Why my C++ program is not running in VS code?

Make sure your compiler executable is in your platform path ( %PATH on Windows, $PATH on Linux and macOS) so that the C/C++ extension can find it. You can check availability of your C++ tools by opening the Integrated Terminal (Ctrl+`) in VS Code and trying to directly run the compiler.


3 Answers

If anyone have this problem, I've setup gcc with VS Code after reading official tutorial here

I got the same problem, and the solution is to move cpp and header files into project folder (1 folder up), that is outside the ".vscode" folder.

dir structure should looke like this:

-> project dir (project root folder)

-> -> .vscode

-> -> -> json files are here (inside the .vscode)

-> -> helloworld.cpp (project files are here inside project dir)

like image 126
metablaster Avatar answered Oct 12 '22 06:10

metablaster


The same exact problem occurred to me. I came here for the solution but I didn't find one. I tried everything I could do but at last, I made a simple change which solved the problem. I named my code "Quick Sort.cpp" before the error. Now I changed it to "QuickSort.cpp", without giving any spaces in between while naming it. Now it is running successfully. This may or may not be the reason for your error. But make sure you did not do this mistake.

like image 2
Manish Patil Avatar answered Oct 12 '22 05:10

Manish Patil


Just don't save your file with any whitespace character i.e. single space/tab ....

Save your file without Space (file name without space) and use c++ extension

like image 1
Raj Dave Avatar answered Oct 12 '22 06:10

Raj Dave