Tuesday, February 2, 2010

CFLAGS

This is really just a note to myself: an elementary example of using the CFLAGS environment variable on OS X:

$ cat test.c
#include <stdio.h>
main() {
printf ("Hello World!\n");
return 0;
}
$ gcc test.c -o test
$ ./test
Hello World!
$ file ./test
./test: Mach-O 64-bit executable x86_64



$ export CFLAGS="-arch i386 -arch x86_64"
$ gcc $CFLAGS test.c -o test
$ ./test
Hello World!
$ file ./test
./test: Mach-O universal binary with 2 architectures
./test (for architecture i386): Mach-O executable i386
./test (for architecture x86_64): Mach-O 64-bit executable x86_64


Just for fun, I looked up the compiler flags used for the very first command to build Python:


gcc 
-c
-arch ppc
-arch i386
-isysroot-isysroot
-fno-strict-aliasing
-fno-common
-dynamic
-DNDEBUG
-g
-fwrapv
-O3
-Wall
-Wstrict-prototypes
-I.
-IInclude
-I./Include
-DPy_BUILD_CORE
-o Modules/python.o
./Modules/python.c


Sources here and here

-c
Compile or assemble the source files, but do not link

-arch
Darwin options, as above

-fno-strict-aliasing
not found anywhere!

-fno-common
In C, allocate even uninitialized global variables in the data section of the object file, rather than generating them as common blocks. This has the effect that if the same variable is declared (without extern) in two different compilations, you will get an error when you link them. The only reason this might be useful is if you wish to verify that the program will work on other systems which always work this way.

-dynamic
Darwin option, passed to the linker
-dynamic
The default. Implied by -dynamiclib, -bundle, or -execute

-DNDEBUG
-D is a pre-processor option
-D name
Predefine name as a macro, with definition 1

-g level
Request debugging information and also use level to specify how much information. The default level is 2

-fwrapv
This option instructs the compiler to assume that signed arithmetic overflow of addition, subtraction and multiplication wraps around using twos-complement representation. This flag enables some optimizations and disables others.

-O3
Optimize yet more. -O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops and -fgcse-after-reload options.

-Wall
All of the -W options combined. This enables all the warnings about constructions that some users consider questionable

-Wstrict-prototypes
Warn if a function is declared or defined without specifying the argument types.

-I.
-IInclude
-I./Include

-I dir
Add the directory dir to the head of the list of directories to be searched for header files. This can be used to override a system header file, substituting your own version, since these directories are searched before the system header file directories. If you use more than one -I option, the directories are scanned in left-to-right order; the standard system directories come after.

-DPy_BUILD_CORE
-D is a pre-processor option
-D name
Predefine name as a macro, with definition 1