C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 01-24-2010, 08:45 AM   #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.
ericad is offline   Reply With Quote
Old 01-24-2010, 09:06 AM   #2
Robot
 
Join Date: Mar 2009
Posts: 372
You have to specify all the .c files when you compile it.
Code:
gcc -o prog main.c file1.c file2.c
Memloop is offline   Reply With Quote
Old 01-24-2010, 09:08 AM   #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 ?
ericad is offline   Reply With Quote
Old 01-24-2010, 09:09 AM   #4
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
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.

Salem is offline   Reply With Quote
Old 01-24-2010, 09:09 AM   #5
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
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.
__________________
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
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge

Last edited by MK27; 01-24-2010 at 09:13 AM.
MK27 is offline   Reply With Quote
Old 01-24-2010, 09:09 AM   #6
and the hat of Destiny
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 22,495
> 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.

Salem is offline   Reply With Quote
Old 01-24-2010, 09:15 AM   #7
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
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
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge
MK27 is offline   Reply With Quote
Old 01-24-2010, 09:15 AM   #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 ?

Quote:
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.
ericad is offline   Reply With Quote
Old 01-24-2010, 09:16 AM   #9
Robot
 
Join Date: Mar 2009
Posts: 372
I'm sure Google will be able to answer that for you. "makefile tutorial" seems like a good search query.
Memloop is offline   Reply With Quote
Old 01-24-2010, 09:18 AM   #10
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
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
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge
MK27 is offline   Reply With Quote
Old 01-24-2010, 09:24 AM   #11
Robot
 
Join Date: Mar 2009
Posts: 372
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)
Memloop is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 12:10 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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