GCC notes

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Read this manual, the understanding of the relevant knowledge of GCC, record.


gcc -Wall hello.c    

gcc -Wall -g hello.c // With debugging information, you can use GDB debugging.


GCC header file path is the default search:



The default lib path is



Moreover, in the /usr/local/xxx file, a higher priority than/usr/xxx

You can use

`gcc -print-prog-name=cc1` -v

`gcc -print-prog-name=cc1plus` -v

To confirm the c++/c compiler for header file position


If your header file and lib are not in these places. That will use -I -L to specify.

-I said to find header file folder

-L said to find lib file folder

An example: gcc -Wall -I/opt/gdbm-1.8.3/include -L/opt/gdbm-1.8.3/lib dbmain.c -lgdbm




If you want to load the path shell directly, can use C_INCLUDE_PATH (c) or CPLUS_INCLUDE_PATH (c++)

LIBRARY_PATH lib path.


$ C_INCLUDE_PATH=/opt/gdbm-1.8.3/include     // C++, is: CPLUS_INCLUDE_PATH


$ LIBRARY_PATH=/opt/gdbm-1.8.3/lib



The path search order is: first search specified by the -I -L, then export is entered, then /usr/local/xxx, the last is/usr/xxx



Can use the ':' to distinguish include or lib, so you don't have a specified. ex:

$ C_INCLUDE_PATH=.:/opt/gdbm-1.8.3/include:/net/include

$ LIBRARY_PATH=.:/opt/gdbm-1.8.3/lib:/net/lib

For C++ programs, use the environment variable CPLUS_INCLUDE_PATH instead of C_INCLUDE_PATH.

Said the current directory.

If you want to write separate, will be very troublesome, will the above representation:

$ gcc -I. -I/opt/gdbm-1.8.3/include -I/net/include

   -L. -L/opt/gdbm-1.8.3/lib -L/net/lib .....


5 to load a dynamic library such as libName.so, it wants to join the -lName at compile time, GCC will automatically find libName.so in the set lib path files, static library file.So file take precedence over.A, if you want to add a static library, that is to join the -static, such as:

$ gcc -Wall -static -I/opt/gdbm-1.8.3/include/ 

    -L/opt/gdbm-1.8.3/lib/ dbmain.c -lgdbm


6. /etc/The ld.so.conf file management path loading so file


7 for debugging, sometimes the program will be core dumped, then, how much you want to have a core file. First of all to join the -g option to at compile time.

Then view

$ulimit -c


$ulimit -c unlimited

$ulimit -c


If ulimit -c is 0, that is not open to generate the core file in the core dumped. The use of ulimit -c unlimited can solve.


$gdb a.out core


8. how the compiler works

(1)preprocessing(to expand macros)

(2)compilation(from source code to assembly language)

(3)assembly(from assembly language to machine code)

(4)linking(to create the final executable)


9. When all you know lib on the command line, then called undefined references error, it should pay attention to is it right? Reference sequence. Such as test.c to call m.so inside the sqrt () method. The compilation, instruction to:

$ gcc -Wall test.c -lm -o calc

If the -lm on the test.c front, will be reported to undefined references error . Because sqrt was not found in the test.c () is defined, will be backward looking for. If the -lm is placed in the front, test.c is not found.


10 can pass through

cpp -dM /dev/null

View all defined in the pre compiler macros.


The 11 is for debugging, sometimes defined

#ifdef TEST



If you want to make the intermediate code execution, it should be

gcc -DTEST test.c -o test

Is the addition of -DXXX, XXX defined labels.


12 you can use Gprof performance testing.


Analysis of 13 executable file. Let you know

$file a.out

The information indicates what.



$ldd a.out

The dynamic library executable file reference.


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Pauline at November 14, 2013 - 10:27 PM