11

I am new to C++ and Visual Studio Code, so I'm not sure how to fix this issue. I've tried to go through the tutorial in Using Clang in Visual Studio Code, 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 11.1 (Big Sur) with Clang version 12.0.0. Why do I have these issues?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rjc810
  • 425
  • 1
  • 3
  • 17
  • 2
    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

8 Answers8

19

Your includePath only has ${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 file: gcc -v -E -x c++ -

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Allen ZHU
  • 690
  • 5
  • 14
  • Open your terminal app and run the command above. – Allen ZHU Dec 23 '20 at 08:49
  • 1
    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
  • What if I don't have a c_cpp_properties.json? I can build and run but the sguigglies remain. – lcj Apr 06 '23 at 12:21
1

as told above you should

  • run this command in the terminal: gcc -v -E -x c++ -
  • then it shows you all the paths to include as shown in the image

enter image description here

  • now open search for this file c_cpp_properties.json in vs code (by tapping cntrl + p in vscode)
  • now open c_cpp_properties.json file and paste all the file paths I've marked (which may be more or less in your case) as shown in image

enter image description here

  • and then you are ready to go!
  • HAPPY CODING!
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++ support. Installing the missing packaged fixed the errors in Visual Studio Code. Failing to compile a hello.cpp from the terminal made it clear my root problem was not in Visual Studio Code.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
plswork04
  • 589
  • 6
  • 11
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,470
  • 2
  • 14
  • 43
0

On Ubuntu I had different gcc/g++ versions installed.

Check your c++ version

c++ -v

Check directory /usr/include/c++ and if it is missing the corresponding version (10/ or 11/ or 12/), install it sudo apt install libstdc++-XX. I had to install libstdc++-12

sudo apt install libstdc++-12
brunorcabral
  • 131
  • 1
  • 5
0

I am working with mingw g++ compiler. I got this error too while I was runnning my program. I just changed the compiler path. It worked in my case.

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 10 '23 at 10:38
0

FWIW, I had a similar issue this week when starting a new cpp project - it turns out that when I had switched from Windows to Linux, midway through kicking off the project, I forgot to update all my paths in the tasks.json and c_cpp_properties.json files (inside the .vscode directory), which were still using the Windows path standards (\ instead of /). Once I fixed this, all was well. Incidentally, I made my tasks.json an array of tasks and my c_cpp_properties.json an array of configurations as well, allowing me to add both windows compiler options as well as linux compiler options separately - on build I select which one to use.

Talisman
  • 1
  • 1
-2

I had the same problem, and I was trying anything to get it to 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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131