27

The following link in the official documentation for GCC:

https://gcc.gnu.org/onlinedocs/gcc/gcc-command-options/environment-variables-affecting-gcc.html

Explains the following environment variables:

LANG
LC_CTYPE
LC_MESSAGES
LC_ALL
TMPDIR
GCC_COMPARE_DEBUG
GCC_EXEC_PREFIX
COMPILER_PATH
LIBRARY_PATH
CPATH
C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
OBJC_INCLUDE_PATH
DEPENDENCIES_OUTPUT
SUNPRO_DEPENDENCIES

But I have also heard/read before about these other compiling flags:

  • For compiling C code: CC, CFLAGS
  • For compiling C++ code: CXX, CPPFLAGS

And linking flags:

  • For the linking stage: LDFLAGS
  • After the code is compiled: LD_LIBRARY_PATH

What is the meaning of CC, CFLAGS, CXX, and CPPFLAGS? Why aren't they included in the official list of environment variables for gcc?

3
  • 2
    They have nothing to do with GCC. They are just a sort of convention on Unix, and accordingly are supported out-of-the-box in Unix family. I guess they became a convention because plain-old makefiles by convention tend to rely on these variables. Many build systems (such as Autotools) adopted this convention too and use similar variables to denote the same things. To be honest, these flags are old pile of crap altogether (perhaps except LD_LIBRARY_PATH) reminding me of 60's every time is encounter them. If you are using a modern build (which you should be) system you can forget about them. Apr 16, 2013 at 20:21
  • 1
    LD_LIBRARY_PATH is used to point out the directories in which shared libraries reside so that applications relying on these shared libraries could be properly linked (dynamically) against them. As you can see this again pertains not to software development and not to GCC, but is just rather a feature/concept of how Unix family handles dynamic linking, installation paths of software components, and their execution. If you are satisfied with these comments I can formulate them as an answer. Feel free to ask more. Apr 16, 2013 at 20:33
  • Yes @Haroogan. You are welcome to formulate this as an answer, since that is already quite informative. Not sure if I will accept anything yet since I would like to give the thread some time to collect answers. Apr 16, 2013 at 20:44

3 Answers 3

31

To begin with, all the variables you mentioned: CC, CFLAGS, CXX, CXXFLAGS, LDFLAGS, LD_LIBRARY_PATH, are originated from Unix OS family. These variables have nothing to do with GCC in the first place, that's why you see no trace of them in the manuals.

The only meaningful variable (which has no direct connection with GCC too) among these is LD_LIBRARY_PATH. You'll probably find this variable to be defined out-of-the-box on any modern Unix-like OS. Here is the the LD.SO(8) man-page from Linux Programmer's Manual which mentions LD_LIBRARY_PATH and its purpose. Here is one more extract:

The LD_LIBRARY_PATH environment variable contains a colon-separated list of directories that are searched by the dynamic linker when looking for a shared library to load.

The directories are searched in the order they are mentioned in.

If not specified, the linker uses the default, which is /lib:/usr/lib:/usr/local/lib.

As you can see LD_LIBRARY_PATH is nothing but an OS-specific environment variable for proper loading of shared libraries. Windows has similar environment variable in this regard: PATH. Windows will scan directories listed in it when searching for dynamic-link library (DLL, a counterpart of SO on Linux) too.

Concerning the rest of the variables (CC, CFLAGS, CXX, CXXFLAGS, LDFLAGS), you see them so often due to the historical reasons. Since the rise of Unix era, software projects were built using Make (scroll down and look at the examples of typical makefiles) — one of the pioneering build tools. These variables were so extensively used in makefiles that eventually they became sort of a convention (see Implicit Rules, for instance). That's why you can even see them defined out-of-the-box on, for example, Linux, and most likely pointing to GCC (as it is considered to be the native toolchain for Linux).

To conclude, the point is: don't scratch your head over CC, CFLAGS, CXX, CXXFLAGS, LDFLAGS, and friends, as they are just a blast from the past. ;)

BONUS


Using plain old Make directly to build complex software today quickly becomes tedious and error-prone. As a result, numerous sophisticated build system generators like GNU Automake or CMake have been developed. In brief, their goal is to provide (arguably) more readable, easy-to-maintain, and high-level syntax to define an arbitrarily complex build system for an arbitrary software project to be built. Typically, before actually building the project, one has to generate a native build system (which could also be represented by plain old makefiles, for example, for portability reasons, but not necessarily) out of this high-level definition using the corresponding set of tools. Finally, one has to build the project with the tool(s) corresponding to the generated (native) build system (for example, Make in case of plain old makefiles, but not necessarily).

Since you are asking these questions, I suspect that you are about to dive into native software development with C or C++. If so, I would strongly recommend you to pick a modern build system (CMake would be my personal recommendation) in the first place, play with it, and learn it well.

5
  • That's a fantastic answer. Thanks @Haroogan. There is a lot of great stuff in your answer on modern build systems too. I can't believe that was deleted (even though I am familiar with the policy). Thanks again. This is exactly what I was looking for. Apr 16, 2013 at 21:41
  • 2
    You're welcome. I know how hard (or rather impossible) it is to grasp all that in one shot for beginners in the field of native programming because there is simply so much stuff going on around: compilation, linkage, build systems, new language standards, old language standards, platforms, ∞... And how easy it is to follow the wrong path right from the beginning. Unfortunately, it is difficult to find useful and up-to-date material to read. The only way is to seek some guidance here on SO. Best of luck with your future endeavors, whatever they are. Apr 16, 2013 at 21:55
  • Wow, I think that thread with your great answer on modern build systems is now lost. It looks like it's not available from the Google cache anymore, nor in stackprinter.com... If we find it anywhere else, we should just add the text to this answer. Sep 18, 2013 at 14:26
  • Aw, that's indeed unfortunate. In any case, I might write a better tutorial on it later, and fully expand this topic, provide real world examples. I'm kind of busy these days, but I'll definitely do that some day. Surely, I will post a link here. Sep 18, 2013 at 15:37
  • By the way, it would be great to see how some of these solutions integrate with the "recently"-introduced (Nov. 2012) GNU GUIX: gnu.org/software/guix (it would be good to know your thoughts on the approach GNU GUIX has taken any way) Sep 18, 2013 at 15:46
2

You can definitely use environment variable with GCC for CFLAGS and CC (and anything else). You just have to pass the variables to the the compile line, with slight differences depending on the operating system.

Linux set CFLAGS environment variable:

export CFLAGS="-g -Wall -std=c89 -pedantic"

Compile on Linux using CFLAGS

gcc $CFLAGS - o progname progname.c

Windows set CFLAGS environment variable:

set CFLAGS=-g -Wall -std=c89 -pedantic

Compile on Windows using CFLAGS

gcc %CFLAGS% - o progname progname.c

You can even setup a temporary compile string at a variable and call it to compile as you're testing.

set BUILD=gcc -g -Wall -std=c89 -pedantic - o progname progname.c

and call it like...

%BUILD%

One thing to remember when setting and using the variables on Linux (as most programmers know), the variables are case sensitive so $cflags would simply be ignored. On Windows case doesn't matter.

On both systems the above only works until the terminal (or command prompt) session is terminate. To make them permanent you need to set the variables in their respective settings files.

1

In simple terms CC, CFLAGS, LDFLAGS etc are gnu Makefile variables. If defined, these will be used by implicit rules even without actually being mentioned in commands/rules

1
  • In your answer, it is not clear what you mean by "these will be used by implicit rules" - one could believe these are some implicit rules of GCC. So you may want to add implicit rules of Make. This is important to realize, because if one specifies his own rule .c.o: then the value of CFLAGS is not in use. If they were the rules of GCC, they would be used.
    – Palo
    Oct 2, 2016 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.