3

I am new to c++ and vscode, so I'm not sure how to fix this issue. I've tried to go through the tutorial at https://code.visualstudio.com/docs/cpp/config-clang-mac but I can't seem to paste their "hello world" code without the iostream, vector, and string errors. I've also seen posts on here indicating that I should change the includePath string array in configurations, but adding the path to my project folder doesn't seem to get rid of these errors. I'm at a loss here since everything I've seen so far still isn't working.

The code which causes these errors has been copy pasted directly from the tutorial above:

#include <iostream>
#include <vector>
#include <string>

using namespace std;

int main()
{
    vector<string> msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};
    
    for (const string& word : msg)
    {
        cout << word << " ";
    }
    cout << endl;
}

In the first three lines, I'm getting the errors "cannot open source file {the included import's name}". The compiler states that #include errors detected. Please update your includePath. Squiggles are disabled for this translation unit (/Users/rjc/projects/helloworld/helloworld.cpp).C/C++(1696)

I added this line in my includePath as such (the configurations page states add one includePath per line, so I put these two statements on separate lines): ${workspaceFolder}/** /Users/rjc/projects/helloworld/

Yet this did not reduce the number of errors. I'm not really sure what to do here, since the tutorial does not seem to be working for me.

I'm running macOS Big Sur (11.1) with clang version 12.0.0. Would be grateful for any intuition as to why I'm having these issues.

rjc810
  • 315
  • 1
  • 2
  • 13
  • 1
    Try replacing `` with `"iostream"`... There's a difference between `` and `"header_file"` Here's a lil something you will find useful: https://stackoverflow.com/questions/21593/what-is-the-difference-between-include-filename-and-include-filename – Novus Edge Dec 23 '20 at 08:37
  • Did you try to compile it directly? Invoke clang++ with your file as argument, if it works then it's VSCode issue, not a compiler issue. – Kaldrr Dec 23 '20 at 08:39
  • I think the intuition is that `includePath` is for 'intellisense' only, it does not affect the compiler. Now why your compiler cannot find the header files is another issue. As Kalldr says the thing you should try is to use your compiler outside of VSCode to find out if it's a compiler issue or a VSCode issue. – john Dec 23 '20 at 08:44
  • @NovusEdge I replaced all three but the compiler is still returning the same errors. It keeps telling me to update my includePath – rjc810 Dec 23 '20 at 08:45
  • 1
    @rjc810 That advice is incorrect (except maybe as a diagnostic tool). `#include ` is correct code. – john Dec 23 '20 at 08:45
  • @john sorry I'm quite new to this, how should I try to compile it outside of vs code? should I just run clang++ helloworld.cpp inside terminal? – rjc810 Dec 23 '20 at 08:47
  • @rjc810 That would be a start, try that and report any error. – john Dec 23 '20 at 08:48
  • The compiler is returning helloworld.cpp:9:23: error: expected ';' at end of declaration vector msg {"Hello", "C++", "World", "from", "VS Code", "an... ^ ; helloworld.cpp:11:29: warning: range-based for loop is a C++11 extension [-Wc++11-extensions] for (const string& word : msg) Not sure where the first error is coming from, since this line (in full) is ``` vector msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"}; ``` – rjc810 Dec 23 '20 at 08:49
  • @rjc810 Well that implies that from the command line the compiler can find the header files (since the errors you have are after that). So it seems to be a VSCode problem. I'm sorry but I don't know anything about VSCode on a Mac. – john Dec 23 '20 at 08:53
  • This page helped me to configure it properly using the CLang compiler https://code.visualstudio.com/docs/cpp/config-clang-mac – darul75 Dec 30 '21 at 14:49

4 Answers4

12

Your includePath only have ${workspaceFolder}. You need to add the path to your system directory. Run this command and make sure all of the paths printed out are listed in your c_cpp_properties.json: gcc -v -E -x c++ -

Allen ZHU
  • 511
  • 3
  • 11
  • Open your terminal app and run the command above. – Allen ZHU Dec 23 '20 at 08:49
  • I copy pasted all the paths into the includePath array in properties.json, but I'm still getting the same errors. I have "${workspaceFolder}/**", "/usr/local/include", "/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1", "/Library/Developer/CommandLineTools/usr/lib/clang/12.0.0/include", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include", "/Library/Developer/CommandLineTools/usr/include", "/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks" – rjc810 Dec 23 '20 at 08:54
  • update: deleting the workspaceFolder string seems to satisfy the compiler. I'm still getting an error from this line though. vector msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"}; Compiler is telling me that a semicolon is expected at the end of the declaration, but my array has a semicolon after it. Am I missing something trivial here? Thank you! – rjc810 Dec 23 '20 at 08:59
  • 1
    Try to install the 'Include Autocomplete' extension in vscode. – Allen ZHU Dec 23 '20 at 09:00
  • installed the extension. not sure why the code is giving me errors, since i copy pasted it directly off of the vscode tutorial. is it maybe a problem with my version of vscode? – rjc810 Dec 23 '20 at 09:03
  • Oh, I'm very sorry. Did you install the 'C/C++' extension in vscode? – Allen ZHU Dec 23 '20 at 09:14
  • No worries! I installed it- but I'm still getting errors on this one line of code: `vector msg {"Hello", "C++", "World", "from", "VS Code", "and the C++ extension!"};` – rjc810 Dec 23 '20 at 15:29
  • Faced the same problem Allen, and this method worked. Could you kindly elaborate on what this command actually does. Newbie here. – Abhinav Mar 30 '21 at 06:11
0

Make sure the compiler in the compilerPath setting exists and that it supports C++.

I saw these stdlib include errors on a (centos) system that had gcc installed without C++ cpp support. Installing the missing packaged fixed the errors in vscode. Failing to compile a hello.cpp from the terminal made it clear my root problem was not in vscode.

plswork04
  • 448
  • 5
  • 10
0

You only need to change 3 times in the c_cpp_properties.json file.

  1. "name": "Mac"

  2. add "usr/include/linux" to the "includePath"

  3. "intelliSenseMode": "clang-x64"

This solution is for Linux users only.

Damini Suthar
  • 1,386
  • 2
  • 9
  • 34
-1

I had the same problem and I was trying anything to get it work

then I realized that when I named the file I only named it helloworld instead of helloworld.cpp behind since this wasn't needed in the other IDEs I've been using.

Dharman
  • 26,923
  • 21
  • 73
  • 125