Thread: Working with multiple source files

  1. #1
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321

    Talking Working with multiple source files

    I've seen you all working with multiple source files and I want to do it too!

    I have

    main.c
    function1.c
    function2.c

    Now how do I link them together? I mean do I have to add something in main.c to include the other two files or what and how to do that? I am using command line to compile.
    Someone told me to do
    Code:
    gcc main.c function1.c function2.c -o executable
    And some other random guy told me to

    Code:
    #include "function1.c"
    #include "function2.c"
    in main.c But both give me 200 errors (probably some of them are syntax errors)

    Anyway, which is the correct way to do this?

    Thanks

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > And some other random guy told me to...
    No

    Put the prototypes of the functions you want to use from function1.c into function1.h (Or whatever) and include that in main.c

    ie
    Code:
    #include "function1.h"
    
    int main(void)
    {
        hello_world();
        return 0;
    }
    function1.h
    Code:
    void hello_world(void);
    function1.c
    Code:
    #include <stdio.h>
    
    void hello_world(void)
    {
        printf("Hello world\n");
    }
    Code:
    gcc main.c function1.c -o executable
    Note: don't compile the header files

    Also you may want to google "Inclusion guards"

  3. #3
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    You can also use global variables from other source files by using the keyword "extern"

    For example if function1.c looks like this:
    Code:
    int function1Test = 0;
    and you want to access the variable "function1Test" from another .c file than in function1.h put
    Code:
    extern int function1Test;
    main.c
    Code:
    #include "function1.h"
    
    int main()
    {
      function1Test = 32;
      return 0;
    }

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes but I wouldn't recommend abusing them
    Last edited by zacs7; 05-28-2008 at 08:02 AM. Reason: It's not opposite day is it?

  5. #5
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by zacs7 View Post
    function1.h
    Code:
    void hello_world(void);
    function1.c
    Can I put both function prototypes in 1 header file or I have to make n number of header files for n number of functions?

    Quote Originally Posted by 39ster View Post
    You can also use global variables from other source files by using the keyword "extern"

    Yeah thanks, I think that will fix those 200 errors

  6. #6
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by abh!shek View Post
    Can I put both function prototypes in 1 header file or I have to make n number of header files for n number of functions?




    Yeah thanks, I think that will fix those 200 errors
    You can but it's better to have one header for each .c file. It's more organised.

  7. #7
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Thanks zacs7 and 39ster

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just don't forget to compile and link each of the source files, or you will (likely) get errors!
    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.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by 39ster View Post
    You can but it's better to have one header for each .c file. It's more organised.
    Although sometimes it makes sense to have one header file for a bunch of source files that provide some particular functionality. E.g. you may have "jpeg.h" for the source files "jpegreadwrite.c", "jpegcompression.c", "jpeginfo.c", "jpegdrawing.c".

    This particularly make sense if your jpeg*.c files become a "libjpeg" library.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    Quote Originally Posted by Elysia View Post
    Just don't forget to compile and link each of the source files, or you will (likely) get errors!
    lolz I _am getting_ errors

    I don't know how to link or anything. I just know I have to do gcc sourcefile1.c source2.c -o executable

    How do I link?

    I'm getting errors like

    * printf, scanf undeclared in function1
    * The macros(?) I defined with #define are undeclared and stuff like that!

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Command lines are beyond me.
    The errors you get suggests that you have failed to link with the standard library for the compiler.
    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.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by abh!shek View Post
    lolz I _am getting_ errors

    I don't know how to link or anything. I just know I have to do gcc sourcefile1.c source2.c -o executable

    How do I link?

    I'm getting errors like

    * printf, scanf undeclared in function1
    * The macros(?) I defined with #define are undeclared and stuff like that!
    To fix your problems with printf/scanf, you need to include <stdio.h> in the file that uses those functions. Likewise, if you have #define, they need to be in an include file that is included by the file that uses the #define.

    To separately compile, use individual file compilation:
    Code:
    gcc -c somefile.c
    gcc -c otherfile.c
    then link with:
    Code:
    gcc -o executable somefile.o otherfile.o
    You should probably also use
    Code:
    gcc -Wall ...
    as that will give you warnings whenever you do something that may be a bit wrong.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Quote Originally Posted by matsp View Post
    Although sometimes it makes sense to have one header file for a bunch of source files that provide some particular functionality. E.g. you may have "jpeg.h" for the source files "jpegreadwrite.c", "jpegcompression.c", "jpeginfo.c", "jpegdrawing.c".

    This particularly make sense if your jpeg*.c files become a "libjpeg" library.

    --
    Mats
    Yeah i like to use 1 header for libs and dlls

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Putting it all in one header may also bloat it.
    So plan carefully.
    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.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Putting it all in one header may also bloat it.
    So plan carefully.
    Sure - there's all sorts of different reasons to have different solutions - I was just trying to point out that sometimes it's not the ideal solution to have one header for each source file - it often is, but not always.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Splitting source into multiple files(Linux & make)
    By IceDane in forum C Programming
    Replies: 6
    Last Post: 05-18-2009, 07:31 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. WM_COPYDATA and mutex selecting multiple files
    By gh0st in forum Windows Programming
    Replies: 2
    Last Post: 10-27-2006, 02:22 PM
  4. multiple source files
    By AmazingRando in forum C Programming
    Replies: 6
    Last Post: 03-13-2005, 03:39 PM
  5. Multiple Source Files!?!?
    By Padawan in forum C Programming
    Replies: 14
    Last Post: 04-04-2004, 12:19 AM

Tags for this Thread