LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   using multiple source code files (http://www.linuxquestions.org/questions/programming-9/using-multiple-source-code-files-606272/)

hoshangi 12-12-2007 07:50 AM

using multiple source code files
 
Hi
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 08: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 08:43 AM

thanks
 
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()
{
a();
b();
return 0;
}


please tell me how i link them with other

thanks

matthewg42 12-12-2007 09: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:
Code:

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:
Code:

%.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.
Code:

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 09:25 AM

thanks
 
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 10:43 AM

man gcc

search for a compile option in that manpage

matthewg42 12-12-2007 11: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:

Code:

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

int func_one(void);

#endif

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

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

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

Code:

#include "1.h"

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


hoshangi 12-12-2007 02:07 PM

ok
 
thanks my friend


All times are GMT -5. The time now is 06:36 PM.