LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Scripting Conditional Disable Symbols (#IfDef)

Solved!
Go to solution

My first real need of compile time flags came while working on a large project which had an optional machine vision module.  The problem was that a vision license had to be deployed on every machine, regardless of whether or not the customer ordered the vision module, because the code contained vision dependencies.  The solution was to refactor to segregate all of the vision code inside of "ENABLE_VISION" conditional structures.  If ENABLE_VISION is False then all of the vision code becomes disabled and the vision dependency no longer exists.  All worked great, hero of the day and all that.  Then I went on vacation and another developer needed to modify the code.  They neglected to look at the readme file because they had experience with the code, were in a hurry, ..., all the usual "reasons".  Time was wasted because they did not know to set the conditional symbol to True and could not get vision to work.  Eventually they checked the project properties and solved the problem.

I come from a textual programming background (C++, Python, VB).  C++ and VB have a conditional compilation feature which allows the programmer to, among other options, turn features on/off at compile time.  LabVIEW does not expose this feature.  There is an undocumented library which can be used to accomplish much the same thing.  I am running LabVIEW 2015 so the path to the library is "C:\Program Files (x86)\National Instruments\LabVIEW 2015\resource\plugins\Utility\IfDef.llb".

You can setup conditional disable symbols by going to Project>Properties>Conditional Disable Symbols.
http://zone.ni.com/reference/en-XX/help/371361N-01/lvhowto/creating_cond_disable_struc/

While developing a project I add symbols and conditional disable structures as needed.  I then use a prebuild script template and an included vi template to write the value of the conditional symbols at build time.  I only use boolean type flags (denoted by "True" string with any other value being false) so that I can logically think of the symbols as #IFDEF as in C++/VB.

I use SetSymbols.vi from IfDef.llb to set/unset project conditional symbols at compile time.  The method that I use will remove the symbols from the conditional symbols property page (maybe ... it does not remove them every time but eventually they go away).  This results in all conditional symbols being interpreted as false.  One side effect is that the symbol must be redefined and set True to test sections of code inside of the relevant conditional disable structures.  In general a project only has 2 or 3 conditional symbols and the syntax is contained in the selector of the conditional structure so it is easy to figure out what to do as long as you know about the conditional disable symbols feature of LabVIEW.

I have attached two LabVIEW 2015 VIs.
Pre-Build Action.vi is the prebuild script.
Set Build Flags.vi which does the manipulation of the build flags.

SetSymbols.vi: I set "Append" False because my experimentation showed that it does not always append so I decided to be clear about what reliably happens.  I leave "Is Persistent" default False for the same reason.
Set Build Flags.vi is intended to be used like a template.  Add/remove flag names and value selection boxes as needed.


The VIs are very simple, I will leave the rest of the documentation to the code.

Download All
Message 1 of 5
(4,118 Views)
Solution
Accepted by topic author randol

I didn't know about the SetSymbols.vi, but this is pretty much how it's done. Comment you don't ask for in this post, but you did here.

 

+1 for posting.

 

This should of course be done from a build spec, so I'm crosslinking to the idea that would make that possible.

 

As you're positing a solution, you might accept any post (yours) as solution. Otherwise, people will open this trying to answer...

0 Kudos
Message 2 of 5
(4,097 Views)

Yup, my approach is basically the same, only I elected to implement a simpler routine to set the symbols. 

 

All I do is include the symbol value in the build configuration name, strip it off in the pre-build vi, and feed it directly into the setConditionalSymbols vi. 

 

There's also a check for valid values, and if there is none it returns an error, and one nice thing is that an error out in the pre-build VI will automatically cancel the build. 

 

It's almost easy at this point, though I'm still flabbergasted that I had to spend the time to kludge this.

Download All
0 Kudos
Message 3 of 5
(4,075 Views)

wiebe@CARYA wrote:

As you're positing a solution, you might accept any post (yours) as solution. Otherwise, people will open this trying to answer...


I cannot accept my original post as solution, so I am accepting yours.

Message 4 of 5
(4,065 Views)

@randol wrote:

wiebe@CARYA wrote:

As you're positing a solution, you might accept any post (yours) as solution. Otherwise, people will open this trying to answer...


I cannot accept my original post as solution, so I am accepting yours.


Thanks.

0 Kudos
Message 5 of 5
(4,050 Views)