LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Cleaning up code - .h vs .c

You're starting to get to the point where you need to know a bit more about how C projects are turned into executables. 
Take a look at Compiling C programs from a Caltech CS course.
----
I am the founder of CnCSoftwareSolutions. When not cleaning up baby drool, I write about test data or work on Vision, a tool for understanding your test data. Visit me at www.cncsoftwaresolutions.com
0 Kudos
Message 11 of 14
(1,096 Views)
Okay - showing my lack of C declaration scopes then...

If I have a number of organised .c files, each containing instrument-specific functions (e.g. DIO.c, Analogue.c, Serial.c), which are all included in a single project, but which all need to cross-reference each other, can I stick all the function declarations in a single header file, and #include that header in all the .c files (hence informing all the .c files of every function)?  This would hence work in the same way as having a GlobalVariables.h (defined with the extern keyword) included in each .c file, yes?
__________________________________________
The world is full of exciting challenges,
brilliantly disguised as insoluble problems.
0 Kudos
Message 12 of 14
(1,079 Views)

Yes. Although it is more usual to have a .h file containing just the declarations used within the corresponding .c file. You could still then have an overall .h file which incorporated further #includes for each such .h file (together with other common declarations, typedefs etc that your project might use) , which is then #included by all your c files.

(This is why programmers like C, and managers hate it: the language is so flexible and powerful that it is difficult to pin down a single 'best practice' approach and get everyone to use it.)

JR

0 Kudos
Message 13 of 14
(1,072 Views)
Yes, that's possible.  NI does something similar with ansi_c.h.  This is just a convenient wrapper for all the standard ansi header files.

If I understand you correctly, it sounds to me like you're having a problem with coupling in your .c files though.  Does Serial.c really need to use functions in DIO.c?  Does DIO.c really need to use functions in Serial.c?  Why?  Can these functions be pulled out into a lower level module?
----
I am the founder of CnCSoftwareSolutions. When not cleaning up baby drool, I write about test data or work on Vision, a tool for understanding your test data. Visit me at www.cncsoftwaresolutions.com
0 Kudos
Message 14 of 14
(1,064 Views)