Vscode-cpptools: "Identifier is undefined" for C-library functions in C++ program.

Created on 3 Nov 2017  路  10Comments  路  Source: microsoft/vscode-cpptools

Environment

  • Operating System: macOS 10.12.6 (16G29)
  • VS Code version : 1.17.2, stable
  • C/C++ extension version : 0.14.0

Bug

The bug occurs when I use any function in C library in a C++ program, such as scanf, printf and malloc. When I am typing the function name, the auto-completion works fine, giving me what I want. But just after I finish typing the whole statement, a red wave line appears under that function. I move my mouse on it, and it shows up something like Identifier "scanf" is undefined. Even after the whole program can be compiled and run correctly, the red wave still exists.
If you need some example code, well, just write a Hello-World program in C, and name it with .cpp suffix, that is an example.
It might be the problem of my setting, but I can't figure out what kind of wrong setting can make auto-completion works fine and error checking fails. So I would rather believe that this was a bug.

Language Service more info needed

Most helpful comment

@jalalmostafa The priority for includePaths is from top to bottom order. You should put the system include files last in the list.

All 10 comments

Autocomplete for scanf/etc. is using the browse.path setting, which is why it's found. The error squiggle is using the includePath and defines setting, so something must be missing from that. Only identifier[.|->|::] uses the includePath autocompletion currently. Can you run clang -v -E -x c++ - < /dev/null with clang replaced by gcc or g++ if you're using those to find out if your includePath setting is correct?

Actually, I'm able to repro this now, even when my includePath appears correct...we'll investigate more.

Oops, sorry. I'm not able to repro this -- my problem was that I accidentally included stdlib.h instead of stdio.h.

I solve the problem accidentally by deleting c_cpp_properties.json file. I still have no idea why autocomplete works but error checking fails.

The issue happens if you have an include file of exactly the same as a header file of the toolchain.
e.g. including a local parser.h while including C:\Program Files (x86)\Windows Kits\10\Include\10.0.16299.0\um in your c_cpp_properties.json which also has parser.h. It seems VSCode cannot prioritize local files even if you include them with double quotes like #include "parser.h".

Windows 10 Build 1709
VSCode 1.19.3
C/C++ 0.14.6

@jalalmostafa The priority for includePaths is from top to bottom order. You should put the system include files last in the list.

@jalalmostafa It works for me. Thank you so much.

The other possibility is that you miss ** at the end of your path in "includePath" in c_cpp_properties.json
For example, it should be like /usr/include/**, but not /usr/include/

@Lynn-Speng Using "/usr/include/**" is not recommended because the ordering of the recursive includes is arbitrary and likely to break for system headers that require a particular ordering. It is better to set your compilerPath to get the correct system includes and ordering, or to manually enter all the system paths in the correct order.

@Lynn-Speng Using "/usr/include/**" is not recommended because the ordering of the recursive includes is arbitrary and likely to break for system headers that require a particular ordering. It is better to set your compilerPath to get the correct system includes and ordering, or to manually enter all the system paths in the correct order.

OK, I learned it. Thanks!

Was this page helpful?
0 / 5 - 0 ratings