2. GNU Compiler Collection

Created Friday 07 February 2014



Compile specifying output file name:
g++ -o hello hello.cpp

Commonly used:
g++ -Wall -g -o Hello.exe Hello.cpp

-o: specifies the output executable filename.
-Wall: prints "all" warning messages.
-g: generates additional symbolic debuggging information for use with gdb debugger.

Compile and link separately:
g++ -c -Wall -g hello.cpp
g++ -g -o hello hello.o

Compile multiple files with a single command:
g++ -o file file1.cpp file2.cpp

Compile each source separately and link them in the next step:
g++ -c file1.cpp
g++ -c file2.cpp
g++ -o file file1.o file2.o

To compile and link the source to a shared library (.dll in Win and .so in Lin) use -shared option
Check http://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html

To see a detailed compilation process use a verbose option:
gcc -v hello.c -o hello.exe

Static libraries:
.lib in Wind, .a in Lin/Un — library's code is copied into the executable on linking. Static library may be created with the archive program 'ar.exe' (there is 'ar' program in Lin)

Shared libraries:
.dll (dynamic link library) in Win and .so (shared object) in Lin
On linking only a small table created in the executable. Before the executable starts running the operating system loads the machine code needed for the external functions — this process is known as "dynamic linking".

  1. Smaller executables — obvious
  2. Saves disk space — one copy of a library can be shared between multiple programs
  3. Saves memory — OS may allow one copy of a library in memory to be used by all running programs
  4. The shared library codes can be upgraded without recompiling the program

GCC by default links to shared libraries when they available
To list the library's contents:
nm filename