Mr72
Mr72

Reputation: 111

Visual Studio Code-Cannot open source file "iostream"

I just want to try c++ coding with Visual Studio code. I have installed vscode 1.18.1 to my laptop (Win10-64).

I got errors by typing following code:

#include <iostream>
using namespace std;
int main()
{
    std::cout << "Hello world!" <<endl;
    return 0;
}

Should happen no Error. C:\Users\Harri\OneDrive\Tiedostot\Demo2.vscode\c_cpp_properties.json -content:

"path": [ "/usr/include", "/usr/local/include", "${workspaceRoot}" ],

Problems/Errors for row 1:

" #include errors detected. Please update your includePath. IntelliSense features for this translation unit (C:\Users\Harri\OneDrive\Tiedostot\Demo2\Calc.cpp) will be provided by the Tag Parser. "

" cannot open source file "iostream" "

Upvotes: 11

Views: 33496

Answers (2)

Sven Arno Jopen
Sven Arno Jopen

Reputation: 34

I had the same problem, but it was easily solved by setting the "Compiler path" in the C/C++ configurations. VS Code then found the library directly and compiled the project.

Screenshot from settings screen in VS Code

Upvotes: 1

Scott McPeak
Scott McPeak

Reputation: 12864

The main problem is cygwin paths

You have cygwin paths like /usr/include in your c_cpp_properties.json file. That is a problem because VSCode does not understand cygwin paths. At a cygwin shell you can run:

  $ cygpath -w /usr/include
  D:\cygwin64\usr\include

to get the equivalent Windows path. Put that into c_cpp_properties.json instead. Remember that you have to double the backslashes when you copy this into a JSON string.

Other suggestions

This SO answer describes how to set up VSCode with cygwin gcc. I haven't tried those instructions but they look reasonable.

Beyond that, I highly recommend going through the Get Started with C++ tutorial on the VSCode site. It might directly answer your question, but even if it doesn't, having a working setup to compare to is valuable.

Also, look at the C/C++ diagnostics: View → Command Palette... → C/C++: Log Diagnostics. This will show things like which compiler VSCode is trying to emulate and what it thinks the #include paths are.

Finally, to get lots of useful information directly from your compiler to compare with what VSCode thinks, if you are using gcc, run at a cygwin or bash prompt:

  $ touch empty.c
  $ gcc -v -E -dD empty.c > compiler-info.txt

That will write to compiler-info.txt all the predefined macros, #include search paths, default target, etc.

Upvotes: 0

Related Questions