Thread: How to create a C project with multiple source files

  1. #1
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39

    How to create a C project with multiple source files

    Hi All,

    How do I create a project using multiple source files?

    I understand the concept of including header files but I don't really know how to combine other source files.

    Here is what I tried (source files attached).

    * Create a new source file (newSource.c)
    * Place newSource.c in same directory as main.c
    * Define structure's, functions and function prototypes in newSource.c
    * Call newSource.c functions from main.c
    * Create variables using structures from newSource.c

    Here is what I got
    C:\Program Files\CodeBlocks\CCNumber\main.c||In function `main':|
    C:\Program Files\CodeBlocks\CCNumber\main.c|7|error: `CustomerRecord' undeclared (first use in this function)|
    C:\Program Files\CodeBlocks\CCNumber\main.c|7|error: (Each undeclared identifier is reported only once|
    C:\Program Files\CodeBlocks\CCNumber\main.c|7|error: for each function it appears in.)|
    C:\Program Files\CodeBlocks\CCNumber\main.c|7|error: syntax error before "customer"|
    C:\Program Files\CodeBlocks\CCNumber\main.c|9|warning: implicit declaration of function `strcpy'|
    C:\Program Files\CodeBlocks\CCNumber\main.c|9|error: `customer' undeclared (first use in this function)|
    C:\Program Files\CodeBlocks\CCNumber\main.c|13|warning: implicit declaration of function `print_customer'|
    ||=== Build finished: 5 errors, 2 warnings ===|

    Do I understand this concept correctly and just need to learn how to configure my IDE? I'm using Code::Blocks.

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Try something along these lines:

    main.c
    Code:
    #include <stdio.h>
    #include "other.h"
    
    int main (void)
    {
        printf("%d\n", getfavoritenumber());
    
        return 0;
    }
    other.h
    Code:
    #ifndef _OTHER_H_
    #define _OTHER_H_
    
    int getfavoritenumber(void);
    
    #endif
    other.c
    Code:
    #include "other.h"
    
    int getfavoritenumber(void)
    {
        return 3;
    }
    Just thought an example would be beneficial.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    That was helpful. Thank you. Can you explain the preprocessor directives in other.h?

  4. #4
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    That is what is called an inclusion guard. You may somewhere in your project, if it gets big, include the same header file multiple times. What it checks for is it a variable is defined, and if not, go ahead and continue, otherwise, stop dead in its tracks.

    It is good practice to put inclusion guards in your header files, or else you may end up with some errors.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    To elaborate, when a header gets included twice or more, its contents is added to the source files more than once. And when the compiler encounters several type of the same declarations, it will spit out an error.
    Therefore, the inclusion guards make the the contents of the file gets included into a file only once.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Feb 2009
    Location
    Seattle
    Posts
    39
    Thank you very much carrotcake1029 and Elysia!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Class Inheritance over multiple source files
    By Swarvy in forum C++ Programming
    Replies: 7
    Last Post: 11-11-2008, 10:03 AM
  2. Multiple Source Files, make files, scope, include
    By thetinman in forum C++ Programming
    Replies: 13
    Last Post: 11-05-2008, 11:37 PM
  3. Quick Question on Multiple Source Files
    By dac in forum C++ Programming
    Replies: 24
    Last Post: 12-01-2006, 03:53 PM
  4. add source files to embedded VC 4.0
    By George2 in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2006, 03:28 AM
  5. Multiple source files for one program
    By gflores in forum C++ Programming
    Replies: 3
    Last Post: 08-15-2004, 02:32 AM

Tags for this Thread