![]() |
| | #1 |
| Registered User Join Date: Dec 2009
Posts: 17
| multiple source files 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 | |
| | #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 | |
| | #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 | |
| | #4 |
| and the hat of Destiny Join Date: Aug 2001 Location: The edge of the known universe
Posts: 22,495
| gcc prog.c func1.c func2.c and so on. |
| Salem is offline | |
| | #5 |
| dat is, vast staat 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;
}
Code: int test() {
return 5;
}
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(); Code: #include "main.h"
#include "test.c"
int main(void) {
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 | |
| | #6 |
| and the hat of Destiny 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. |
| Salem is offline | |
| | #7 | |
| dat is, vast staat Join Date: Jul 2008 Location: SE Queens
Posts: 6,612
| Quote:
(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 | |
| | #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:
(except using a IDE, which does it for you, right?) Last edited by ericad; 01-24-2010 at 09:19 AM. | |
| ericad is offline | |
| | #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 | |
| | #10 |
| dat is, vast staat Join Date: Jul 2008 Location: SE Queens
Posts: 6,612
| 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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |