I am trying to learn how a Makefile should look like, when it comes to the flags, especially the linking ones. Here is my Makefile:
OBJS = n.o
SOURCE = n.cpp
# HEADER = there are no header files, so I commented that
OUT = test
CXX = ../mpich-install/bin/mpic++
FLAGS = -I../intel/mkl/include ../intel/mkl/lib/intel64/libmkl_scalapack_ilp64.a -Wl,--start-group ../intel/mkl/lib/intel64/libmkl_intel_ilp64.a ../intel/mkl/lib/intel64/libmkl_core.a ../intel/mkl/lib/intel64/libmkl_sequential.a -Wl,--end-group ../intel/mkl/lib/intel64/libmkl_blacs_intelmpi_ilp64.a -lpthread -lm -ldl
all: $(OBJS)
$(CXX) $(OBJS) -o $(OUT) $(FLAGS)
# create/compile the individual files >>separately<<
n.o: n.cpp
$(CXX) -c n.cpp $(FLAGS)
.PHONY : all
# clean house
clean:
rm -f $(OBJS)
and then I am getting:
../mpich-install/bin/mpic++ -c n.cpp -I../intel/mkl/include ../intel/mkl/lib/intel64/libmkl_scalapack_ilp64.a -Wl,--start-group ../intel/mkl/lib/intel64/libmkl_intel_ilp64.a ../intel/mkl/lib/intel64/libmkl_core.a ../intel/mkl/lib/intel64/libmkl_sequential.a -Wl,--end-group ../intel/mkl/lib/intel64/libmkl_blacs_intelmpi_ilp64.a -lpthread -lm -ldl
g++: warning: ../intel/mkl/lib/intel64/libmkl_scalapack_ilp64.a: linker input file unused because linking not done
g++: warning: ../intel/mkl/lib/intel64/libmkl_intel_ilp64.a: linker input file unused because linking not done
g++: warning: ../intel/mkl/lib/intel64/libmkl_core.a: linker input file unused because linking not done
g++: warning: ../intel/mkl/lib/intel64/libmkl_sequential.a: linker input file unused because linking not done
g++: warning: ../intel/mkl/lib/intel64/libmkl_blacs_intelmpi_ilp64.a: linker input file unused because linking not done
../mpich-install/bin/mpic++ n.o -o test -I../intel/mkl/include ../intel/mkl/lib/intel64/libmkl_scalapack_ilp64.a -Wl,--start-group ../intel/mkl/lib/intel64/libmkl_intel_ilp64.a ../intel/mkl/lib/intel64/libmkl_core.a ../intel/mkl/lib/intel64/libmkl_sequential.a -Wl,--end-group ../intel/mkl/lib/intel64/libmkl_blacs_intelmpi_ilp64.a -lpthread -lm -ldl
That means that I should use some flags only in the last part of the process. What is the correct way to handle such a case? Maybe create FLAGS1
and FLAGS2
? It should work, but I want to know which the correct way.
The correct way is to keep up with
make
standard variable names, in particularCXXFLAGS
andLDFLAGS
.You don't want to specify libraries for the linker flags like you try to do here (specifying subjects actually):
Instead of using
FLAGS
and giving a direct subject for linking, you should rather use the standardLDFLAGS
makefile variable to setup the path, and let the linker find the appropriate static or shared libraries:Distinguish from compilation phase flags (
CXXFLAGS
), and linking stage flags (LDFLAGS
).Aside:
To avoid this
action in your final target rule (that was probably added to avoid missing to catch up with header dependencies), add an
-MF
option to theCXXFLAGS
, and include the results.Here's more detailed information about the various techniques:
Auto-Dependency Generation