LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help with makefile? (https://www.linuxquestions.org/questions/programming-9/help-with-makefile-148213/)

BrianK 02-19-2004 07:31 PM

help with makefile?
 
I don't know if this is possible, but I want to list all my .C files and then be able to use the list to make my program. I have this:

Code:

CXX = g++
DEF = -DPRMAN
INC = -I /usr/local/prman/include -I /root/MLInclude

# files:= $(wildcard *.C)

files:= 1.C \
        2.C \
        3.C \
        4.C \
        5.C \
        6.C \
        7.C \
        8.C \
        9.C \

objects:= $(files:%.C=%.o)


all: $(objects)
        $(CXX) $(files:.C=.o) -o program

$(objects ): $(files ) $(files:.C=.h )
        $(CXX) -c $(DEF) $(INC) $(files ) -o $(files:.C=.o )

So I'd like to type "make" and have it make each one of the object files, then the program, but instead of evaluating the variables one at a time, it evaluates all of them at once. Is there a way to make it evaluate the variables one at a time (deliniatd by spaces)? Or is there some other way I can automate the make - so that I can just change the file list and have everything just work?

In other words, how would I make the green stuff evaluate one at a time, but everything else evaluate the entire string?

Thanks

wapcaplet 02-19-2004 10:31 PM

I'm no Makefile expert by any means, but perhaps some kind of rule like the following would do it:

.C.o:
$(CXX) -c $< -o $@

Basically just telling make how to create an object file out of a C source file. Not too sure about the syntax...

bigearsbilly 03-11-2004 11:27 AM

try something like:
=======================
TARGETS = $(CFILES:.c=)

include include.mk

${TARGETS}:

include.mk:
ls *.c | xargs echo CFILES = > include.mk

BrianK 03-11-2004 02:23 PM

Quote:

Originally posted by bigearsbilly
try something like:
=======================
TARGETS = $(CFILES:.c=)

include include.mk

${TARGETS}:

include.mk:
ls *.c | xargs echo CFILES = > include.mk

heeeyy! that's pretty slick. I didn't know about "xargs" - looks like that's exactly what I was looking for.

Thanks!

bigearsbilly 03-12-2004 02:35 AM

I worked out how to do it properly.
this will automagically find
c files and include the header file dependencies
too. ( a bit cleverer than my previous e.g but
still short and sweet!)

They must be simple single source files though
i.e. each with it's own main() and not linked together.

Assuming you have "makedepend" also.

the standard rule is if you do
"make target" it will assume a "target.c" file and cc it.
You don't even need a make file!

like "make func"

will
cc -c func.c -o func.o
cc func.o -o func




# billy billingham
# Thu Mar 11 22:21:13 GMT 2004
#
# ---------------------------------
# Standard makefile for standalone
# C files (not multifile projects)
# e.g. makes 'func.c' with:
#
# cc -c func.c -o func.o
# cc func.o -o func
#
# end up with 'func'
#
#
# using the built-in rules for
# simple single source programs
#
# recursive call to make, first
# call to create the include'd files
# make cannot find the includes so makes them
# before it does the all: target
# ---------------------------------

INCLUDEFILE=files.mk # contains CFILES=
DEPENDFILE=depend.mk # output of makedepend to add the .h deps
TARGETS=$(CFILES:.c=) # remove .c to make the program name

all:
@echo recursive make
$(MAKE) $(TARGETS)
rm $(DEPENDFILE) $(INCLUDEFILE) # remade next time

$(DEPENDFILE):
touch $@
makedepend -f $@ *.c

$(INCLUDEFILE):
echo CFILES= *.c > $@

clean:
-rm *.o

include $(INCLUDEFILE)
include $(DEPENDFILE)


All times are GMT -5. The time now is 03:18 AM.