squeevee
squeevee

Reputation: 161

Visual Studio Code intellisense, use g++ predefined macros

I'm using Visual Studio Code for c++ with MinGW and g++. My code uses a couple of g++ predefined macros. These work in the compiler without any problem, but Intellisense doesn't know about the macros and that causes some misdiagnosed errors (and other Intellisense problems) in the editor.

I can get all the predefined macros for my particular configuration of g++ with this command:

g++ -dM -E foo.cpp > defines.h

And this allows me insert them into the "defines" property of the c_cpp_properties.json file. This solution is a hack though, and if I'm not careful it could completely undermine the purpose of these macros. I want the compiler and Intellisense to cooperate across multiple development platforms, and that's looking like a pretty complicated setup.

Is there a better way to let Intellisense know about the g++ predefined macros?

Upvotes: 1

Views: 1674

Answers (3)

xvan
xvan

Reputation: 4885

Combining clint and squeeve answers:

If you're using cmake and CMake Tools extension, and assuming that CMake outputs to ${workspaceFolder}/build/, cmake generates a compile_commands.json that can be loaded as:

{
    "configurations": [
        {
            "compileCommands": "${workspaceFolder}/build/compile_commands.json",
            "configurationProvider": "ms-vscode.cmake-tools"
        }
    ],
    "version": 4
}

Upvotes: 0

squeevee
squeevee

Reputation: 161

The property in c_cpp_properties.json called compilerPath allows IntelliSense to query the compiler for default include paths and defines. It may be necessary to set intelliSenseMode to gcc-x64 for this to work, as that is not the default on Windows. (I currently do not have a Windows setup so I can't test this for the time being)

Upvotes: 1

Clint Chelak
Clint Chelak

Reputation: 242

From what I can tell, Intellisense's ability to properly handle preprocessor macros is still limited. If you are using CMake for Makefiles, it appears that you can tell Intellisense what macros you have, as seen here: auto-defined macros. I have not played with this personally, but it would be worth investigating.

If you can't get this to work, I found a feature to just turn off the macro-based highlighting. In settings, set"C_Cpp.dimInactiveRegions" to false. This isn't ideal, because it stops all graying out, including debug blocks like if(0) {...}, but it could be a temporary fix and you might find it less irritating.

Apart from that, look closely for added features in future updates. I'll try to update this post when I find any new discoveries on the matter.

Upvotes: 1

Related Questions