Prasanth Madhavan
Prasanth Madhavan

Reputation: 13309

help with make file

I have a make file like this....... some of the files are in the main directory and some others in the tests directory..

VPATH = tests

objects = main.o script.o factory.o serve.o enter.o\
           login.o notify.o check.o
script : $(objects)
    g++ $(objects) -lcurl -o script

main.o : script.h
script.o : enter.h login.h factory.h
factory.o : check.h notify.h serve.h
check.o :
serve.o : check.h
notify.o :
enter.o : check.h
login.o : check.h

.PHONY : clean
clean :
    -rm *.o script

i want make to save the object files to the directory where its cpp file comes from.. i.e. if script.cpp was inside tests folder, then i want the script.o also tobe inside the tests folder.. now it just saves the file inside the main folder..

Thanks in advance..

EDIT 1: I need to add files lateron to the tests folder.. is there a way to make the makefile recognise that new files have been added and compile them also?

Upvotes: 1

Views: 186

Answers (2)

bta
bta

Reputation: 45057

Instead of hard-coding a list of files to build, you can use a wildcard to find source files. You can also use a substitution to convert this into a list of object files. Provide a generic rule for building a .c into a .o and you should be all set.

FILES_TO_BUILD := $(wildcard *.c) $(wildcard tests\*.c)
OBJECTS_FILES  := $(patsubst %.c,%.o,$(FILES_TO_BUILD))

%.o: %.c
    $(CC) $(COPTS) $^ # or whatever your compiler line is

script: $(OBJECT_FILES)
    g++ $^ -lcurl -o $@

I haven't tested this makefile (it's just off the top of my head) but it should give you something to start with.

Upvotes: 2

Kiril Kirov
Kiril Kirov

Reputation: 38163

You can add something like

 mv *.d $(VPATH)/

in your makefile, to be something like:

...
script : $(objects)
    g++ $(objects) -lcurl -o script
    mv *.d $(VPATH)/
...

You may need to add @ in front of mv, "@mv..."

Upvotes: 0

Related Questions