# Thread: multiple source files

1. ## 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. You have to specify all the .c files when you compile it.
Code:
gcc -o prog main.c file1.c file2.c

3. Oh ok, thanks.
Instead of doing that, can't I just write #include "file1.c" and #include "file2.c" in main.c ?

4. gcc prog.c func1.c func2.c
and so on.

5. 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.

6. > 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.

7. Originally Posted by Salem
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)

8. 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?)

9. I'm sure Google will be able to answer that for you. "makefile tutorial" seems like a good search query.

10. Originally Posted by Memloop
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.

11. 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