Modules

This is a discussion on Modules within the C Programming forums, part of the General Programming Boards category; I was reading on modules and how you can split up the source code of large programs. But, it didn't ...

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    Modules

    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...

    --Garfield


    PS I comment [stealth] on his new avatar.
    1978 Silver Anniversary Corvette

  2. #2
    Ethereal Raccoon Procyon's Avatar
    Join Date
    Aug 2001
    Posts
    189
    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.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > 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.
    Attached Files Attached Files
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    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...

    --Garfield
    1978 Silver Anniversary Corvette

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Thanks for the zip! I understand the point and how to use the modules. Thanks again.

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-07-2010, 11:08 PM
  2. Sugguestion on choosing my modules
    By ssharish2005 in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-27-2007, 05:44 PM
  3. Multiple Modules
    By rwmarsh in forum C++ Programming
    Replies: 6
    Last Post: 02-18-2006, 03:34 PM
  4. howto add sys call using modules only ?
    By pinkeshzaveri in forum Linux Programming
    Replies: 1
    Last Post: 08-23-2005, 12:28 PM
  5. Problems with modules
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2005, 08:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21