Appendix
Well, I'm planning to create an Appendix which would contain some extra stuff which might not be related to the technical C++ tutorial and which I can't place under other categories.
Running your C++ program on the Linux OS:
I am just learning Linux now and of course the first thing I wanted to do was to compile and execute a C++ program. First of all, Linux uses the GNU C++ compiler and it's called g++ (gcc is the C compiler). So, first type your program in a text editor (like VI) and save the file (let's say you name it as first.cpp).
Next, at the command prompt (shell prompt) type g++ first.cpp.
$ g++ first.cpp
If there are no errors you'll just be shown the prompt again. The C++ compiler would have created an executable file called "a.out" in your directory. To execute this type:
$ ./a.out
and press ENTER. Your program will execute. So, will all out programs be named a.out? Generally you'll want to give some other name to your executable file. To do this type:
$ g++ first.cpp -o first
Now the executable file is called 'first' and you'll have to type ./first to execute the program. That's all there is! As I learn more I'll put up more here (suggestions welcome).
Creating Libraries in Linux:
For creating libraries, first create the *.cpp files and *.h files (as explained using VC++). The only difference is that here you’ll have to ask g++ to only compile your *.cpp files. If we have 3 programs disp1.cpp, disp2.cpp and disp3.cpp and we want to combine them into a library then:
$ g++ -c disp1.cpp disp2.cpp disp3.cpp
The –c option tells g++ to only compile the 3 codes. When a C++ code is compiled an object file is produced. Thus we will now have 3 object files namely disp1.o disp2.o and disp3.o.
A library (static library) is just a collection of object files. So we can club all 3 object files into one library file. Let’s call our library libf.a (*.a is a convention used to denote static libraries in Unix).
$ ar crv libf.a disp1.o disp2.o disp3.o
‘ar’ is an archiving command and this clubs all 3 object files into libf.a. If you want to use the library then just mention the name of the library at the time of linking. For example: If we have a code project.cpp which uses some function defined in disp1.cpp then:
$ g++ project –o project.cpp libf.a
g++ will now get the definitions of the functions from the correct object file present in libf.a.
Sometimes you may need to index your library (indexing the library helps in faster access and some compilers use the index to access the object code. Indexing is just like creating a table of contents for the library). To do this make use of the ‘ranlib’ command:
$ ranlib libf.a
This step should be executed after creating the library libf.a.
Go back to the Contents Page 2
Copyright © 2004 Sethu Subramanian All rights reserved.