05-28-2006 06:28 PM
05-29-2006 02:07 AM
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.
05-29-2006 01:33 PM
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?
05-29-2006 02:12 PM
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
05-29-2006 02:25 PM
07-27-2006 10:08 AM
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.
07-27-2006 10:33 AM
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!
07-27-2006 11:00 AM
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
07-27-2006 11:29 AM
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