LinuxQuestions.org
Help answer threads with 0 replies.
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 07-24-2007, 11:48 PM   #1
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Rep: Reputation: 32
doubt with Makefile


Hi all,
i know how to write a Makefile for the below case,
:with more than one .c file in the same directory but only one main() function .

can we write a Makefile such that it should compile and link more than one .c (each .c file containing main() function) file in the same directory ???
someone help me please...
thanks..
 
Old 07-25-2007, 12:50 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
Sure.

Code:
all: alpha beta gamma

alpha: alpha.c

beta: beta.c

gamma: gamma.c
Hope this helps.
 
Old 07-25-2007, 12:52 AM   #3
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Rep: Reputation: 30
Quote:
Originally Posted by culin
Hi all,
i know how to write a Makefile for the below case,
:with more than one .c file in the same directory but only one main() function .

can we write a Makefile such that it should compile and link more than one .c (each .c file containing main() function) file in the same directory ???
someone help me please...
thanks..
i know only for a main()..
this the make file for one.c
Code:
# basic GTK+ app makefile
SOURCES = one.c
OBJS    = ${SOURCES:.c=.o}
CFLAGS  = `pkg-config gtk+-2.0 --cflags`
LDADD   = `pkg-config gtk+-2.0 --libs`
CC      = gcc
PACKAGE = one

all : ${OBJS}
  ${CC} -o ${PACKAGE} ${OBJS} ${LDADD}

.c.o:
  ${CC} ${CFLAGS} -c $<

# end of file
cheers
munna
 
Old 07-25-2007, 02:00 AM   #4
gd2shoe
Member
 
Registered: Jun 2004
Location: Northern CA
Distribution: Debian
Posts: 835

Rep: Reputation: 49
Quote:
Originally Posted by culin
...
can we write a Makefile such that it should compile and link more than one .c (each .c file containing main() function)...
Uhh, not to my understanding. The issue I think exist has nothing to do with make, but with C. If I understand properly, you cannot link two pieces of code together that both have a "main()" function. How could it? Which function would the symbol table say was "main()" after they were linked? Which would you expect to be run if the user ran your application?

Let's assume for a moment that I misunderstand you, and you really meant to say that you have several programs, each with their own source file, in the same folder; also that you want to have one makefile for all of them. Now that can be done. I could be wrong, but here's what I'd try:


Code:
all : one two three

one : one.o common.o
    gcc -o one one.o common.o
two : two.o common.o
    gcc -o two two.o common.o
three : three.o common.o
    gcc -o three three.o common.o

one.o: one.c one.h common.c
    gcc -c one.c common.c -o one.o
two.o: two.c two.h common.c
    gcc -c two.c common.c -o two.o
three.o: three.c three.h common.c
    gcc -c three.c common.c -o three.o
common.o : common.c
    gcc -c common.c -o common.o
Now this is obviously a bad example (I can think of several reasons), but it should get the point across. You have the "target" (usually the file to be created) followed by a colon and the files that it depends upon. Then the lines that follow describe how to create the target file. There are all kinds of tricks that can be used in makefiles, but the basic idea is always the same.
 
Old 07-25-2007, 03:31 AM   #5
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
thanks all for the reply..
i will make my question more specific....
say for eg: there are 3 .c files (all .c files in the same directory and the Makefile also in the same directory.)
a.c
Code:
#include<stdio.h>
void hello(void);
int main()
{
        printf(" hello how are u \n");
        hello();
        return 0;
}
b.c
Code:
void hello2(void);
void hello(void)
{
        printf(" hello how are u 111\n");
        hello2();
}
c.c
Code:
void hello2(void)
{
        printf(" hello how are u 222\n");
}
and my Makefile is like this..
Code:
RM = /bin/rm -f
PROG = prog
objects := $(patsubst %.c,%.o,$(wildcard *.c))

executablename : $(objects)
        gcc -o executablename $(objects)
        @echo "hello how are u"
clean:
        $(RM) $(PROG) $(objects)
with this make file it will generate an executable named executablename
and if we run ./executablename output is the print statements of all the 3 .c files..
now whats my question is
if say
a.c
Code:
#include<stdio.h>
void hello(void);
int main()
{
        printf(" hello how are u \n");
        return 0;
}
b.c
Code:
int main()
{
        printf(" hello how are u 111\n");
        return 0;
}
c.c
Code:
int main()
{
        printf(" hello how are u 222\n");
        return 0;
}
in this context CAN I WRITE A Makefile (may be with 3 executables,i dunno exactly)???
does that have any sense ???
and if yes how ??
Quote:
Originally Posted by wjevans_7d1@yahoo.co
all: alpha beta gamma

alpha: alpha.c

beta: beta.c

gamma: gamma.c
sorry i didnt get ???
thanks again....

Last edited by culin; 07-25-2007 at 03:34 AM.
 
Old 07-25-2007, 03:58 AM   #6
gd2shoe
Member
 
Registered: Jun 2004
Location: Northern CA
Distribution: Debian
Posts: 835

Rep: Reputation: 49
This isn't a makefile question, but a compiler question. I'm not a C guru, but I'd say no. I don't think it's possible (not as a single executable, anyway).

Remember, make doesn't compile anything. A makefile doesn't link anything. All it does is contain instructions to be passed along to the real compiler and linker. If the compiler/linker cannot do something, then make cannot force them to.

If you are intent on finding out if it is truly possible or impossible, stop investigating how to force make to do it. Instead, start looking at your compiler (probably the gcc).

Or better yet, use only one function called "main()" throughout your project. Why would you want more than one anyway?
 
Old 07-25-2007, 04:27 AM   #7
culin
Member
 
Registered: Sep 2006
Distribution: Fedora Core 10
Posts: 254

Original Poster
Rep: Reputation: 32
oh.. thanks for the info gd2shoe,
but can u please gimme some info about how does the make work when we run make or make install or make uImage form the kernel source directory....
will all those source files will be in different directory and each Makefile in all the individual directories ?? so that when we does a make from kernel source it invokes the make files in those directories ??
sorry if i am wrong...
 
  


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
Makefile issue: "Makefile.src: File not found" m3rkury Linux - Software 1 02-22-2007 10:15 PM
automake, makefile, makefile.in and makefile.am Fond_of_Opensource Linux - Newbie 1 09-12-2006 08:35 PM
what is the meaning of Makefile,Makefile.am,Makefile.in cynthia_thomas Linux - General 3 12-08-2005 05:00 AM
how to get (makefile -f makefile )output into the textview widget in Pygtk sailu_mvn Programming 3 02-28-2005 03:57 AM
generate Makefile from Makefile.in without calling ./configure ? chris78 Programming 2 05-02-2004 12:23 PM

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

All times are GMT -5. The time now is 08:49 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