Defining preprocessor options using Visual Studio Code

I'm transitioning from SES to Visual Studio Code.

In SES, I can define multiple builds and then define preprocessor options (eg. -DOPTION1) for each build definition

I don't see a way to do this when using the nRF Connect SDK and Visual Studio Code.

(Note:  I know how to specify defines using the CMakeLists.txt file for all builds (using add_definitions(-DOPTION1) , but not on a "per-build" basis).

I'm using the NRF CONNECT extension in Visual Studio Code.

Thanks!

  • Hello,

    I have asked the developers and am awaiting their answer, but in the meantime could you take a look if CMake presets may be something relevant?

    Best regards,

    Michal

  • I have already tried my best with CMake presets. But it does not seem to be a good solution, following is what I have tried:

    CMakeLists.txt

    option(CUSTOMBUILD "flag used to define all flags for custom build" n) #n by default
    if(CUSTOMBUILD)
        #add some compilation flags
        target_compile_definitions(app PRIVATE DISABLE_SOMETHING=1)
    else()
        #add some other compilation flags
    endif(CUSTOMBUILD)
    unset(CUSTOMBUILD CACHE)

    Created a new build config with following Extra CMake arguments: 

    -DCUSTOMBUILD=y

    Saved the configuration as preset, it shows up in  CMakePresets.json as :

    {
        "version": 2,
        "cmakeMinimumRequired": {
            "major": 3,
            "minor": 20
        },
        "configurePresets": [
            {
                "name": "build_custom",
                "displayName": "Build for nRF9160 DK NRF9160 Non Secure",
                "generator": "Ninja",
                "binaryDir": "${sourceDir}/build_custom",
                "cacheVariables": {
                    "NCS_TOOLCHAIN_VERSION": "NONE",
                    "BOARD": "nrf9160dk_nrf9160ns",
                    "CUSTOMBUILD": "y",
                    "CONF_FILE": "${sourceDir}/prj.conf",
                    "DTC_OVERLAY_FILE": "${sourceDir}/nrf9160dk_nrf9160ns.overlay"
                }
            }
        ]
    }

    The problems with this approach are:

    1. When another developer loads the build configuration, they are able to see the Extra CMake arguments filled from the CMakePresets. But afterwards, if someone goes to Edit Build Configuration, these extras are no more visible.
    2. After a Prestine Build, extra presets are gone and have to be set again. 

    My intention is to use the flags defined for custom build as:

    #ifndef DISABLE_SOMETHING
    // enable
    #endif

  • Okay, I understand.

    I have now gotten the confirmation that unfortunately, there hasn't been any changes since the last time concerning this feature.

    Best regards,

    Michal

  • Just rebooting this one as it seems crazy that you cannot pass a preprocessor define to the different builds in a project.  This is a basic function of all IDEs I have used in the past for firmware development. Any update?

  • I will ask the developers about it.

    Best regards,

    Michal

Related