Thread: Separating out code into multiple files

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    144

    Separating out code into multiple files

    Hi. I have a source file that's 1700 lines long. Attached to it is a header file that's only 25 lines long. The interface is designed to be as simple as possible, though the implementation of what goes on underneath the surface is quite complex.

    1700 lines is getting a bit unmanageable. The code uses two internal data structures, each of which come with a set of functions designed to work on the data. I'd like to separate the structures and functions into their own source files.

    Yet, those types will only be used by this original source file. For me to declare all the functions and structures in such a way that they can be accessed by all other translation units ruins my concept of keeping the interface as simple as possible and keeping data and functions as private as possible. So what do I do? Do I do this in the .c file:

    Code:
    #include "Morestuff.c"
    TIA, Richard

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Including a "c" file is normally considered very poor programing.

    I would put the structures into one or more header (.h) files.
    I would put the function prototypes into one or more header (.h) files.
    Then break the function bodies into multiple .c files that are then compiled into object files linked together by the linker.
    Note, you might wish to create an static or dynamic library from the object files.

    Tim S.
    Last edited by stahta01; 04-24-2011 at 04:35 PM. Reason: Added note

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    First of all 1700 lines is not unheard of for C source files...

    Your best bet is to break it out functionally... for example: User interface in one file, file access in another, networking in a third and so on.
    Then for each file make a header with function prototypes and any extern variables and typedefs you need. The headers (.h) would then be included into your main, and other files as needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Breaking up source code into multiple files
    By mherald81 in forum C Programming
    Replies: 9
    Last Post: 10-15-2010, 04:55 PM
  2. Replies: 13
    Last Post: 08-12-2010, 01:58 PM
  3. separating functions into diff files
    By cuizy in forum C Programming
    Replies: 1
    Last Post: 04-29-2009, 03:50 PM
  4. separating files for classes and thier implemenation.
    By steve1_rm in forum C++ Programming
    Replies: 18
    Last Post: 11-06-2008, 01:49 PM
  5. Splitting Code Into Multiple Files
    By pobri19 in forum C++ Programming
    Replies: 3
    Last Post: 09-15-2008, 04:21 AM