Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I need to pass CFLAGS explicitly to gcc?

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?

like image 590
Foo Bar Avatar asked Apr 27 '15 15:04

Foo Bar


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.

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.

Which GCC option should be used?

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.


1 Answers

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.

like image 126
Maxim Egorushkin Avatar answered Oct 06 '22 19:10

Maxim Egorushkin