I was reading on modules and how you can split up the source code of large programs. But, it didn't tell me how to do it. What kind of file do you include? Just a .c and .h? Where do you have to save these files to "get" them in the "main" source code? Thanks...
PS I comment [stealth] on his new avatar.
There's a thread on this on the C++ board too.
Basically, just save a .h file containing only the function prototypes and global variable declarations, and a separate .cc file of the same name containing the function implementations. Include only the .h file from your main program using #include "filename.h" (use quotes, not brackets, to tell it to search the current directory and not the compiler's library of include files); and depending on your compiler you'll have to find a way to tell it to link the .cc code. Many compilers have a project manager that lets you add .cc files that it will automatically link to when you make the program.
> But, it didn't tell me how to do it
Ok, lets give this a go. Say you want to create a separate module of your linked list routines which presently exist in main.c
The zip file shows how the linked list functions have been moved to another module, and an interface .h file created to allow other modules (main.c) access to that interface.
1. Identify the natural boundaries within your code. You a looking for functions which can be grouped together for some reason. For example, linked list insert, append, search, delete routines can all be grouped in this manner.
When developing large programs from scratch, this partioning is something which falls naturally out of the design process.
2. Move the functions identified in step 1 to another source (.c) file, and name it appropriately (eg. list.c).
At this time, you also need to create a list.h file, which describes the interface to the functions in the list.c file. The .h file contains things like the linked list structure, and the function prototypes.
main.c and list.c need to have #include "list.h"
> Where do you have to save these files to "get" them in the "main" source code?
Depends on your compiler / IDE
For unix/linux/command line compilers, the normal approach is to use a makefile.
For say VC++, create a project and add the two .c files (main.c and list.c) to the list of source files.
Most things are smart enough that once you have fixed list.h and list.c, they never get recompiled (list.obj will be up to date). In a large project of many files, only recompiling the one you're editing is much faster.
Okay, I'm not sure if I'm understanding such of an easy idea. So, you group relative functions in a .c file and then have global declarations and prototypes in a .h file. So, in the main source and the secondary source, I have to include the .h file? Then the main source will go right through to the secondary .c file?
I'm about to download the zip file. Thanks...
Thanks for the zip! I understand the point and how to use the modules. Thanks again.