LabVIEW C Code Generator
LabVIEW C Code Generator
Use the LabVIEW C Generator to generate generic C code from VIs. The generated C code can run on
most platforms.
Note Refer to the LabVIEW C Generator Readme, which is available by opening
readme_C_Generator.html on the NI LabVIEW C Generator CD, for system requirements
and instructions about installing the C Generator.
This manual contains a step-by-step tutorial that shows you how to generate C code from an exported
VI and use that generated C code on a target. In this tutorial, you specify a VI to export for C code
generation, configure C code generation settings, define the function prototype for the exported VI,
generate C code, and use the generated C code on a non-LabVIEW target. The tutorial also describes
how to create and configure the generated C code so you can use it in the following commonly used
targets:
• A 32-bit Windows platform (Win32) DLL created with the Microsoft Visual Studio compiler.
• A static library created in Cygwin using GNU Compiler Collection (GCC).
• An ARM application built in Keil µVision for a Luminary Micro EK-LM3S8962 target.
Note You must configure some settings differently for each target. As you use this tutorial,
refer to the Win32 DLL, Cygwin Static Library, and EK-LM3S8962 Application sections of
this manual for target-specific instructions.
Contents
Installing Additional Third-Party Software..........................................................................................1
Creating the LabVIEW Project.............................................................................................................2
Creating a Build Specification..............................................................................................................2
Creating a VI for C Code Generation...................................................................................................3
Defining the Function Prototype...........................................................................................................5
Setting C Code Generation Options......................................................................................................7
Testing the VI........................................................................................................................................7
Generating C Code in LabVIEW..........................................................................................................9
Building the Generated C Code..........................................................................................................11
Win32 DLL.................................................................................................................................11
Cygwin Static Library.................................................................................................................13
EK-LM3S8962 Application........................................................................................................15
Where to Go from Here......................................................................................................................16
Tip When you create a new VI to export for C code generation, you must account for differences
in VIs you develop to run as LabVIEW applications and VIs you develop for C code generation.
Exported VIs and all VIs in their hierarchies support some block diagram objects differently
from VIs running as LabVIEW applications. Refer to Developing VIs for C Code Generation
in the LabVIEW Help for more information about these differences. Select Help»LabVIEW
Help to search the LabVIEW Help.
Testing the VI
National Instruments recommends you test the exported VI before generating C code and test generated
C code before you use it externally. You can test the exported VI in LabVIEW to simulate the behavior
of the generated C code.
The C Generator includes syntax checking of the exported VI. When you create a C Code Generation
build specification, the C Generator enables syntax checking for the exported VI. When LabVIEW
checks the syntax of the VI for which you want to generate C code, LabVIEW is checking that the C
Generator can create generic C code based on the design of the exported VI. LabVIEW always checks
the syntax of the exported VI before it builds the VI.
Complete the following steps to test the exported VI in LabVIEW and check the syntax of the exported
VI.
1. Open Greatest Common Divisor.vi.
2.
Enter two numbers in the x and y controls. Click the Run button, , in the VI. The VI displays
the greatest common divisor of the two numbers.
3. View the block diagram. If the block diagram is not visible, select Window»Show Block Diagram.
4. Add a Timed Loop to the block diagram. The Timed Loop is located on the Timed Structures
palette. Because the Timed Loop is unsupported by the C Generator, LabVIEW displays the Warning
button, , in the VI toolbar, as shown in the following figure.
LabVIEW displays the Warning button during syntax checking if the exported VI hierarchy contains
unsupported data types, VIs, functions, or structures. While syntax warnings do not prevent you
from running the exported VI in LabVIEW, you cannot generate C code for the VI until you correct
all syntax warnings.
5. Click the Warning button in the VI toolbar to display the Error list window, which explains that
the Timed Loop is unsupported by the C Generator, as shown in the following figure.
6. Remove the Timed Loop from the block diagram. LabVIEW no longer displays the Warning button
in the VI toolbar because the syntax is correct.
Note If you remove the checkmark from the Show Warnings checkbox in the Error list
window, LabVIEW no longer displays syntax warnings for C code generation. If you disable
warnings, you can check the syntax of the exported VI by right-clicking the C Code Generation
build specification in the Project Explorer window and selecting Check Syntax from the
shortcut menu.
• dependencies.mk is the secondary makefile. The C Generator includes all LabVIEW settings,
such the build specification name and C code generation option values, that are required to generate
C code. The C Generator updates this file if the hierarchy of the exported VI changes. LabVIEW
also automatically overwrites this file during the build process.
DllMain.c, which is not created when the C Generator generates C code, is an example entry point for
a DLL. This file is required for building DLLs that run on Windows. This file is located in the
labview\CCodeGen\libsrc\platform\win directory.
Win32 DLL
Complete the following steps to use the generated C code in a Win32 DLL created with the Microsoft
Visual Studio compiler.
Note By default, the C Generator creates a Makefile for a Win32 DLL created with the
Microsoft Visual Studio compiler.
1. Open Makefile, which is located in the directory that contains the generated C files.
2. Ensure that the following source files are included:
BDLIBS1 = CCGArrSupport.c CCGArrSupport2.c CCGArrSupport3.c
CCGClusterSupport.c CCGCmplxSupport.c CCGEnumSupport.c CCGDVRSupport.c
CCGFXPSupport.c
BDLIBS2 = CCGFltSupport.c CCGIntSupport.c CCGStrSupport.c CCGTimeSupport.c
CCGTokString.c CCGUDClassSupport.c CCGVariant.c CCGXMLSupport.c ExecStack.c
BDLIBS3 = LVBlockDiagram.c LVCGenRTInit.c LVContext.c LVHeap.c LVTdp.c
MemCheck.c nbemptybin.c nbfifo.c nbfifo_inst.c nbitemtable.c NumText.c
ViLib.c rtmath.c
BDLIBS = $(BDLIBS1) $(BDLIBS2) $(BDLIBS3)
LVLIBS = arrresize.c
Tip Refer to Including Generated C Code in External Applications in the LabVIEW Help
for more information about which source files you must compile.
LVLIBS = arrresize.c
COMMONLIBS = Headless.c
Tip Refer to Including Generated C Code in External Applications in the LabVIEW Help
for more information about which source files you must compile.
LVDefs_plat.h contains basic constants and macros that the C Generator needs to generate C
code. This file provides the mapping between generic C function calls that the C Generator generates
and platform-specific, run-time functions. LVDefs_plat.h also contains platform definitions of
data types for the compiler.
To properly define data types, you must know the compiler definitions of those data types. To
define the generic function calls, you must know how the target SDK implements these function
calls. LVDefs_plat.h defines the data types and function calls correctly for a Cygwin target.
12. Close LVDefs_plat.h.
13. Open and review LVSysIncludes.h, which is located in the
labview\CCodeGen\include\platform\cygwin–gcc directory.
LVSysIncludes.h contains platform-specific header files, toolchain-specific header files, and
standard C header files. Most files in the labview\CCodeGen directory include LVSysIncludes.h.
14. Close LVSysIncludes.h.
Running the GNU Make Utility to Create an Executable
You use the GNU Make utility to build applications and libraries automatically from the generated C
code. Before you can run the Makefile in the directory that contains the generated C code, you must
set up the required toolchain.
Complete the following steps to set up the toolchain and run the GNU Make utility.
1. Use the Cygwin command line to verify that GCC and the GNU Make utility are on the PATH.
2. To create GCD.exe, call the make GCD.exe command from the command line.
Running and Calling the Generated C Code
You now can run GCD.exe to test the exported function. When you run the application, main.c calls
GreatestCommonDivisor(12, 15). The expected output of the exported function is 3.
EK-LM3S8962 Application
The C Generator installs a µVision project (.Uv2) you can use to build the generated C code in an ARM
application. Complete the following steps to use the generated C code in an ARM application built in
Keil µVision for a Luminary Micro EK-LM3S8962 target.
1. In Keil µVision, open labview\examples\CGenerator\Tutorial\Keil – ARM
LM3S8962\example.Uv2.
2. Expand the LM3S8962 target in the Project Workspace window.
3. Expand the Generated Files folder. Notice that folder includes GCD.c and
NI_Gmath_lvlib_Gcd.c, which are the generated C files.
4. Right click the LM3S8962 target in the Project Workspace window and select Options for Target
from the shortcut menu.
5. On the Target tab, notice that Operating system is set to Nothing. Do not set this value to RTX,
which is incompatible with the generated C code.
6. On the Target tab, ensure that the Use MicroLIB option does not contain a checkmark. This option
is incompatible with the generated C code.
The program calls GreatestCommonDivisor(12, 18). After you click the Run button, the LED
blinks six times to return the greatest common divisor of 12 and 18.