From 11:00 PM CST Friday, Apr 11th - 1:30 PM CST Saturday, Apr 12th, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

global #defines and #includes during build from multiple sources?

So I have this project with some modules that have functions which are called by "main", and occasionally some functions that may also be called by other functions within that same module.

           |----> module1.c
main.c ----|----> module2.c
           |----> module3.c
           |----> etc.

I have many #includes and #defines and some structures and functions and whatnot that i want to be global.

I have been building these projects in the CVI environment where all the .c files are lumped in the "source files" box, and all the includes in the "include files" box. 

Apparently I am doing something wrong, because all of my "globals" are only global in scope within it's own module ... including those in "main.c"

Which means I had to repeat certain declarations within each module that requrires them, with led to problems with multiply defined functions, etc...

the obvious solution (i thought) was to exclude the modules from the build environment, and #include them explicitly within my main.c program...  this worked nicely for a while, until it came time to debug in the environment debug window -- then i noticed all of the line numbers and positioning notations were skewed way off, and it became very difficult to trace debug any errors to their source.

I vaguely recall this issue being addressed back in my programming class days ...  so its probably some simple and well-known issue, and I'm just lagging behind everyone else.

but i sure would appreciate it if someone could clue me in.

Thanks!

Rob J.
0 Kudos
Message 1 of 5
(5,849 Views)

I usually put all macros, typedefs, structs, variables and function definitions that are global to the whole application in an include file that I #include in every source that is part of the project, the same as I need to #include common files like userint.h and cvirte.h and the include files associated to your UIRs



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 5
(5,840 Views)

As Roberto says, it is good practice to put common definitions in a header file which is #included by all your C source files. Note that this should only have definitions, not statements that actually generate code. So for example it should contain function prototypes, not function definitions. This will avoid the multiple function definition problems you had. Similarly, you can define variables that are to be used between modules as extern in the include file: this will allow all modules to be aware that the variable exists and can be linked to, but only one of the source files should then declare the actual instance of the variable.

JR

Message 3 of 5
(5,833 Views)
thanks.   that is quite simple.  

apparently i got too used to programming C with commandline compilers and makefiles, without any debugging window environment.  



 
0 Kudos
Message 4 of 5
(5,801 Views)
...  as well as got used to programming in a sloppy style   😛


0 Kudos
Message 5 of 5
(5,792 Views)