Jan 11, 2013

Compiling C

-O3 indicates optimization level three, which tries every trick known to build faster code. If, when you run the debugger, you find that too many variables have been optimized out for you to follow what’s going on, then change this to -O0.
-Wall adds compiler warnings. This works for gcc, Clang, and icc. For icc, you might prefer -w1, which displays the compiler’s warnings, but not its remarks.
-Werror your compiler will treat warnings as errors.
-I adds the given path to the include search path, which the compiler searches for header files you #included in your code.
-L adds to the library search path. Order matters:
gcc -I/usr/local/include use_useful.c -o use_useful -L/usr/local/lib -luseful
If you have a file named specific.o that depends on the Libbroad library, and Libbroad depends on Libgeneral, then you will need: gcc specific.o -lbroad -lgeneral
-g adds symbols for debugging.
-std=gnu11 is gcc-specific, and specifies that gcc should allow code conforming to the C11 and POSIX standards

pkg-config
pkg-config --libs gsl libxml-2.0
# -lgsl -lgslcblas -lm -lxml2

pkg-config --cflags gsl libxml-2.0
# -I/usr/include/libxml2

gcc `pkg-config --cflags --libs gsl libxml-2.0` -o specific specific.c
# gcc -I/usr/include/libxml2 -lgsl -lgslcblas -lm -lxml2 -o specific specific.c
Runtime linking
export LD_LIBRARY_PATH=libpath:$LD_LIBRARY_PATH
# or 
LDADD=-Llibpath -Wl,-Rlibpath

No comments: