LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

I want to use multiple C files in CVI project. Any tips how to do it?

while trying to use several C files in a single project, I am getting linker errors.
Any user knows the secret how to do it? it's really anoying to work with long source file
 
0 Kudos
Message 1 of 9
(7,602 Views)

Source code in a CVI project can be divided in several C files according to your needs: you must take care of how macros and variables are defined so that your functions can access them.

First of all, every source file must include all h files necessary to compile the functions coded in it. General purpose include files can be #included in all source files (I'm speaking of files like userint.h, ansi_c.h or other general purpose files). Include files related to UIR files must also be added in all files that manage user interface elements so that the compiler and linker can process panels and controls names (that are no more than macros defined in those files).

Next you must define variables in a proper place: those that are to be accessed from functions in more than a source file can be defined in a separate .h file to be included in all sourced files, while variables used only by functions in a single source file can be defined in this c file and are not visible to functions in a different c file.

You may want to tailor your project so that all and only necessary .h files are included in every source file, but a simpler way to avoid multiple processing of include files is to use appropriate headers to manage processing. As an example, userint.h has this structure:

#ifndef USERINT_HEADER
#define USERINT_HEADER

// The rest of the include file lies here

#endif  /* USERINT_HEADER */

This way, the include file is processed only the first time: the first operation is to define USERINT_HEADER macro so that every other inclusion of this file does not determine its processing. If you structure your headers this way, you may simply add all your .h files to all of your source files without causing any overhead to the compiler.



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?
0 Kudos
Message 2 of 9
(7,591 Views)

Roberto, Thanks for your answer. However, all compilers I used before has a linker option to define a list of the source files. Part of them include the "make" utility which compile only the changed files and then compile everything. I don't know how to do it in CVI, and if I include 2nd file in the main file by #include, when I call a function from one file to another I am getting an error message "missing prototype".

Can you explain in detaile how to avoid such errors?

0 Kudos
Message 3 of 9
(7,576 Views)

Ben, you simply must add your several source files to the project using Edit >> Add files to project menu item (you'll see them listed in the project window or in the leftmost column if you are using the integrated view); next you must #include in all source files the appropriate .h files and let the compiler do its work. There is no make file in CVI environment: the IDE itself is in charge of compiling all sources and linking them into an executable.

Be sure to have only one main () function among all sources; it's also better to have only one RunUserInterface () in the project, even if the project will compile without errors if more then one of them is present in all source files: in case you are reusing existing code check this item and cut all additional RunUserInterface statements.

As an example of a multi-source project you can study icnoedit sample shipped with cvi: you'll find it in <cvidir>\samples\apps\iconedit folder



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 4 of 9
(7,566 Views)
In the CVI IDE, make sure you have View>Project Tree enabled.  All the .c files (e.g. "main.c" and "misc_funcs.c", etc.) should be added to your project: Use "Edit>Add Files to Project" to add them.  This is that list of source files you want to specify.
 
Do not use #include to include other .c fails into your main.c file, but you probably should use #include (in the .c files) to include a .h file that contains prototypes for your fuctions as Roberto describes. You should also add that .h file to the project so CVI knows where to find it.
 
Hopefully this is helpful to you. If not, you may have to supply us a simplified description that gives some more details.
 
--Ian
 
 
0 Kudos
Message 5 of 9
(7,565 Views)

Ian, I know that you wrote an answer for me a 2 months ago, but it did not work out. As you wrote, using the #include in the file did not work, as it cause problems with the debugger activity. However, adding the files in the project files list caused errors as undeclared parameters and missing prototypes.

Most of my experiance is with non PC systems (stand alone embedded systems) with virious chips & compilers which has their own rules to build & compile project.

Right now, I am working with multiple files with another editor, and I run a batch file which chain all the file to 1 big file in order to run it under CVI compiler.

Please advise in detailes, as I have big projects with multiple communication channels, and I can't wirk with 1 long file.

 

0 Kudos
Message 6 of 9
(7,424 Views)

Ben, I can certainly agree that working with one huge file is not satisfactory.  My first recommendation (if you have not already done it) is to take a look at the example program Roberto suggested ( ...CVI80\samples\apps\iconedit.prj). Note how the .h ("include") files contain both variable declarations and prototypes. They are included in sources files (.c) that need them.  The errors about you were getting are almost certainly due to not putting those prototype and variables into an include file and then including it in all source files that need it with (for example) "#include protos.h" and "#include declar.h"

Keep in mind that by breaking your huge file into several source files, you'll need to pay attention to the "scope" of the variables and functions. Specifically variables and prototypes at the top of that huge file will no longer be visible in separate .c files you break out.  You may also need to look up the "static" keyword in a C book.

The "problems with debugger activity" are not clear to me. If you can be more specific perhaps someone can help. 

If your batch file that "chains" your current source files together just concatentates the files before compilation, it is effectively still a one-source-file program.  Actually breaking the program into sensible modules in different files for use in an IDE like CVI is quite different, and requires attention to global vs local variables etc.

Hope this helps!

 

0 Kudos
Message 7 of 9
(7,409 Views)

Thanks Ian, I'll follow your tips and recheck Roberto suggested example.

About the debugger problems: when I chained the source files with #include, the breakpint function that are not in the 1st source file did not work.

Thanks

0 Kudos
Message 8 of 9
(7,400 Views)

Hmm, there's a clue in your post: although it is legal c syntax, it is not a good idea to #include .c files inside other .c files. #include should only be used for header .h files. There are a few tips in this post.

JR

0 Kudos
Message 9 of 9
(7,396 Views)