LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-12-2007, 06:50 AM   #1
hoshangi
Member
 
Registered: Nov 2007
Posts: 88

Rep: Reputation: 15
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
 
Old 12-12-2007, 07:16 AM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 12-12-2007, 07:43 AM   #3
hoshangi
Member
 
Registered: Nov 2007
Posts: 88

Original Poster
Rep: Reputation: 15
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
 
Old 12-12-2007, 08:05 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.

Last edited by matthewg42; 12-12-2007 at 08:08 AM.
 
Old 12-12-2007, 08:25 AM   #5
hoshangi
Member
 
Registered: Nov 2007
Posts: 88

Original Poster
Rep: Reputation: 15
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
 
Old 12-12-2007, 09:43 AM   #6
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
man gcc

search for a compile option in that manpage
 
Old 12-12-2007, 10:04 AM   #7
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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;
}
 
Old 12-12-2007, 01:07 PM   #8
hoshangi
Member
 
Registered: Nov 2007
Posts: 88

Original Poster
Rep: Reputation: 15
ok

thanks my friend
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
multiple C source files howto introuble Programming 2 07-31-2005 07:49 PM
Source Code Files NightKids Linux - Newbie 1 06-05-2005 07:07 PM
Need source code for making ISO files neo_in_matrix Programming 1 05-11-2005 10:01 PM
Using g++ to compile multiple source files arnab_be Linux - Newbie 1 03-03-2004 02:21 PM
I need help using multiple source files CodeWarrior Programming 4 12-30-2003 04:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration