Thread: How to split a C program into several files?

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    130

    How to split a C program into several files?

    Hello, I'm trying to split my prime C's file into several files.c and each file has specific function from my prime C's file code, in other words would do something like dividing my a code into several files because it's so big and long in one file!...
    How exactly can I do that? can you explain to me by an example of how to make those files? I really searched in google but all were useless.


    thanks
    Last edited by Romyo2; 06-20-2015 at 05:53 PM.

  2. #2
    Registered User migf1's Avatar
    Join Date
    May 2013
    Location
    Athens, Greece
    Posts
    385
    Quote Originally Posted by Romyo2 View Post
    Hello, I'm trying to split my prime C's file into several files.c and each file has specific function from my prime C's file code, in other words would do something like dividing my a code into several files because it's so big and long in one file!...
    How exactly can I do that? can you explain to me by an example of how to make those files? I really searched in google but all were useless.


    thanks
    By googling "ansi c source code modularization" the 1st result I got was this one, and the second result was this one.

    I believe the latter is fine to get you started, while the former will give you initiatives to master the topic.
    "Talk is cheap, show me the code" - Linus Torvalds

  3. #3
    Registered User
    Join Date
    May 2015
    Posts
    130
    Quote Originally Posted by migf1 View Post
    By googling "ansi c source code modularization" the 1st result I got was this one, and the second result was this one.

    I believe the latter is fine to get you started, while the former will give you initiatives to master the topic.
    lets say I've split the code into two files
    Code:
    function1.c
    which has a function1 as
    Code:
    void function1();
    {
              /*whatever*/
    {
    in the other file that called
    Code:
    function2.c
    which has a function2 as
    Code:
    void function2()
    {
                 /*whatever*/
    {
    I have also a H header called "helper.h", if I want to apply the helper.h onto the divided files I must write
    Code:
    include "helper.h"
    , but Idk why it gives too many errors occurred...

    Can you please show me and give me a hint of how to split exactly a code into files with header file? I'm not meaning to write me a code for, just make it as general..doesn't matter what codes you would write for splitting.

    thanks.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    You need to understand these terms/ideas:
    function prototypes https://en.wikipedia.org/wiki/Function_prototype
    the difference between defination and declaration https://en.wikipedia.org/wiki/Declar...programming%29
    header guards https://en.wikipedia.org/wiki/Include_guard

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    A simple yet complete example can be found on post #2 here: How to create a C project with multiple source files

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. split strings (and split thread)
    By mahi in forum C Programming
    Replies: 1
    Last Post: 10-31-2011, 06:56 AM
  2. split pdf files with ghostscript API
    By zoidberg in forum C++ Programming
    Replies: 1
    Last Post: 07-08-2010, 04:52 PM
  3. Replies: 0
    Last Post: 03-09-2009, 10:15 PM
  4. how to split long programe in small files
    By umeshjaviya in forum C Programming
    Replies: 11
    Last Post: 04-15-2008, 02:45 AM
  5. HTML page split into files in c
    By Munisamy in forum C Programming
    Replies: 2
    Last Post: 02-21-2005, 05:58 AM