eclipsecmakestm32cubeideindexer

STM32Cube IDE cmake target_compile_definitions not picked up by indexer


I have a Cmake project with in the cmakelists the following command

target_compile_definitions(${PROJECT_NAME}
    PRIVATE CORE_CM7
    PRIVATE STM32H755xx
)

The projects compiles perfectly however in the editor it shows #if defined(CORE_CM7) as inactive piece of code.

I cannot seem to get the Indexer to recognize these definitions.

I have even thought of dirty solution as in exporting it to a header and including that everywhere. But it kinda defeats the purpose in the first place.


Solution

  • Your indexer not recognizing this makes sense if it's just looking at your source code. The CORE_CM7 symbol only exists when cmake constructs the command line arguments to invoke your compiler. You can see this if you have a compile_commands.json being made which is controlled via CMAKE_EXPORT_COMPILE_COMMANDS.

    In your CMakeLists.txt:

    set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
    

    This file is typically used to inform language servers/indexers of the specific compiler invocation which is required to correctly show inactive code, etc.

    I'm not using eclipse but a quick search shows the following as how to enable consulting this file as:

    1. Open Project Properties by right-clicking your project in the Project Explorer panel.
    2. Expand C/C++ General, select Preprocessor Include Paths, Macros etc. and open the Providers tab and enable:
      MCU GCC Built-in Compiler Parser
      MCU GCC Build Output Parser
      Compilation Database Parser
    3. Click Apply to make the Compilation Database text field editable.
    4. Put a full path to your compile_commands.json file into the text field.

    Reference