Vscode-cpptools: My vscode cannot recognize "cout" "endl" etc... (MinGW)

Created on 23 May 2017  路  21Comments  路  Source: microsoft/vscode-cpptools

_From @LiuZHolmes on May 23, 2017 3:11_

  • VSCode Version: Code 1.12.2 (19222cdc84ce72202478ba1cec5cb557b71163de, 2017-05-10T13:20:36.315Z)
  • OS Version: Windows_NT ia32 10.0.14393
  • Extensions:

|Extension|Author|Version|
|---|---|---|
|code-gnu-global|austin|0.2.2|
|code-runner|formulahendry|0.6.17|
|vscode-clang|mitaki28|0.2.2|
|cpptools|ms-vscode|0.11.1|
|debug|webfreak|0.19.0|
|c-cpp-snippets|wlhe|0.1.0|;

I already set my cpp_properties correctly(I think),like:

        {
            "name": "Win32",
            "includePath": [ "C:/MinGW/include/*", 
            "C:/MinGW/lib/gcc/mingw32/5.3.0/include/*" ,
            "C:/MinGW/lib/gcc/mingw32/5.3.0/include/c++/*", 
            "C:/MinGW/lib/gcc/mingw32/5.3.0/include/c++/mingw32", 
            "C:/MinGW/lib/gcc/mingw32/5.3.0/include/c++/backward" ],
            "defines": [
                "_DEBUG",
                "UNICODE"
            ],
            "browse": {
                "path": [],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        }

And with the simple code:

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "Hello world!" << endl;
    int a;
    cin >> a;
    return 0;
}

there are errors like:

'identifier "cout" is undefined'
'argument list for variable template "endl" is missing'
'identifier "cin" is undefined'


Steps to Reproduce:

1.Install vscode and cpp extensions

2.Set your cpp_properties like me(you may need to change the directory of mingw)

_Copied from original issue: Microsoft/vscode#27110_

Language Service fixed (release pending)

Most helpful comment

The fix for this has been released. Please install version 0.12.0 and let us know if you have any trouble with it. I also wrote up a page recommending a default MinGW configuration that may be of interest to you.

All 21 comments

What's more, if you change your intellisense engine to "Tag Parser", the error also changes to that it can't recognize header iostream. By the way, even in the situation like this I can still build and run my cpp file.

If you are using the "Tag Parser" setting, you need to make sure that the browse.path setting is populated. In your c_cpp_properties.json that you shared, it looks like it is empty. You should add

"C:/MinGW/include", 
"C:/MinGW/lib/gcc/mingw32/5.3.0/include"

or similar to your browse.path array. These paths are recursively searched, so use /* if you want to make it non-recursive.

And the reason that the new IntelliSense didn't work for you is because on Windows the IntelliSense engine compiles the code in Microsoft mode instead of clang/gcc mode. We don't currently expose a setting to change the mode, but there is a workaround I just tested that resolves the squiggles. It requires you to copy %userprofile%\.vscode\extensions\ms-vscode.cpptools-0.11.1\bin\msvc.64.linux.json over msvc.64.intel.json in the same folder. The Linux JSON file sets the IntelliSense engine to use clang mode instead of Microsoft mode.

I tried the second way and it works! And I think the first method can solve it, too. Thanks for your patient explanation.

Back again, the error shows up once more after I added another head file below iostream. Then I copied some of my IncludePath to browse.path array. It's OK so far, hope that I've solved it once and for all.

Is everything working for you now? If so, you may close this issue. Otherwise, please let me know if there is something else not working and I can help you resolve it.

Thanks.

Same problem here...
What should i do that may solve this problem?

{
    "name": "Linux",
    "includePath": [
        "${workspaceRoot}",
        "/usr/include/c++/5",
        "/usr/local/include",
        "/usr/include"
    ],
    "defines": [],
    "browse": {
        "path": [
            "/usr/include/c++/5",
            "/usr/local/include",
            "/usr/include"
        ],
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
    }
}

Thanks for the help :smile:

@igorPhelype, this issue is about MinGW on Windows. Your configuration looks like standard Linux. Can you please open a new issue and provide some detail about your distro and what exactly is not working for you?

Thanks.

The fix for this has been released. Please install version 0.12.0 and let us know if you have any trouble with it. I also wrote up a page recommending a default MinGW configuration that may be of interest to you.

I used your recommended settings, but there is still problems with things like freopen,scanf,printf
In problems, it says 'identifier "freopen" is undefined' and I believe these are included in my included path.
I use MinGW 6.3.0, does this have sth to do with the problem?

Can you try adding "__GNUC__" to the "defines" array? This fixed the issue for me. I'll update the documentation.

@igorPhelype

I solved it on my machine with these steps :

  1. add #include <bits/stdc++.h> header file to your program
  2. then if you click this newly added line you will see a small yellow bulb
  3. after clicking the bulb you will see options for including more paths
  4. add all paths until the green or red underline is gone

So after adding all include paths this should work fine and mine file looked like this :

{
            "name": "Linux",
            "includePath": [
                "/usr/include",
                "/usr/local/include",
                "${workspaceRoot}",
                "/usr/include/c++/5",
                "/usr/include/x86_64-linux-gnu/c++/5",
                "/usr/include/c++/5/tr1",
                "/usr/include/linux",
                "/usr/include/x86_64-linux-gnu",
                "/usr/lib/llvm-3.8/lib/clang/3.8.0/include"
            ],
            "defines": [],
            "intelliSenseMode": "clang-x64",
            "browse": {
                "path": [
                    "/usr/include",
                    "/usr/local/include",
                    "${workspaceRoot}"
                ],
                "limitSymbolsToIncludedHeaders": true,
                "databaseFilename": ""
            }
        },

Note : You may also need to install clang on your linux machine

@bobbrow
Hi bobbrow, could you please help me resolve the issue in "intelliSenseMode": "msvc-x64"?

I get the following 3 errors in VS Code from Win10:

  1. identifier "string" is undefined"
  2. no operator "<<" matches these operands -- operand types are: std::ostream << const char [10]
  3. argument list for variable template "endl" is missing

after add the "includePath" and "browse"->"path" as the result of "gcc -v -E -x c++ -", as following:
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include/c++",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include/c++/i686-w64-mingw32",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include/c++/backward",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include-fixed",
"C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/../../../../i686-w64-mingw32/include",
"${workspaceRoot}"

I changed "intelliSenseMode" to "clang-x64", but other errors showing up.

I add "__GNUC__=6" in "defines" that resolves the last 2 errors, so there is only the first error.
I have #include iostream and string, using namespace std; also get a new error: identifier "cout" is undefined.

@lovingjo "msvc-x64" is only for cl.exe (on Windows)...might be better to rename it to avoid the confusion. clang-x64 is for clang and gcc, although we have a backlog item to create a gcc mode for cases where clang diverges. What are the errors with clang-x64?

You might also consider trying out insiders build and setting your compilerPath setting to the mingw compiler: https://github.com/Microsoft/vscode-cpptools/releases/tag/v0.16.0-insiders , which automatically add all the system includes/defines.

Also, it's usually better to create a new issue instead of adding to a closed issue.

@sean-mcmanus Thank you for your reply!! I just recreate a new c_pp_properties.json, and update as following, all errors or warning are gone, and then I think may be I should add the path result of "gcc -v -E -x c++ -" to browse.path, I try it, and all good.

I find that "__GNUC__=7", "__cdecl=__attribute__((__cdecl__))" added in "defines" are the key to resolve the issue, am I right? but I still don't understand the meaning of these two.

Could you please tell me some document about the "defines" and why should we keep the order of includePath and browser.path?

    {
        "name": "Win32",
        "includePath": [
            "${workspaceRoot}",
            "C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include/c++",
            "C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include/c++/i686-w64-mingw32",
            "C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include/c++/backward",
            "C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include",
            "C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/include-fixed",
            "C:/Program Files (x86)/mingw-w64/i686-7.2.0-posix-dwarf-rt_v5-rev1/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.2.0/../../../../i686-w64-mingw32/include"
        ],
        "defines": [
            "_DEBUG",
            "UNICODE",
            "_UNICODE",
            "__GNUC__=7",
            "__cdecl=__attribute__((__cdecl__))"
        ],
        "intelliSenseMode": "clang-x64",
        "browse": {
            "path": [           
            ],
        "limitSymbolsToIncludedHeaders": true,
        "databaseFilename": ""
        }
    }

@lovingjo Yeah, adding the 2 defines is correct. And you want the root paths for your cpp/h files in the browse.path if you want them to be found for Go to Definition (that setting is recursive). If you set your compilerPath with the insider build, then setting the defines shouldn't be necessary (we automatically query the compiler for the builtin includes and defines). I'm not sure I understand what you're asking with your other questions.

@sean-mcmanus I should keep learning more about coding. Thank you very much!!

If you have the "IncludePath" and browse.path settings all correct, you can set "intelliSeneseMode" from "msvc-x64" to "clang-x64", which solved my problem.

I find the same problem...

           "includePath": [
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/backward",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/include-fixed",
                "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/include"
            ],

this is my include path, but actually with the simple code.

#include <cstdio>

int main() {
    std::printf("hello world");
}

The intelligence seems not able to find printf with a

namespace "std" has no member "printf"

@qqktr can you try setting "compilerPath" to "C:/Program Files/mingw-w64/x86_64-8.1.0-posix-seh-rt_v6-rev0/mingw64/bin/g++.exe" (or where ever your compiler lives)?

We do not recommend adding system include paths directly to "includePath" since we added the "compilerPath" setting. We will set those for you if you use "compilerPath".

If that does not resolve your problem, please open a new issue and we will help you troubleshoot.

Was this page helpful?
0 / 5 - 0 ratings