Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to put things (CFLAGS or CXXFLAGS) on Makefile?

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?

like image 484
KcFnMi Avatar asked Jun 10 '18 12:06

KcFnMi


People also ask

Where do you put CFLAGS?

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.

How do I add CFLAGS in Makefile?

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.

What is the use of CFLAGS in Makefile?

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.

What is CFLAGS and Ldflags?

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).


1 Answers

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.

like image 146
ネロク・ゴ Avatar answered Oct 22 '22 23:10

ネロク・ゴ