-   Programming (
-   -   using multiple source code files (

hoshangi 12-12-2007 06:50 AM

using multiple source code files
I want to use multiple source code file with make

but i don't know how
if you have any sample or a book please give me

thanks my friends

matthewg42 12-12-2007 07:16 AM

What you ask is essentially, "tell me how to use make", so you must forgive me if this answer is also very general... I would recommend that you read the GNU Make manual. It contains many examples.

hoshangi 12-12-2007 07:43 AM

for example i have 3.c 2.c and 1.c

the a() function locate in 3.c
the b() function locate in 2.c

and in 3.c:

int main()
return 0;

please tell me how i link them with other


matthewg42 12-12-2007 08:05 AM

You should invoke gcc first on each .c file with the -c option. This means "compile to object file" - i.e. omit the linking stage. Once you have an object file for each .c file, you can link them together:

gcc -c 1.c -o 1.o
gcc -c 2.c -o 2.o
gcc -c 3.c -o 3.o
gcc 1.o 2.o 3.o -o myprogram

You can write a Makefile which includes wildcard rules like this:

%.o : %.c
        gcc -c -o $@ $<

This means that all targets which end in .o will depend on a like-named file but ending in .c. In the command $@ means the target name, $< means the left most dependency name.

A simple Makefile which will generate the gcc commands above might look like this. Note that that the 8 spaces before commands should be a TAB in your Makefile - NOT spaces.

myprogram : 1.o 2.o 3.o
        gcc -o myprogram 1.o 2.o 3.o

%.o : %.c
        gcc -c -o $@ $<

Note that you don't need to use make at all - you can invoke all the gcc commands manually if you prefer. Make is nice because it will work out which files have been modified since the last build and re-compile and re-link only what needs to be re-compiled and re-linked. Thus as your projects become larger, make becomes more valuable, but for trivial examples like the one you describe, it's probably not worth the effort.

hoshangi 12-12-2007 08:25 AM

when i compile 1.c the gcc has errored that tha a() and b() not define
or please define your function
while a() is in the 3.c and b() is in the 2.c

what i can to do when compiling 1.c

orgcandman 12-12-2007 09:43 AM

man gcc

search for a compile option in that manpage

matthewg42 12-12-2007 10:04 AM

You need to have a function declaration in a header file which should be included in the file where they are used. You should use the pre-processor to prevent an include file from being read more than once.

So you would make a header file for 1.c, like this:


/* this is 1.h */
#ifndef F1_H
#define F1_H 1

int func_one(void);


And then you should include that wherever you refer to func_one:

/* this is 1.c */
#include "1.h"

int func_one(void) {
    /* the implementation of func_one goes here */


#include "1.h"

int main(int argc, char** argv) {
        return 0;

hoshangi 12-12-2007 01:07 PM

thanks my friend

All times are GMT -5. The time now is 09:10 AM.