First the Makefile here had
CFLAGS = -g -Wall -lm
I was playing with C that time. Now I'm on C++ and I have to add -I eigen, quick google on it and found CXXFLAGS exist for the C++ world, while CFLAGS exist for the C world. So I updated Makefile to
CFLAGS = -g -Wall -lm
CXXFLAGS = -I eigen
Then I found https://wiki.gentoo.org/wiki/GCC_optimization, and was inspired to updated it again
CFLAGS = -g -Wall -lm
CXXFLAGS = ${CFLAGS} -I eigen
The complete thing:
CC = g++
CFLAGS = -g -Wall -lm
CXXFLAGS = ${CFLAGS} -I eigen
OBJS = main.o multiply.o
PROGRAM = multitply
$(PROGRAM): $(OBJS)
$(CC) $(OBJS) $(CFLAGS) -o $(PROGRAM)
Should I add -I eigen to CXXFLAGS or CFLAGS?
Also noticed the existence of CPPFLAGS.
Should I change to $(CC) $(OBJS) $(CXXFLAGS) $(CPPFLAGS) -o $(PROGRAM)
or to $(CC) $(OBJS) -o $(PROGRAM)?
Should I change to $(PROGRAM): $(OBJS) *.h, so it rebuilds whenever .h files get changes?
Any other improvements to it?
Put CFLAGS last in the compilation command, after other variables containing compiler options, so the user can use CFLAGS to override the others. CFLAGS should be used in every invocation of the C compiler, both those which do compilation and those which do linking.
It should be CFLAGS := -Wall -Wextra $(CFLAGS) , the difference is that CFLAGS is explicitly appended. So for example, you may set -Og , but user don't want optimization and passes CFLAGS=-O0 on command line. By using CFLAGS += -Og your -Og will take over the user provided value.
CFLAGS and CXXFLAGS are either the name of environment variables or of Makefile variables that can be set to specify additional switches to be passed to a compiler in the process of building computer software.
The make-specific variables (the ones in capital letters) follow a name convention such that: CC refers to the compiler (gcc in this case); CFLAGS contains compiler directives/options; and LDFLAGS is a list of link (or "load") directives (here, instructions to link with the C math library).
I would use CFLAGS when compiling C files and CXXFLAGS when compiling C++ files. Besides CFLAGS and CXXFLAGS you are perhaps missing another relevant variable here: CPPFLAGS.
Should I add
-I eigentoCXXFLAGSorCFLAGS?
CPPFLAGS is typically used for providing options related to the preprocessor.
I would use this variable for specifying include directories:
CPPFLAGS = -I eigen
Another interesting variable, which is useful for providing libraries, would be LDLIBS. You could take advantage of it for passing -lm:
LDLIBS = -lm
Should I change to
$(PROGRAM): $(OBJS) *.h, so it rebuilds whenever.hfiles get changes?
The approach I would recommend is to add prerequisites for the header files to the corresponding object files by writing rules without recipe, for example:
main.o: main.h multiply.h ...
multiply.o: multiply.h ...
Besides, * won't do what you expect to do, i.e., it is not a wildcard in that context. Place those lines at the end of the Makefile, so that they don't replace the default target.
The Makefile could be something like:
CXXFLAGS = -g -Wall
CPPFLAGS = -I eigen
LDLIBS = -lm
OBJS = main.o multiply.o
PROGRAM = multitply
$(PROGRAM): $(OBJS)
$(CXX) $^ $(LDLIBS) -o $@
No need for repeating $(PROGRAM) and $(OBJS) in the recipe, you can simply use the automatic variables $@ and $^, respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With