48

When using the "Default" intellisense engine, some of the symbols in my C++ project cannot be resolved. It turns out that it's because they are in headers where they are guarded by an #ifdef that depends on a macro passed to gcc with the -D flag by the makefile. How can I tell the intellisense engine about these defines so that it is able to compile those parts of the header?

Kamil Kisiel
  • 19,723
  • 11
  • 46
  • 56

4 Answers4

81

Project makefile defines are set in .vscode/c_cpp_properties.json.

"configurations": [
{
   ...
   "defines":[
       "MYSYMBOL",
       "MYVALUE=1"
   ]
}
], ...

Here are some methods to open c_cpp_properties.json:

  1. Find a green squiggle on something like an include statement that Intellisense can't resolve. Hover around and click the lightbulb that appears (which is tiny and a bit of a game to click). It will open the project config file in the editor.

  2. Same as above, but put cursor on the green squiggle line and press Ctrl+..

  3. Use the command pallet: ctrl+shift+P, then select C/C++: Edit configurations (JSON). Note that you should be in a C/C++ file while opening command pallet, otherwise this option might not appear in list.

  4. If the file already exists in your .vscode folder, open it with File->Open.

Although vscode will reprocess the settings after c_cpp_properties.json is modified, I found a restart is sometimes required when changing values.

There is basic and incomplete information here: https://code.visualstudio.com/docs/languages/cpp

This is a good link about the c_cpp_properties.json file itself: https://code.visualstudio.com/docs/cpp/c-cpp-properties-schema-reference

Pushpendra
  • 459
  • 1
  • 4
  • 18
jws
  • 2,171
  • 19
  • 30
  • If you are making use of compileCommands JSON file, it uses the defines from the JSON file. Is there any way to force a define of eg. INTELLISENSE inside the intellisense engine? – Wayne Uroda May 21 '20 at 04:34
  • 6
    In newer version of VSCode: File->Preferences->Settings->(Search right panel) c_cpp > Default:Defines – LeviX Jun 16 '20 at 15:00
  • 1
    Is there also a way to "undefine" a macro? – rustyx Mar 21 '21 at 15:57
  • +1 for the link to the .json file itself. So many answer give just a breadcrumb about a detail without context. – harper Apr 02 '23 at 06:45
2

The other answers so far have two issues that don't seem trivial to fix:

  1. There might be times where different source files need different compiler flags.

  2. It could be painfully tedious to manually figure out what compiler flags are necessary and manually add them.

Luckily, VS Code's C/C++ extension supports a compile_commands.json database. This stores information specific to every individual source file including the defines, include directories, and other compiler command line flags. I just posted a more detailed description of how to generate one and get VS Code to use it over here: https://stackoverflow.com/a/59618515/12663912

Peter O.
  • 32,158
  • 14
  • 82
  • 96
dutchkin
  • 121
  • 1
  • 3
1

This capability has now been added: https://github.com/Microsoft/vscode-cpptools/issues/304

you can set "defines" in your c_cpp_properties.json file

Daniel
  • 3,243
  • 2
  • 32
  • 31
  • 1
    Is it possible to "auto-"fill `"defines"` with the content of files like toplevel `.config` files? Like for example `"defines": ["${workspaceFolder}/.config"],` – bambino307 Sep 19 '19 at 10:31
0

After following the advice in this thread I still couldn't get vscode to recognise the macros I'd defined in c_cpp_properties.json.

Turns out, if you're using the CMake extension for vscode and have the "all" target selected then any macros you've defined in your CMakeLists won't be recognised, nor will any in the c_cpp_properties.json. It was as straightforward as selecting a target to build from the status bar and intellisense was able to pick up any macros defined for that target. No need for the c_cpp_properties.json.

ImJimmi
  • 98
  • 5