I read a lot of tutorials about CFLAGS
and also looked in the official docs. Everywhere they say CFLAGS
is implicit but still pass it explicitly in their example makefile to the compiler:
CFLAGS=-O2
gcc $(CFLAGS) -c foo.c -o foo.o
So, what does the term "implicit" mean in this context? If I declare CFLAGS=-O2
in my makefile and later just say gcc -c foo.c -o foo.o
, will -O2
be active or not (so, is it really implicit)? If so, why do all tutorials (including official docs) still pass it explicitly in their examples?
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.
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.
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The "overall options" allow you to stop this process at an intermediate stage. For example, the -c option says not to run the linker. Then the output consists of object files output by the assembler.
Everywhere they say CFLAGS is implicit but still pass it explicitly in their example makefile to the compiler.
gcc does not use CFLAGS
environment variable. See Environment Variables Affecting GCC.
CFLAGS
is a conventional name for a Makefile variable with C-compiler flags and it is used by implicit make rules. See Variables Used by Implicit Rules for more details.
If you use your own make rules instead of the built-in ones, you do not need to use CFLAGS
at all. Although it is a useful convention to do so because people are familiar with the conventional make variable names.
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