Author
Christopher Marshall (christopherlmarshall@yahoo.com)
Raw Notes on gcc
# this creates a.o and b.o from a.c and b.c gcc -c a.c b.c # this links a.o and b.o into prog gcc -o prog a.o b.o #gcc options -Idir prepend 'dir' to list of dirs to find include files in -Ldir prepend 'dir' to list of dirs to find libraries in -lfoo link against libfoo.so or libfoo.a -static link against static libraries only # variables involved in finding headers: CPATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH OBJC_INCLUDE_PATH # CPATH # used for all languages. directories specified here are # serached after any paths given with -I on the command line # C_INCLUDE_PATH # used just for C. directories specified here are searched # after any paths given with -isystem on the command line # CPLUS_INCLUDE_PATH # used just for C++. directories specified here are searched # after any paths given with -isystem on the command line # OBJC_INCLUDE_PATH # used just for objc. directories specified here are searched # after any paths given with -isystem on the command line # variables involved in finding libraries: /etc/ld.so.conf LIBRARY_PATH LD_LIBRARY_PATH LD_PRELOAD # the dirs listed in /etc/ld.so.conf are searched by ldconfig when it sets links. # the dirs listed in LIBRARY_PATH are searched by gcc when linking and using -lfoo args. # the dirs listed in LD_LIBRARY_PATH are searched by the program loader as it # follows references in the executable to dynamic libraries # the dirs listed in LD_PRELOAD are like LD_LIBRARY_PATH, only they are guaranteed to be # searched first. # ar commands # create libname.a from foo.o, bar.o, and baz.o ar rcs libname.a foo.o bar.o baz.o # create the library one file at a time ar rcs libname.a foo.o ar rcs libname.a bar.o ar rcs libname.a baz.o # list the contents of a static library # how do you do this for a shared library? ar t libname.a # list the shared libraries an executable is linked against ldd program # creating a shared library # libfoo.so.5.3.12 # lib is built from f1.c, f2.c, and f3.c and linked against libc and libm # the soname of the library is libfoo.so.5 and the actual library file is libfoo.so.5.3.12 # note how we create a link from the soname to the lib file at the end gcc -fPIC -c f1.c gcc -fPIC -c f2.c gcc -fPIC -c f3.c gcc -shared -Wl,-soname,libfoo.so.5 -o libfoo.so.5.3.12 f1.o f2.o f3.o -lc -lm ln -sf libfoo.so.5.3.12 libfoo.so.5 ln -sf libfoo.so.5 libfoo.so # here we compile and link a program against libfoo, assuming libfoo is in the current directory and # not system wide. Notice how we have to add the current directory to the LD_LIBRARY_PATH when loading the program. gcc -c prg.c gcc -L"." -lfoo -o prg prg.o LD_LIBRARY_PATH=$(pwd) ; ./prg # here is how it would work if it were system-wide. cp libfoo.so.5.3.12 /usr/lib ldconfig gcc -lfoo -o prg prg.o # dlopen(), dlerrer(), dlsym(), dlclose()
