《an introduction to gcc》阅读笔记

2021-08-10  本文已影响0人  老杜振熙

title: an_introduction_to_gcc.md
date: 2021-08-09 20:42:04
tags: gcc


Convention

Compiling a C program

警告相关

Compiling files independently

In general, linking is faster than compilation—in a large project with many source files, recompiling only those that have been modified can make a significant saving. The process of recompiling only the modified files in a project can be automated using GNU Make

Linking with external libraries

Compilation options

Setting search paths

Note that you should never place the absolute paths of header files in #include statements in your source code, as this will prevent the program from compiling on other systems.

Shared libraries and static libraries

因为链接一个动态库可以使得可执行文件的体积更小,并且对动态库进行版本更新不会影响到使用它的可执行文件(只要接口不变),所以,只要有可能,GCC就会默认使用动态链接

C language standards

By default, gcc compiles programs using the GNU dialect of the C language, referred to as GNU C. This dialect incorporates the official ANSI/ISO standard for the C language with several useful GNU extensions, such as nested functions and variable-size arrays.

For reference, the non-standard keywords and macros defined by the GNU C extensions are asm, inline, typeof, unix and vax

Additional warning options

Using the preprocessor

Some macros are automatically defined by the compiler—these typically use a reserved namespace beginning with a double-underscore prefix ‘__’.

Compiling for debugging

The debug option(-g) works by storing the names of functions and variables (and all the references to them), with their corresponding source code line-numbers, in a symbol table in object files and executables.

本章仅对gdb进行了简单的阐述,详细的学习,需要阅读:《Debugging with GDB: The GNU Source-Level Debugger》

#include <stdio.h>

int deref_nullptr(int *p){
    int x = *p; // 这里有问题
    return x;
}

int main(int argc, const char** argv) {
    int *p = NULL;
    int x = deref_nullptr(p);
    printf("output is %d\n", x);
    return 0;
}
# 例子
# print 打印变量的值
# backtrace 打印当前状态下的调用栈
$ gdb main core
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from main...
[New LWP 1195]
Core was generated by `./main'.
Program terminated with signal SIGSEGV, Segmentation fault.
#0  0x0000556e0426c159 in deref_nullptr (p=0x0) at ./gdb_test.cpp:4
4           int x = *p;
(gdb) print p
$1 = (int *) 0x0
(gdb) backtrace
#0  0x00005555ef33a159 in deref_nullptr (p=0x0) at ./gdb_test.cpp:4
#1  0x00005555ef33a18a in main (argc=1, argv=0x7ffd5ed3ee78) at ./gdb_test.cpp:10
(gdb)

Compiling with optimization

Source-level optimization

Optimization level

optimizations may not necessarily make a program faster in every case.

从0到3:

Compiling a C++ program

永远遵守一个规定:C++代码使用g++,而C代码使用gcc

explicit template instantiation

Platform-specific options

Code produced with a specific ‘-march=CPU’ option will be faster but will not run on other processors in the x86 family.

-m开头的option都是和平台特定的选项

Compiler-related tools

gcovgprof不想看了,先放一放

Creating a library with the GNU archiver

Examining compiled flags

# nm 例子
$ g++ -Wall -g -o ./main ./simple.cpp
$ nm main
0000000000003d90 d _DYNAMIC
0000000000003f90 d _GLOBAL_OFFSET_TABLE_
0000000000001238 t _GLOBAL__sub_I_main
0000000000002000 R _IO_stdin_used
                 w _ITM_deregisterTMCloneTable
                 w _ITM_registerTMCloneTable
00000000000011eb t _Z41__static_initialization_and_destruction_0ii
                 U _ZNSolsEPFRSoS_E@@GLIBCXX_3.4
                 U _ZNSt8ios_base4InitC1Ev@@GLIBCXX_3.4
                 U _ZNSt8ios_base4InitD1Ev@@GLIBCXX_3.4
0000000000004040 B _ZSt4cout@@GLIBCXX_3.4
                 U _ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_@@GLIBCXX_3.4
0000000000002004 r _ZStL19piecewise_construct
0000000000004151 b _ZStL8__ioinit
                 U _ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc@@GLIBCXX_3.4
00000000000021ac r __FRAME_END__
0000000000002014 r __GNU_EH_FRAME_HDR
0000000000004010 D __TMC_END__
0000000000004010 B __bss_start
                 U __cxa_atexit@@GLIBC_2.2.5
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000004000 D __data_start
0000000000001160 t __do_global_dtors_aux
0000000000003d88 d __do_global_dtors_aux_fini_array_entry
0000000000004008 D __dso_handle
0000000000003d78 d __frame_dummy_init_array_entry
                 w __gmon_start__
0000000000003d88 d __init_array_end
0000000000003d78 d __init_array_start
00000000000012d0 T __libc_csu_fini
0000000000001260 T __libc_csu_init
                 U __libc_start_main@@GLIBC_2.2.5
0000000000004010 D _edata
0000000000004158 B _end
00000000000012d8 T _fini
0000000000001000 t _init
00000000000010c0 T _start
0000000000004150 b completed.8060
0000000000004000 W data_start
00000000000010f0 t deregister_tm_clones
00000000000011a0 t frame_dummy
00000000000011a9 T main
0000000000001120 t register_tm_clones

# 例子
$ ldd main
        linux-vdso.so.1 (0x00007fff2c7d6000)
        libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fd49d627000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd49d435000)
        libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fd49d2e6000)
        /lib64/ld-linux-x86-64.so.2 (0x00007fd49d81b000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fd49d2cb000)

Further reading

The definitive guide to GCC is the official reference manual, “Using GCC”, published by GNU Press:

上一篇 下一篇

猜你喜欢

热点阅读