multiple source files

This is a discussion on multiple source files within the C Programming forums, part of the General Programming Boards category; Hello, I've made my first C program. It's quite big, so I decided to divide it into multiple files. The ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    17

    multiple source files

    Hello,

    I've made my first C program. It's quite big, so I decided to divide it into multiple files. The program did work as a whole.
    Some files include more than 1 function.
    Each .c contains the function definitions, and for each .c I've created a .h file with the same name, that contains the function prototypes, the librairies that need to be included, and protection against double inclusion. Each .c only contains one include, which is the related .h (for example, the first line of function.c is #include "function.h")
    I hope I was clear enough as to what I did.
    I don't have a makefile because I don't know how to make it yet.
    When I write gcc -o prog main.c, I get many "undefined reference to '...' "

    Can you help me to make this work please ?

    thanks.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You have to specify all the .c files when you compile it.
    Code:
    gcc -o prog main.c file1.c file2.c

  3. #3
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    Oh ok, thanks.
    Instead of doing that, can't I just write #include "file1.c" and #include "file2.c" in main.c ?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    gcc prog.c func1.c func2.c
    and so on.
    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.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    One way to do this is to simply keep all the files discrete (you do not really need an .h file for each .c file), for example:

    main.c
    Code:
    #include <stdio.h>
    
    int main(void) {
    	int x = test();   /* test() is defined elsewhere */
    	printf("%d\n",x);
    	return 0;
    }
    test.c
    Code:
    int test() {
    	return 5;
    }
    You can now simply go:
    gcc main.c test.c
    This will produce one executable (a.out, since we didn't give it a name) and everything will work hunky-dory, no need for includes.

    My preferred, alternative way if you have a bunch of files is to use one single header file which contains all of the prototypes for all the functions included in any file, and all necessary standard library includes for all files.

    main.h
    Code:
    #include <stdio.h>
    /* prototypes */
    int test();
    Then you include it in the file containing main(), eg.
    Code:
    #include "main.h"
    #include "test.c"
    
    int main(void) {
    This is slightly easier to compile:
    gcc main.c
    but the result is the same.
    Last edited by MK27; 01-24-2010 at 09:13 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > can't I just write #include "file1.c" and #include "file2.c" in main.c ?
    You can, but it's a completely horrible thing to do (and it scales terribly).
    Imagine doing it with 10K files rather than just 5.
    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.

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Salem View Post
    You can, but it's a completely horrible thing to do (and it scales terribly).
    Imagine doing it with 10K files rather than just 5.
    Yeah, I'd rather write them all out on the command line (not)
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    17
    I understand now. Well thanks to all for your help.
    Is there any chance one of you could tell me how to make a 'makefile', or give me a site that explains it for beginners ?

    Yeah, I'd rather write them all out on the command line (not)
    yeah well, is there any other solution than writing them all out on the command line or writing them in main.c ?
    (except using a IDE, which does it for you, right?)
    Last edited by ericad; 01-24-2010 at 09:19 AM.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    I'm sure Google will be able to answer that for you. "makefile tutorial" seems like a good search query.

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Memloop View Post
    I'm sure Google will be able to answer that for you. "makefile tutorial" seems like a good search query.
    IMO the OP is right not to worry about makefiles for now and instead concentrate on learning C. Makefiles serve absolutely no real purpose for beginners or on small projects (beyond adding an extra element to maintain during development) and it will be an easier thing to learn later, when necessary.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    This is a generic makefile that you can use to compile all .c files in one dir into an executable (a.out).

    Code:
    CC = gcc
    DEBUG = -g
    CCFLAGS = $(DEBUG) -ansi -pedantic -Wall -Wextra
    SRC := $(wildcard *.c)
    OBJFILES := $(SRC:.c=.o)
    COMPILE = $(CC) $(CCFLAGS) -c
    EXECUTABLE = a.out
    
    $(EXECUTABLE): $(OBJFILES)
    	$(CC) $(OBJFILES) -o $(EXECUTABLE)
    
    %.o: %.c
    	$(COMPILE) $< -o $@
    
    clean:
    	@ \rm -f $(OBJFILES)
    
    zap:
    	@ \rm -f $(OBJFILES) $(EXECUTABLE)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple source files in one project
    By mintsmike in forum C++ Programming
    Replies: 8
    Last Post: 06-27-2009, 08:20 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  3. multiple source files
    By AmazingRando in forum C Programming
    Replies: 6
    Last Post: 03-13-2005, 03:39 PM
  4. Multiple Source Files!?!?
    By Padawan in forum C Programming
    Replies: 14
    Last Post: 04-04-2004, 12:19 AM
  5. Linking multiple source files: Undefiled Reference to...
    By Inquirer in forum C++ Programming
    Replies: 4
    Last Post: 05-03-2003, 06:47 PM

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