GCC
GCC -c compile the source code to object code
e.g. gcc -o abc.c, the object file abc.o will be generated.
GCC -o compile the exe file
e.g. gcc -o abc abc.o, the exe file abc will be generated.
GCC -I path, the header file search path
e.g. gcc -c -I include abc.c
GCC -l option is to link the library. It can use for static and share link. Link -l with library name without the lib prefix and the .a or .so extensions.
Static : gcc -static -ltest xx.c, it will link with libtest.a
share : gcc –ltest xx.c it will link with libtest.so
in Linux, library names almost always start with lib.
using -L to get the path of lib.
e.g. gcc -ltest -L/usr/lib xx.c
GCC -D pass macro to source code
e.g. gcc -D NDEBUG -c abc.c, pass the NDEBUG to source abc.c, it also can be NDEBUG=3 if you want pass a macro with a value.
GCC -E
If there are some MACRO codes are not easy to understand, you can use gcc –E to expand MACRO to C code.
GCC -save-temps
adding parameters -S -save-temps makes it leave all intermediate files - preprocessed, assembly, objects
static lib (*.a)
below command links a static lib libtest
ar cr libtest.a test1.o test2.o
cr is to tell create archive
-> using it
gcc -static -o app app.o -L. -ltest
dynamic lib (*.so)
gcc -c -fPIC test1.c
gcc -shared -fPIC -o libtest.so test1.o test2.o
PIC: position independence code
PIC stands for position-independent code. The functions in a shared library may be loaded at different addresses in different programs, so the code in the shared object must not depend on the address (or position) at which it is loaded.
->using it
gcc -o app app.o -L. –ltest
Dynamic link can auto reference all related libs. For example, the tiff lib which uses libjpeg and libz.
when you use dynamic link method to compile your application, thoes libs are linked automatically. You can use ldd command to see what libs are linked.
But for static link, you must put all relevant libs in gcc commands, otherwise, some errors will be shown when compiling
GDB
using gdb, you must enable the debug information in gcc command, gcc -g
#start debug
%gdb abc
#run
%run, it will run the abc
There are lots of command in gdb, we can get the support by info gdb or man gdb.
next, step, where, break, run, print, etc.
addr2line
Converts program addresses into filenames and numbers by reading the debug symbol tables in an executable file. It is very useful when decoding addresses printed out in a system crash report.
ar
The archive utility is used to create static libraries.
ld
This is the GNU linker.
ldd
ldd command displays the shared libraries that are linked into an executable
nm
This lists symbols from object files.
objcopy
This is used to copy and translate object files.
objdump
This is used to display information from object files.
strip
This is used to strip an object file of debug symbol tables, thus making it smaller. Typically, you would strip all the executable code that is put onto the target.
strings
This displays strings of printable characters in files.
readelf
This displays information about files in ELF object format.
#get GCC include folder
for C: gcc -xc -E -v -
for C++: gcc -xc++ -E -v -
浙公网安备 33010602011771号