6

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?

KcFnMi
  • 5,516
  • 10
  • 62
  • 136
  • 1
    Defining `CXXFLAGS` in terms of `CFLAGS` is probably not a good idea. I usually interpret `CFLAGS` to refer to flags for the C compiler specifically, and `CXXFLAGS` for the flags to the C++ compiler. If you don't have mixed languages in your project then you don't need the C version of the flags at all. – Cubic Jun 10 '18 at 12:16
  • Which files do you have? C or C++ files? – JFMR Jun 10 '18 at 14:56
  • "Now I'm on `C++`". – KcFnMi Jun 10 '18 at 15:16
  • Possible duplicate of [CFLAGS, CCFLAGS, CXXFLAGS - what exactly do these variables control?](https://stackoverflow.com/q/5541946/608639) Also see [GNU Coding Standards - Makefile Conventions](https://www.gnu.org/prep/standards/html_node/Makefile-Conventions.html). – jww Jun 10 '18 at 16:50

1 Answers1

9

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 eigen to CXXFLAGS or CFLAGS?

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 .h files 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.

JFMR
  • 23,265
  • 4
  • 52
  • 76