How to add preprocessor macros in VS code with NCS.

Hi there,

We are quite new to Zephyr. We are currently porting our existing app onto Zephyr as a test. We are currently using NCS 2.5.1

So for our existing application running in IAR, we have different build configurations. The one build configuration is "debug", in which serial logging and other debugging tools is enabled. The other build configuration is release, which is optimized and does not include debugging tools like serial logging in the build (does not configure the uart or use it ever).

For the different build configurations, we then provide different preprocessor macros, for example RELEASE for the release build and DEBUG for the debug build. In the code then, we can use the following:

 #ifndef RELEASE
    //Do something for DEBUG build
 #else
    //Do something for RELEASE build
 #endif
\

However we cant seem to provide preprocessor macros, unless we are overlooking it. I cant imagine that it is not possible to do this. So in the above statement, the #else clause is always true since RELEASE can never be defined.

Does not seem to be easy to have different project settings for different build configurations, as changing the proj.conf and cmakelists.txt file seems to change settings project wide (on all build configs).

Regards,

Frikkie

  • Hi Frikkie

    I would recommend having a look at this guide  and this on how to add different build configurations for Vscode. At least for debugging I would suggest selecting build optimization debugging as it would include more debugging features. You can also create multiple build configurations for one project with different settings and you can specify in the build configuration which prj.conf you want the application to use. 

    From the build configuration window it is also possible to add extra cmake arguments

    Regards

    Runar

  • Thanks for the fast response, Runar. Good to know that different build configurations can use different proj.conf files.

    I have read through the 2 links you have posted, however my original question on how to add a preprocessor macro remains. 

    I have tried adding extra CMAKE arguments, but it does not work. Lets take for example the below code snippet, where we want to define something (in this example we just name it ENABLE_SOMETHING). So if ENABLE_SOMETHING is defined, nothing should happen as the code is commented out. However, if ENABLE_SOMETHING is not defined, the Q_DEFINE_THIS_MODULE(main) function should be called:

    I then add an extra CMAKE argument:

    However, we still get a build error on the line "Q_DEFINE_THIS_MODULE(main)", even though it should not be compiled in. You can also see from the first screenshot that intelli-sense does not grey out the line, indicating the macro is not seen.

    In all IDEs I have worked with, you were simply able to go to the project options, add a preprocessor macro and on you go (IAR example below):

  • Hi and thanks for the fast reply. 

    To solve it like you want we need to define the preproccesor macros for cmake with the calls as shown in this post on stack overflow

    https://stackoverflow.com/questions/9017573/define-a-preprocessor-macro-through-cmake

    You can add variables with the add_compile_definitions()

    I did a quick test with the following code

    #include <stdio.h>
    
    int main(void)
    {
    	printf("Hello World! %s\n", CONFIG_BOARD);
    	#ifdef ENALBED
    		printf("IT IS ENABLED \n");
    	#else
    		printf("IT IS NOT ENABLED \n");
    	#endif
    
    	return 0;
    }
    
    

    cmakeList.txt

    cmake_minimum_required(VERSION 3.20.0)
    
    find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
    project(hello_world_cmake)
    #add_compile_definitions(ENALBED) #ran the code with this commentet and not commented 
    
    target_sources(app PRIVATE src/main.c)
    

    output

    *** Booting nRF Connect SDK v2.5.99-dev1 ***
    Hello World! nrf52833dk_nrf52833
    IT IS ENABLED 
    *** Booting nRF Connect SDK v2.5.99-dev1 ***
    Hello World! nrf52833dk_nrf52833
    IT IS NOT ENABLED 
    
    
    
    
    

    I thought it would work if you added it as an argument in the build configuration. But either I'm doing something wrong or it does not work. So I need to investigate more why it is ignored. Configuring CMakeLists.txt works however. I got it working if I build with the command line and write is as -DVARIABLE=y. I will get back to you tomorrow. 
    Update: I got a coworker to test the same Cmake flag and it works on their computer. So You need to write -DVARIABLE=y in the add extra cmake argument  and it should work. 

    Regards

    Runar

  • Hi Runar, thanks that seems helpful!

    However it seems that this is added project wide for all build configurations. So lets say we have 2 build configs, build x and y. In build x, we want to add_compile_definitions(ENALBED), however in build y, we do not want it added.

    How do we go about doing that? Can we specify 2 seperate CMakelList.txt files?

Related