How to define a build variable

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to define a build variable

1,592 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tvink on Mon Apr 25 07:21:02 MST 2016
Hi,

I want to define a build variable ( with a value ) and keep it in the project properties.  There seems to be a place for this in "properties->C/C++ build->Build Variables"  however I can not get this to work when I build. 

I have a variable "blabla" with a value 1, of type string.  ( everything is in debug configuration )
I have tried placing this in the properties for my project and in the properties of the referenced item where the #if resides.  Have tried with the "Show system variables" checked and unchecked in the Build Variables dialog.

In my code I have 

#if(blabla == 1)
code here
#else
other code here
#endif

The if clause continues to be greyed out.
If I place the define #define blabla 1 in my header file, it works.

Should this work from the project files or am I missing something.

Thanks
0 Kudos
10 Replies

1,301 Views
lpcware
NXP Employee
NXP Employee
bump
0 Kudos

1,301 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tvink on Wed Apr 27 07:04:44 MST 2016
Thanks, you have helped me advance my understanding the tool.  Even though it is not perfect I can live with the way things stand.
0 Kudos

1,301 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Wed Apr 27 00:51:50 MST 2016
The 'greyed out' is determined by the Indexer. You need to re-index your projects to fix this. To do this, right-click on the project and select Index->Rebuild.

When you rebuild a project, files are only rebuilt if the build system - make - knows it has changed. If a file changes, make know this and so can recompile it. If a compiler setting has changed, LPCXpresso knows about this and can force the build. The problem with Build Variables is that they are outside of both of these mechanisms and so things do not get rebuilt automatically. There may be a better way to do this, that does work as you want, but I don't know it.
0 Kudos

1,301 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tvink on Tue Apr 26 09:24:40 MST 2016
Update on last post

I placed a #pragma message "message here" in each of the #if sections.
It appears that the correct parts are compiling... at least the correct message ends up in the log file.

But the wrong parts remain greyed out in the source editor.  Even if I close and reopen.

So maybe there is something wrong with the editor?


Another thing goes wrong too... if I change the build variable in the workspace and then rebuild all... the files don't recompile.  They think that they are up to date.  You have to remember to do a clean.  Even "build all projects" does not build all projects unless you clean first...


Tony
0 Kudos

1,301 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tvink on Tue Apr 26 06:39:05 MST 2016
Here are code bits from two files that build both build in my test project.  ( I temporarily put a syntax error in each to make sure that they were actually building.  Also did a "clean" in the quick start before building. )
Both projects have the same ${BOARD_TYPE} in their symbols.

Here is the log line for compiling sdmmc.c, the -DDGTYPE=1 is in there

arm-none-eabi-gcc -DDGTYPE=1 -D__REDLIB__ -DDEBUG -D__CODE_RED -D__USE_LPCOPEN -DCORE_M3 -I"E:\Dev\VSI-2534-Nextgen\LPCXprssoWorkSpace\BoardBringup\lpc_chip_18xx\inc" -I"E:\Dev\VSI-2534-Nextgen\LPCXprssoWorkSpace\BoardBringup\lpc_board_ngx_xplorer_1830\inc" -I"E:\Dev\VSI-2534-Nextgen\LPCXprssoWorkSpace\BoardBringup\VSI_2534__periph_sdmmc\example\inc" -I"E:\Dev\VSI-2534-Nextgen\LPCXprssoWorkSpace\BoardBringup\VSI_2534__periph_sdmmc\fatfs\inc" -O0 -g3 -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -fdata-sections -mcpu=cortex-m3 -mthumb -specs=redlib.specs -MMD -MP -MF"example/src/sdmmc.d" -MT"example/src/sdmmc.o" -MT"example/src/sdmmc.d" -o "example/src/sdmmc.o" "../example/src/sdmmc.c"
Finished building: ../example/src/sdmmc.c


Here is the log for compiling board.c, the -DDGTYPE=1 is in there too

arm-none-eabi-gcc -DDGTYPE=1 -D__REDLIB__ -DDEBUG -D__CODE_RED -D__USE_LPCOPEN -DCORE_M3 -I"E:\Dev\VSI-2534-Nextgen\LPCXprssoWorkSpace\BoardBringup\lpc_chip_18xx\inc" -I"E:\Dev\VSI-2534-Nextgen\LPCXprssoWorkSpace\BoardBringup\lpc_board_ngx_xplorer_1830\inc" -O0 -g3 -Wall -c -fmessage-length=0 -fno-builtin -ffunction-sections -fdata-sections -mcpu=cortex-m3 -mthumb -D__REDLIB__ -specs=redlib.specs -MMD -MP -MF"src/board.d" -MT"src/board.o" -MT"src/board.d" -o "src/board.o" "../src/board.c"
Finished building: ../src/board.c



Here is the code in file sdmmc.c in project: VSI_2534__periph_sdmmc, the 1st two lines are not grey after the build.

//#define DG_TYPE 2

#if DGTYPE == 1
#define bbbb 1
#elif DGTYPE == 2
#define bbbb 2
#else
#define bbbb 3
#endif



Here is the code in the board.c file in library: lpc_board_ngx_xplorer_1830, the last three lines are not grey after the build

//#define DG_TYPE 2

#if DGTYPE == 1
#define aaaa 1
#elif DGTYPE == 2
#define aaaa 2
#else
#define aaaa 3
#endif



Thanks!
0 Kudos

1,301 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Tue Apr 26 01:07:51 MST 2016

Quote:
t takes two "clean then builds"...


I see this too. I suspect that it is because the makefiles are not being regenerated (the makefile has the expanded version of the build macro, not the actual build macro). The 2nd clean seems to regenerate the makefile. One way around this would be just to delete the Debug (or Release, as appropriate) directory and then just 'Build'.


Quote:
I am a little afraid to mess with the library build settings...


Why? Just add the changes you want to make (to the build settings and the source code)


Quote:
..the build variable does not seem to be available in the code..


This makes no sense. Show me the exact code your are trying to use and the command line passed to the compiler. For example, this works for me
#if VERSION == 10
/* version 10 code*/
#else
/* other version of code */
#endif
0 Kudos

1,301 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tvink on Mon Apr 25 13:53:04 MST 2016
Thankyou, that was very helpful...

I still have a couple of problems if you don't mind offering suggestions.

It takes two "clean then builds" in quick panel before a change in the build variables takes affect.  I.E. After changing the build variable the first time I clean then build, I still see the old variable in the log files
( -DVERSION=10 as in your example ).  On the second clean then build, the new value is passed ( -DVERSION=20 ).   Not a big deal as long as you remember to do the build twice.  Sure is confusing when you don't.


The build variable is working properly in my all projects where I set ${MY_BUILD_VAR} as in your example...   except for the library provided in LPCOpen.   I am a little afraid to mess with the library build settings for fear of breaking something that may not be readily apparent.  However I do need to change stuff in there and would like to be able to control this via the build variables too.   Any ideas?

( I did put the ${MY_BUILD_VAR} in the compiler symbols but the build variable does not seem to be available in the code... and the -DVERSION=20 is in the log....  just is not seen by the #if VERSION == 20 in the code )

Thanks,
Tony
0 Kudos

1,301 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Mon Apr 25 09:31:13 MST 2016
You can do this (and define a value too).

Note that when passing preprocessor defines via the command line, use VARIABLE=value. For example
-DVERSION=10

You can simply use a build variable to define the symbol and pass it to all your projects:

1. Window->Preferences, C/++->Build Variables
add a variable called (for example) MY_BUILD_VAR and give it a value which is what you want to pass to the compiler. i.e. VERSION=10. So, now MY_BUILD_VAR="VERSION=10"

2. IN each project that you want to use it, add the following to the Settings (MCU Compiler->Symbols)
${MY_BUILD_VAR}

Now compile your projects. You can add as many build variable and preprocessor symbols as you like by repeating the above steps.

HTH
0 Kudos

1,301 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by tvink on Mon Apr 25 09:12:46 MST 2016
Hi Thanks,

That's a little closer but I was hoping for better control.  ( I did read through your link and it is helpful )

The compiler define does not allow me to set a value so I will have to change my strategy, I can live with that.

But what seems to be a killer is I have to set the define in the properties of the referenced items as well as the project.  So I loose the "one point of control".
( For example if I set the define in my project properties it is not seen in board.c which is in "lpc_board_ngx_xplorer_1830"... I need to set it there too )

My ideal solution would be something that was seen workspace wide.

Can this be done?

Thanks
0 Kudos

1,301 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Mon Apr 25 07:39:04 MST 2016
Are you sure that you aren't just trying to set a compiler define? Which can be done in the Symbols entry of the Compiler properties for your project/build config.

Anyway, there is some information on build variables at:

https://www.lpcware.com/content/faq/lpcxpresso/build-macros

Regards,
LPCXpresso Support
0 Kudos