[Date Prev][Date Next][Date Index]
[cs485] Notes on using the g++ compiler.
Most of this is taken from the bool
Programming with GNU software -- Loukides and oram.. and published by
0'reilly
(i should probably get a soft - copy of this book)
First I will describe the different stages of compilation.
A> .c, .cc., .cpp ==> Preprocessor (Stage I) ==> Compiler (Stage II) ==>
files with .s extensions.
B> Files with .s extensions ==> Assembler (Stage III) ==> files with .o
extensions
C> Files with .o extensions + Files with .a extensions (Built-in Libraris)
==> Loader and Linker (Stage IV) ==> Executable (a.out)
Assume that we have a file prog2.cpp and we want to compile it...
then using the following command *g++ -c prog2.cpp* it will produce the
prog2.o object file, which mean sthat the g++ compiler will do all
compilation except the last stage of linking to libraries.
If you do *g++ prog2.o* , It will automatically use the object file
generated above and link it and produce a.out (the executable)
The following command is the most useful.
g++ -o prog2 prog2.cpp
which wwill directly produce the executable.
In order to use the GDB debugger, use the compilation option.
g++ -o prog2 -g prog2.cpp
after this invoke *gdb prog2* and use the debugging commands.
To define some constants use the -D flag.. for e.g
g++ -o prog2 -DDEBUG prog2.cpp
will automatically insert a #define DEBUG 1 in your source which you can
use. (i might be worng here.. need to verify this)
To specify some libraries, use the -l option .. for e.g.
g++ -o prog2 prog2.cpp -lm
will automatically include the "libm.a".. usually gcc finds this library in
/usr/lib
If you want to g++ to look into some other place first specify the path
using the -L option... for e.g.
g++ -o prog2 -L/home/shashank prog2.cpp -lm
will try to locate the libm.a in /home/shashank first .. and then continue
the search to the default place.
NOTE: -lm must be specified at the end of the command for a special reason.
Other important options
-w Supress all warning messages
-Wall Produce lots more warning messages
-O optimize the generated code
Next mail will discuss the use of Make file in detail..
Regards
Shashank
Creating a LIBRARY
---------------------
I don't know as yet how to do this, but i have read in this book that i need
to use the ar command.. Will have to read more about this in the future..
(page # 103)