CFLAGS, CCFLAGS, CXXFLAGS - what exactly do these variables control?

I use GNU make to compile my C ++ code, and I would like to understand how to make my compilations customizable.

I read in various places that CFLAGS , CCFLAGS and CXXFLAGS are used for this purpose. So how should I use them? If I have additional command line arguments for the compiler, should I add them to CFLAGS or add them? Is there a common practice?

Why are there three different variables? I assume that the C compiler should get CFLAGS and CCFLAGS , while the C ++ compiler should get CFLAGS and CXXFLAGS - did I get it right?

Is the user supposed to set these variables at all? Do you install any automatic tools ( automake , autoconf , etc.)? The linux system that I should use does not define any of these variables - is this typical?

Currently, my Makefile looks like this and I feel this is a bit dirty:

 ifdef code_coverage GCOV_FLAG := -fprofile-arcs -ftest-coverage else GCOV_FLAG := endif WFLAGS := -Wall INC_FLAGS := -Istuff -Imore_stuff -Ietc CCFLAGSINT := -O3 $(WFLAGS) $(INC_FLAGS) $(CCFLAGS) ... (somewhere in the makefile, the command-line for compilation looks like this) $(CC) $(CCFLAGSINT) -c $< -o $@ ... (somewhere in the makefile, the command-line for linking looks like this) $(CC) $(GCOV_FLAG) $(CCFLAGSINT) $(OBJLIST) $(LDFLAGS) -o $@ 

I am sure there are no errors; The makefile works very well. But is there anything that goes against the conventions (e.g. CCFLAGSINT - instead of just overwriting CCFLAGS ? Or CXXFLAGS ? FUD!)

Sorry for so many questions; you obviously wonโ€™t answer them, but I hope that the answers will help me understand the general idea of โ€‹โ€‹these settings.

+61
gcc naming-conventions makefile
Apr 04 '11 at 17:14
source share
3 answers

As you noticed, these are Makefiles {macros or variables}, not compiler options. They implement a number of agreements. (Macros are the old name for them, which is still used by some. GNU make doc calls them variables.)

The only reason names matter is the default make rules, visible through make -p , which use some of them.

If you write all your own rules, you can select all your own macro names.

In vanilla wildebeest do, there is no such thing as CCFLAGS. There are CFLAGS , CPPFLAGS and CXXFLAGS . CFLAGS for the C compiler, CXXFLAGS for C ++ and CPPFLAGS for both.

Why are CPPFLAGS in both? Traditionally, it is the home of the preprocessor flags ( -D , -U ), and both c and C ++ use them. The assumption that everyone wants the same environment for c and C ++ is perhaps dubious, but traditional.




PS As James Moore noted, some projects use CPPFLAGS for flags for the C ++ compiler, and not for flags of the C. Android NDK preprocessor, for one huge example.

+74
Apr 04 2018-11-11T00:
source share

According to GNU make manual:

CFLAGS: additional flags for the C compiler.
CXXFLAGS: additional flags for the C ++ compiler.
CPPFLAGS: additional flags for the C preprocessor and programs that use it (C and Fortran compilers).

source: https://www.gnu.org/software/make/manual/make.html#index-cFLAGS
Note: PP stands for PreProcessor (not Plus Plus), i.e.

CPP: a program to run the C preprocessor with the results on standard output; default is $ (CC) -E.

These variables are used by implicit make rules.

Compilation of C programs
no made automatically from nc with form recipe
'$ (CC) $ (CPPFLAGS) $ (CFLAGS) -c.

Compilation of C ++ programs
no is automatically generated from n.cc, n.cpp or nC with a recipe of the form
'$ (CXX) $ (CPPFLAGS) $ (CXXFLAGS) -c.
We recommend that you use the suffix '.cc for C ++ source files instead of'. C.

source: https://www.gnu.org/software/make/manual/make.html#Catalogue-of-Rules

+8
Sep 05 '18 at 7:38
source share

And just for Mizux to say as a minimal example:

main_c.c

 #include <stdio.h> int main(void) { puts("hello"); } 

main_cpp.cpp

 #include <iostream> int main(void) { std::cout << "hello" << std::endl; } 

Then without any Makefile :

 make CFLAGS='-g -O3' CXXFLAGS='-ggdb3 -O0' CPPFLAGS='-DX=1 -DY=2' main_c main_cpp 

works:

 cc -g -O3 -DX=1 -DY=2 main_c.c -o main_c g++ -ggdb3 -O0 -DX=1 -DY=2 main_cpp.cpp -o main_cpp 

So, we understand that:

  • make were implicit rules for creating main_c and main_cpp from main_c.c and main_cpp.cpp
  • CFLAGS and CPPFLAGS were used as part of an implicit compilation rule .c
  • CXXFLAGS and CPPFLAGS were used as part of an implicit rule for compiling .cpp
0
May 24 '19 at 10:04
source share



All Articles