Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Makefile with source code in 2 directory

Reply
Thread Tools

Makefile with source code in 2 directory

 
 
Rudra Banerjee
Guest
Posts: n/a
 
      09-05-2012
Dear friends,
my project has structure like
$ls new
files Makefile help.c table.c callbk_list.h cwlst.h
where files is a directory, with hundreds of C code, which are the functions in the main code table.c

I tried to compile with Makefile as:
CC=gcc
CFLAGS =-O3
SUBDIR=files
PCONF1=pkg-config --cflags --libs gtk+-2.0
objects := $(wildcard $(SUBDIR)/*.o)
ptbl: $(objects) help.c table.c callbk_list.h cwlst.h
$(CC) `$(PCONF1)` $(objects) help.c table.c -o ptbl

$(SUBDIR)/%.o: $(SUBDIR)/%.c
$(CC) -c `$(PCONF1)` $<

which is not compiling the $(SUBDIR)/%.c files at all, with make giving error:

$ make
gcc `pkg-config --cflags --libs gtk+-2.0` help.c table.c -o ptbl
/tmp/ccOKcWuf.o: In function `main':
table.c.text+0xa9a): undefined reference to `callback_A'
table.c.text+0xb3a): undefined reference to `callback_B'
.....
and so on

but if I try to compile directly,
gcc -g table.c help.c files/prop_*.c `pkg-config --cflags --libs ^Ck+-2.0` -o ptbl
its working fine.
Can anybody please show me the error?
 
Reply With Quote
 
 
 
 
Rudra Banerjee
Guest
Posts: n/a
 
      09-05-2012

gcc -g table.c help.c files/prop_*.c `pkg-config --cflags --libs gtk+-2.0` -o ptbl
 
Reply With Quote
 
 
 
 
Casey Carter
Guest
Posts: n/a
 
      09-05-2012
On 2012-09-04 23:45, Rudra Banerjee wrote:
> objects := $(wildcard $(SUBDIR)/*.o)


$(wildcard $(SUBDIR)/*.o) will be empty unless the object files are
compiled already. Try:

sources := $(wildcard $(SUBDIR)/*.c)
objects := $(sources:.c=.o)

 
Reply With Quote
 
Rudra Banerjee
Guest
Posts: n/a
 
      09-05-2012

>
>
> sources := $(wildcard $(SUBDIR)/*.c)
>
> objects := $(sources:.c=.o)


Thanks,
Its working.
Though it was creating the .o files of SUBDIR source in maindir.
so I did:
exe=ptbl
CC=gcc
CFLAGS =-O3
SUBDIR=files
PCONF1=pkg-config --cflags --libs gtk+-2.0
sources := $(wildcard $(SUBDIR)/*.c)
objects := $(sources:.c=.o)
$(exe): $(objects) help.c table.c callbk_list.h cwlst.h
$(CC) `$(PCONF1)` $(objects) help.c table.c -o $(exe)

$(SUBDIR)/%.o: $(SUBDIR)/%.c
$(CC) -c `$(PCONF1)` $<
mv $(*F).o $(SUBDIR)/$(*D)

and moved the files back to SUBDIR.
is there any way to create the .o of SUBDIR/*.c in SUBDIR only?
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      09-05-2012
On Wed, 2012-09-05, bartek szurgot wrote:
> On 09/05/2012 07:52 AM, Rudra Banerjee wrote:
>> $(SUBDIR)/%.o: $(SUBDIR)/%.c
>> $(CC) -c `$(PCONF1)` $<
>> mv $(*F).o $(SUBDIR)/$(*D)
>>
>> and moved the files back to SUBDIR.
>> is there any way to create the .o of SUBDIR/*.c in SUBDIR only?

>
> %.o: %.c
> $(CC) ...


Which is the builtin rule in Gnu make, so you can omit it and not
write anything at all! If you need something special per subdirectory
you can e.g. do this without overriding the default rule:

test/%.o: CPPFLAGS+=-I.

> there is one more thing missing here - the dependencies on header files.
>
> you might just prefer to use ready-to-go solution. i've once wrote
> generic makefile for compiling all files in the current directory and
> subdirectories:
> https://github.com/el-bart/build_pro..._build_process
> it also tracks dependencies, allows to build automated and manual tests,
> debug, release. all builds are done in a separate directory.


Well, yeah, as long as he can still understand roughly what's going
on. I've seen enough people treating the Makefiles as magic which they
don't have to understand or care about.

/Jorgen

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Casey Carter
Guest
Posts: n/a
 
      09-05-2012
On 2012-09-05 00:52, Rudra Banerjee wrote:
> Thanks,
> Its working.
> Though it was creating the .o files of SUBDIR source in maindir.
> so I did:
> exe=ptbl
> CC=gcc
> CFLAGS =-O3
> SUBDIR=files
> PCONF1=pkg-config --cflags --libs gtk+-2.0
> sources := $(wildcard $(SUBDIR)/*.c)
> objects := $(sources:.c=.o)
> $(exe): $(objects) help.c table.c callbk_list.h cwlst.h
> $(CC) `$(PCONF1)` $(objects) help.c table.c -o $(exe)
>
> $(SUBDIR)/%.o: $(SUBDIR)/%.c
> $(CC) -c `$(PCONF1)` $<
> mv $(*F).o $(SUBDIR)/$(*D)
>
> and moved the files back to SUBDIR.
> is there any way to create the .o of SUBDIR/*.c in SUBDIR only?
>

Add your `$(PCONF1)` into CFLAGS, and get rid of the compiling rule.
make's default rules should build everything properly:

exe = ptbl
PCONF1 := $(shell pkg-config --cflags --libs gtk+-2.0)
CC = gcc
CFLAGS = -O3 $(PCONF1)
SUBDIR = files
sources := $(wildcard $(SUBDIR)/*.c)
objects := $(sources:.c=.o)

$(exe): $(objects) help.c table.c callbk_list.h cwlst.h

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
System.IO.Directory.GetDirectories() and System.IO.Directory.GetFiles() are not returning the specified directory Nathan Sokalski ASP .Net 2 09-06-2007 03:58 PM
Makefile.PL- install modules on home directory Nina Perl Misc 1 10-11-2005 04:31 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi C Programming 0 12-29-2004 05:10 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi Cisco 0 12-29-2004 05:03 PM
libitery directory in gcc-3.1.1 source code package Liang Chen C Programming 22 08-23-2004 09:09 AM



Advertisments