2. GNU Compiler Collection
Created Friday 07 February 2014
Compile specifying output file name:
g++ -o hello hello.cpp
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
To see a detailed compilation process use a verbose option:
gcc -v hello.c -o hello.exe
.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)
.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".
- Smaller executables — obvious
- Saves disk space — one copy of a library can be shared between multiple programs
- Saves memory — OS may allow one copy of a library in memory to be used by all running programs
- 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: