A (work in progress) compilation of useful GCC configuration, focused on C.
-Wallis a must.
-Werrorto treat warnings as errors.
-l instructs the linker it has some work to do (i.e. link a library please).
-lm is shorthand for
-libm and in turn
-libm.a is a static library is selected.
-L hint to the linker where to locate libraries.
-L/opt/gdbm-1.10/src in combination with -lgdbm tells the linker exactly where to find the gdbm libraries. Multiple
-L can be specified. Alternatively use the
LIBRARY_PATH environment variable like so:
-I hint to compiler where to locate headers.
-I include may be specified. Alternatively use the
C_INCLUDE_PATH environment variable like so:
-g debug symbols. Now you’ll get variable and function names, yummy.
-std=gnu11 by default GCC uses the C89 standard. C11 FTW.
-O3 optimisation level 3. This is heavy duty. If you find debugging tough (e.g. variables have been optimised out etc), try dropping it back to level zero
Link with explicit static library:
$ gcc -Wall -I/opt/gdbm-1.8.3/include dbmain.c /opt/gdbm-1.8.3/lib/libgdbm.a
Link with explicit shared library:
$ gcc -Wall -I/opt/gdbm-1.8.3/include dbmain.c /opt/gdbm-1.8.3/lib/libgdbm.so
Provide header and library hint paths:
$ gcc -Wall -I/tmp/gdbm-1.10/src -L/tmp/gdbm-1.10/src dbmain.c -lgdbm
A simple makefile:
make uses the following recipe:
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $*.c
make decides you have an executable to build from object files:
$(CC) $(LDFLAGS) first.o second.o $(LDLIBS)