gcc --version:gcc的版本
-Wall编译阶段是否报告警告W:warning
-o:指定编译后的文件
-v:Verbose Compilation
The ‘-v’ option can be used to display detailed information about the exact sequence of commands used to compile and link a program.
The output produced by '-v' can be useful whenever there is a problem with the compilation process itself.It displays the full directory paths used to search for header files and libraries,the predefined preprocessor symbols,and the object files and libraries used for linking.
-c:产生目标文件,is used to compile a source file to an object file.
There is no need to use the option '-o' to specify the name of the output file in this case.When compiling with '-c' the compiler automatically creates an object file whose name is the same as the source file,with '-o' instead of the original extension.
There is no need to put the header file on the command line,since it is automatically included by the #include
linker ld:gcc main.o hello.o -o hello
-g:生成调试信息
ddd a.out
make:如果目标文件的时间戳比源文件的时间戳旧,(源文件是新的)则重新编译
Linking with external libraries:
Libraries are typically stored in special archive files with the extension ‘-a’,referred to as static Libraries.
To enable the compiler to link the external library,we need to supply the library in the gcc command line:
gcc -Wall main.c /usr/lib/libm.a -o calc
To avoid the need to specify long paths on the command line,the compiler provides a short-cut option '-l' for linking against libraries.
gcc -Wall main.c -lm -o calc
In general,the compiler option '-lNAME' will attempt to link object files with a library file 'libNAME.a' in the standard library directories.
-I:search .h//系统目录下找
-L:search .o
ar:Creating a Library with ar
The GNU archiver ar combines a collection of objects files into a single archive file,also know as a library.An archive file is simply a convenient way of distributing a large number of related object files together.
ar cr libNAME.a file1.o file2.o... filen.o
The archiver ar also provides a "table of contents" option 't' to list the object files in an existing library.
ar t libNAME.a
gcc -Wall main.c libhello.a -o hello//位置不能颠倒
==gcc -Wall main.c -L. -lhello -o h3
==gcc -Wall main.c -L/home/wilson -lhello -o h4
Shared libraries are handled with a more advanced from of linking,which makes the executable file smaller.They use the extension'.so',which stands for shared object.
Additional Warning Options:
-W:
-Wconversion:
-Wshadow:
-Wcast-qual:
-Wwrite-Strings:
-Wtraditional:
Using the Preprocessor:
GNU C preprocessor cpp, is part of the GCC package.The preprocessor expands macros in source files before they are compiled.It is automatically called whenever GCC processes a C or C++ program.
To define a macro with a value,the '-D' command-line option can be used in the form '-DNAME=VALUE'.
GCC provides the '-g' debug option to store additional debugging information in object files and executables.This debugging information allows errors to be traced back from a specific machine instruction to the corresponding line in the original source file.It also allows the execution of a program to be traced in a debugger, such as the GNU Debugger gdb.
gdb a.out core.2997
backtrace
quit
cat -n null.c
Compiling with Optimization
ldd a.out
The command ldd examines an executable and displays a list of the shared libraries that is needs.These libraries are referred to as the shared library dependencies of the executable.
gprof a.out
gcov a.c
grep "####" *.gcov