http://cpp.gantep.edu.tr
C++ resources. Faculty of Engineering, University of Gaziantep
|
||||||||||
MAIN - Tutorials - Howtos - nmPrimer - Misc - Links - Contacts | ||||||||||
Last modified: Fri, 22 Jan 2010 11:57:02 +0200
Complete: ##################-- (90%) Howto: Compiling C++ ProgramsIn this how to, compilation of C++ sources will be demonstrated using the g++ compiler under Linux. A description of a Windows-based compiler is given in Compiling with the Dev-C++ compiler. g++ is a free C++ compiler, it is part of the GNU Compiler Collection. If you use the Linux operating system then there is a good chance that g++ is already installed; open a command shell and type $ g++ --version This short document explains how to perform basic g++ compilations. First let's say you have a C++ source file called pancake.cpp:
Basic compilation To compile to an executable called pancake the basic command is: $ g++ pancake.cpp -o pancake The program can be executed by typing pancake or you may need to give the path as follows
$ ./pancake $ man g++ $ g++ -Wall pancake.cpp -o pancake pancake.cpp: In function 'int main()': pancake.cpp:7: warning: unused variable 'i' Compiling multiple sources Multiple sources can be compiled into a single executable as follows:
$ g++ powers.cpp power1.cpp power2.cpp power3.cpp -o powers Header files power1.h, power2.h, and power3.h are also present. These sources are shown below. Main program powers.cpp
Function power1.cpp and it's header file power1.h
Function power2.cpp and it's header file power2.h
Function power3.cpp and it's header file power3.h
Creating and linking object files The compilation: $ g++ powers.cpp power1.cpp power2.cpp power3.cpp -o powers $ g++ powers.cpp power1.o power2.o power3.o -o powers $ g++ -c power1.cpp $ g++ -c power2.cpp $ g++ -c power3.cpp This is useful when some source files are complete and so do not require recompiling while code in other files are being developed. Creating and linking library files The object files created in the previous section can be placed into a single library which can then be linked during compilation. This is achieved by using the archive tool ar (see man ar for details). Object files can be placed in a library for example as follows: $ ar rcvf libsubs.a power1.o power2.o power3.o More objects can be added to the library with: $ ar rcvf libsubs.a another.o The contents of a library can be listed with: $ ar tv libsubs.a Functions in the library can be listed with: $ nm libsubs.a Objects can be deleted from the library with: $ ar dv libsubs.a power1.o Once a library has been created, it can be linked in a compilation as follows: $ g++ powers.cpp -L. -lsubs -o powers The -L. options tells the linker that the library can be found in the current directory. If the library is not in the current directory then you should give the path, for example: $ g++ powers.cpp -L/home/fred/cpp/lib/ -lsubs -o powers $ g++ powers.cpp -L$MYLIB -lsubs -o powers $ MYLIB=/home/fred/cpp/lib/ |
||||||||||
please send us you comments/questions/corrections |