makefile - Why doesn't my make file leave behind object files? -


i new make files , put bit of trial & error. code used compile c++ program.

my main.cpp file in same folder makefile. have lib/ folder contains headers main depends on.

the following makefile results in correct , complete compilation of code. expecting find *.o objects left behind. (note i've tried make both , without "clean" rule, , same results both times.)

# # makefile #  cxx = g++ ccflags = -o3 -i/sw/include -l/sw/lib       ## /sw/include , /sw/lib contain dependencies files in lib/ ldflags = -lpng opts = $(ccflags) $(ldflags)  sources = $(wildcard lib/*.cpp) main.cpp objects = $(sources: .cpp = .o) target = spirals  $(target): $(objects)     $(cxx) $(opts) $^ -o $@  .phony: depend depend:     g++ -mm $(sources) > depend       ## generate dependencies list include depend  .phony: clean clean:     rm -f *.o lib/*.o $(target) 

also, in case matters, i'm on macosx , program designed in xcode. (i know xcode has own build flow, i'm designing command-line program linux system , i'd test compilation & linking in bash environment instead of going through xcode.)

questions:

  1. am correct expect makefiles produce *.o files stick around once main target has been created?

  2. if so, why doesn't makefile this?

if observe command $(target) rule causes run:

g++ -o3 -i/sw/include -l/sw/lib -lpng lib/bar.cpp lib/foo.cpp main.cpp -o spirals 

you'll see $(objects) in fact contains *.cpp files, , there no *.o files sticking around because haven't asked any.

the problem here:

objects = $(sources:.cpp=.o) 

in gnu makefile written, substitution reference written excess spaces, never matches , $(objects) ends same $(sources). rewrite above , it'll expect.

(other notes: -lpng needs go @ end of link command work in general, should introduce make variable (traditionally called $(ldlibs)) arrange that. new makefiles, better spell out dependencies explicitly rather playing games $(wildcard) , computed $(objects). -i options needed during compilation while -l options used during linking, arrange separate $(cxxflags)/$(ldflags) variables used in separate rules added when required.)


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -