GCC Command Line Switches
- -o = output
- if no -o specified, you get a.out
- -c = compile a .c file to a .o object file
- -Wall = give standard warnings
- -W even expanded set of warnings
- common practice to use -W and -Wall together.
- can explicitly specify full pathname to all source and library files
- -l<NAME> = link against library with standard name and path
- -I<PATH> = specify path containing needed include file (can be multiple -I's in one gcc command
- -L<PATH> = specify path containing needed library
- -D<MACRO NAME> = declare to compiler that you're using the macro MACRO_NAME for purposes of preprocessor
- `cpp -dM /dev/null` = show all predefined macros
- Good idea to surround macro values with parentheses if used for numerical expression, for order of operations
- -E = show what the cpp file would look like with the values for macros substituted
- -g = include debugging symbols
- -O<optimization level> = must not optimize if you want to debug
GCC Environment Variables
- SOME OF THESE USED BOTH AT COMPILE TIME AND RUNTIME!
- standard unix colon separated list
- LD_LIBRARY_PATH = runtime load path for shared libraries
- a shell script.
- ./configure --help
- ./configure --prefix=/install/to/here
- Write the code for the "foo" library
- Make the foo header file
- Compile your library code into an object file:
- gcc -c -fPIC foo_source_code.c -o foo_object_file.o
- Link the object file into the actual library file using the standard naming conventions
- gcc -shared -Wl,-soname,libfoo.so.1 -o libfoo.so.1.0.1 foo_object_file.o
- #include the foo header and use the code in your foo library in your main executable code
- Link your newly minted library to the main executable
- gcc main.c -o dynamically_linked -L. -lfoo
- Executing the dynamically linked programm
- .a extension = static library. "Archive." Code is copied from archive into executable.
- .so extension = shared library "Shared Object"
- linker option -rpath = hard code the path of the directory where the library is into the executable, so the executable will go to that path to look for the library. Not good if lib is moved or executable is moved to different system.
Compilation auxilliary programs
- nm - displays the name list or a symbol table of a binary
- ldd - on linux, runs the program and shows what libraries are linked to - NOT SAFE
- strace - very nice utility!
Debugging Linker problems
- nm the_binary.0.dylib | c++filt
- look at the symbol table of the compiled binary, and demangle the c++ symbols to be human readable
- The symbol type. At least the following types are used; others are, as well, depending on the object file format. If lowercase, the symbol is local; if uppercase, the symbol is global (external).
- A - The symbol's value is absolute, and will not be changed by further linking.
- B - The symbol is in the uninitialized data section (known as BSS).
- C - The symbol is common. Common symbols are uninitialized data. When linking, multiple common symbols may appear with the same name. If the symbol is defined anywhere, the common symbols are treated as undefined references.
- D - The symbol is in the initialized data section.
- G - The symbol is in an initialized data section for small objects. Some object file formats permit more efficient access to small data objects, such as a global int variable as opposed to a large global array.
- I - The symbol is an indirect reference to another symbol. This is a GNU extension to the a.out object file format which is rarely used.
- N - The symbol is a debugging symbol.
- R - The symbol is in a read only data section.
- S - The symbol is in an uninitialized data section for small objects.
- T - The symbol is in the text (code) section.
- U - The symbol is undefined.
- V - The symbol is a weak object. When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is used with no error. When a weak undefined symbol is linked and the symbol is not defined, the value of the weak symbol becomes zero with no error.
- W - The symbol is a weak symbol that has not been specifically tagged as a weak object symbol. When a weak defined symbol is linked with a normal defined symbol, the normal defined symbol is used with no error. When a weak undefined symbol is linked and the symbol is not defined, the value of the weak symbol becomes zero with no error.
- - - The symbol is a stabs symbol in an a.out object file. In this case, the next values printed are the stabs other field, the stabs desc field, and the stab type. Stabs symbols are used to hold debugging information.
- ? - The symbol type is unknown, or object file format specific.