Compiler flags in vscode with nRF connecy extension

I want to add compiler  flags to my  build configuration i.e _DEBUG, etc

My understanding is that  a build is done by  build task

nRF Connect extension allows to create a tasks.json from a default nRF Connect build task

In  Terminal->Configure tasks I can select nrf Connect build and it creates the following tasks.json file

{
    "version": "2.0.0",
    "tasks": [
        {
           
            "type": "nrf-connect-build",
            "config": "c:\\<project>\\build",
            "runCmake": false,
            "problemMatcher": [
                "$gcc",
                "$cmake",
                "$kconfig",
                "$kconfig_syntax",
                "$kconfig_syntax_files",
                "$dts",
                "$dts_syntax"
            ],
            "group": "build",
            "label": "My app(Debug)"
        }
    ]
}
The problem is that I cannot add the "args" property - at least in other discussions I saw it being used to add compiler flags 
When I add it I get an error "args is not allowed"
How can I add compile flags to my nrf connect build task ? Or is there another way to do this?
My ultimate goal is to create a debug and release configurations with some defines present in one but not the other 
Thanks
  • Hi Andy,

    Sorry for the late response..

    The field Extra flags for CMAKE adds variables that CMake understands and those are not saved, so they only apply to the build just being created.

    Adding -DCMAKE_C_FLAGS=-DAPP_DBG_LEVEL=4 in the extra flags field is one way to do this.. To make it shareable, try adding it in the cmake file by using the following command:  

    target_compile_definitions(app PRIVATE -DAPP_DBG_LEVEL=4)

    Best Regards,
    Swathy
  • Could you clarify: where does the "target_compile_definitions" directive needs to be added? In CMakeLists.txt?
    If that's the case it does not solve my problem. My ultimate goal is to have 2 configurations, one for debug and one for release. In the debug configuration the APP_DBG_LEVEL is 4 and in the release one it's 0 . And those configurations need to be shareable. How do I accomplish that?
    Thank you 

  • Any other ideas? Seems like such a  basic thing in any IDE to configure debug and release build configurations with some compiler flags present on one but not the other. How can this be accomplished in a vscode project??

  • This has not been resolved. Any other ideas? It seems like a fundamental feature that is trivial to configure in all other IDE's I've worked with

    What am I missing??

  • Hi Andy,

    Adding -DCMAKE_C_FLAGS=-DAPP_DBG_LEVEL=4 in the extra flags field is one way to do this. But this is not a shareable way.

    Could you try doing it the following way.?

    Add a flag to control compilation flags on the command line, and set additional flags in an if/else in the cmakelists.txt file. This is how you can implement this:

    In cmake, set up a flag that controls the behavior - for instance "APP_DBG_LEVEL". Then, in the cmake code, you can check this flag, and activate other settings or behavior depending on the value. Take a look at this Stack overflow answer, for example: https://stackoverflow.com/a/48984477/2495121. In the answer, a flag called USE_MY_LIB is added to control compilation flags and other things. 

    Where the snippet in that answer says "#add some compilation flags", you could do

    target_compile_definitions(app PRIVATE ABC=5)

    which creates a define called "ABC" that can be referenced in the source code.

    This way, there is no need to specify -DCMAKE_C_FLAGS="-DABC=5" on the command line to get the expected behavior, but could enable different configuration types through more understandable command line flags, if required. Hope this helps.

    Regards,

    Swathy

Related