ion.sash295
ion.sash295

Reputation: 97

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

Upvotes: 6

Views: 31866

Answers (7)

user16198467
user16198467

Reputation:

Mhmm! It seems you did not follow correct naming scheme while giving names to your file. For example: Hello World.cpp would cause this error. But, If you use Hellow_World.cpp then it perfectly works fine.

Try to use underscrore (_) while joining two words of your file names.

Upvotes: 0

Saqib Masood
Saqib Masood

Reputation: 1

While saving the file name of it should not contain space for eg. Instead of naming Hello world name it Hello_world

Upvotes: 0

Manish Patil
Manish Patil

Reputation: 21

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.

Upvotes: 2

Raj Dave
Raj Dave

Reputation: 11

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

Upvotes: 1

metablaster
metablaster

Reputation: 2184

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)

Upvotes: 3

Jes Wang
Jes Wang

Reputation: 1

Save the file in the folder of helloworld, like projects\helloworld\

Upvotes: -1

parth_07
parth_07

Reputation: 1408

Seems like compiler isn't able to locate source files , Update tasks.json to compile your code with complete path ,

"tasks": [
    {
        "label": "build hello world on WSL",
        "type": "shell",
        "command": "g++",
        "args": [
            "-g",
            "-o",
            "/home/marc/projects/helloworld/helloworld.out",
            "${file}"//Just change this
        ],
        "group": {
            "kind": "build",
            "isDefault": true
        }
    }
]

${file} gives complete path of the file .

Upvotes: 0

Related Questions