All notes
Gcc

Options

-std=[c99, gnu99, c++11, gnu++11]
gnu.org: c++-Extensions.

gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/usr/include/libxml2 -I/tmp/pip_build_wangcf/lxml/src/lxml/includes -I/usr/include/python2.6 -c

Environment variables

Stackoverflow.

GNU.

AR
    Archive-maintaining program; default ‘ar’.
AS
    Program for compiling assembly files; default ‘as’.
CC
    Program for compiling C programs; default ‘cc’.
CXX
    Program for compiling C++ programs; default ‘g++’.
CPP
    Program for running the C preprocessor, with results to standard output; default ‘$(CC) -E’.
LINT
    Program to use to run lint on source code; default ‘lint’.
MAKEINFO
    Program to convert a Texinfo source file into an Info file; default ‘makeinfo’.
TEX
    Program to make TeX DVI files from TeX source; default ‘tex’.
RM
    Command to remove a file; default ‘rm -f’.

ARFLAGS
    Flags to give the archive-maintaining program; default ‘rv’.
ASFLAGS
    Extra flags to give to the assembler (when explicitly invoked on a ‘.s’ or ‘.S’ file).
CFLAGS
    Extra flags to give to the C compiler.
CXXFLAGS
    Extra flags to give to the C++ compiler.
CPPFLAGS
    Extra flags to give to the C preprocessor and programs that use it (the C and Fortran compilers).
LDFLAGS
    Extra flags to give to compilers when they are supposed to invoke the linker, ‘ld’, such as -L. Libraries (-lfoo) should be added to the LDLIBS variable instead.
LDLIBS
    Library flags or names given to compilers when they are supposed to invoke the linker, ‘ld’. LOADLIBES is a deprecated (but still supported) alternative to LDLIBS. Non-library linker flags, such as -L, should go in the LDFLAGS variable.
LINTFLAGS
    Extra flags to give to lint. 

Installation

Search PATH

GNUDoc on Search-Path. GCC looks in several different places for headers. if you do not instruct it otherwise, it will look for headers requested with "#include <file>" in the following order:

  1. For C++ programs, it will look in libdir/../include/c++/version, first.
  2. /usr/local/include
  3. libdir/gcc/target/version/include
  4. /usr/target/include
  5. /usr/include

In the above, target is the canonical name of the system GCC was configured to compile code for; often but not always the same as the canonical name of the system it runs on. version is the version of GCC in use.

Flags

Visibility

GNUWiki.

Why is the new C++ visibility support so useful?

By default, GCC exports all symbols in DSO. So it is strongly recommended to restrict this by gcc -fvisibility=hidden -fvisibility-inlines-hidden, and export symbols manually with __attribute__ ((visibility ("default"))).

Use nm -C -D on the outputted DSO to compare before and after to see the difference it makes.

To enable it in Cmake:

IF(UNIX)
    IF(CMAKE_COMPILER_IS_GNUCC)
         SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden -fvisibility-inlines-hidden")
    ENDIF(CMAKE_COMPILER_IS_GNUCC)
ENDIF(UNIX)

// BUILDING_DLL indicates: 1. we are building DLL instead of static lib. 2. we are exporting DLL symbol, not importing.
#if defined _WIN32 || defined __CYGWIN__
  #ifdef BUILDING_DLL
    #define DLL_PUBLIC __declspec(dllexport) // Note: actually gcc seems to also supports this syntax.
  #else
    #define DLL_PUBLIC __declspec(dllimport) // Note: actually gcc seems to also supports this syntax.
  #endif
  #define DLL_LOCAL
#else
  #if __GNUC__ >= 4
    #define DLL_PUBLIC __attribute__ ((visibility ("default")))
    #define DLL_LOCAL  __attribute__ ((visibility ("hidden")))
  #else
    #define DLL_PUBLIC
    #define DLL_LOCAL
  #endif
#endif

extern "C" DLL_PUBLIC void function(int a);
class DLL_PUBLIC SomeClass
{
   int c;
   DLL_LOCAL void privateMethod();  // Only for use within this DSO
public:
   Person(int _c) : c(_c) { }
   static void foo(int a);
};

To aid you converting old code to use the new system, GCC now supports also a #pragma GCC visibility command:


extern void foo(int);
#pragma GCC visibility push(hidden)
extern void someprivatefunct(int);
#pragma GCC visibility pop

#pragma GCC visibility is stronger than -fvisibility; it affects extern declarations as well. -fvisibility only affects definitions. This is more true for C than C++; C++ interfaces tend use classes, which are affected by -fvisibility.

Problems with C++ exceptions: Exception catching of a user defined type in a binary other than the one which threw the exception requires a typeinfo lookup.

PIC

Position independent code.

Related commands

nm

List symbols from object files.


# Lists all exported symbols in demangled form.
# -C, --demangle
# -D, --dynamic
nm -C -D library.so

# -a, --debug-syms
# -l, --line-numbers
# -r, --reverse-sort

objdump

Display information from object files.

FAQ

/usr/bin/ld: skipping incompatible /usr/lib/libSth.so when searching for -lSth

ArchBBS. It is mainly because you are on a x86_64 machine but LD looks at 32-bit libraries under /usr/lib instead of /usr/lib64.

How link to a specific version of a shared library

StackOverflow.

glibc, libc.so, libstdc++.so

glibc is composed of many pieces, e.g. ld-linux.so.2, libm.so.6, libdl.so.2.

# There are many files in glibc.
rpm -ql glibc | wc -l
# 293