Advanced C++ - Part IIIb (creating libraries)


  1. Tell VC++ the path where to search for the libraries.
  2. Tell VC++ the name of the library.

For step 1 go to TOOLS-> OPTIONS. You’ll get a pop-up box with lots of tabs at the top (as shown below):

We need to tell the path for the Library files. So change the SHOW DIRECTORIES FOR option as shown below:

Now add the pathname where you have the file carlib.lib (click the new icon present in the options dialog box to add a new path).

In my case it is in the current project directory itself. Click OK.

Next we need to tell VC++ the name of the library we want to use.

Go to PROJECT-> SETTINGS and you’ll see a dialog box as shown below:

In the object/Library modules text field, enter our library name carlib.lib at the end of the list. This will automatically get added in the Project Options textbox at the bottom. Click OK and now build the exe file. You shouldn’t encounter any errors this time since the linker will be able to obtain the object code for all the function definitions.

What have we achieved by using a library?

The class user is now totally unaware about the implementation details of your function. Since the library is in object code, no user would try to manipulate the contents of the library file (you could open the library file in Notepad to view the contents which would appear cryptic). The library file would be something like this (the snapshot is only part of the file):

This isn’t something that a user would want to play around with!

What we created is called a static library. There are 2 types of libraries:

Static is simple to create but has a major disadvantage. If many programs make use of static libraries and if all of them are being run at the same time, then each program will have a copy of the library in memory. Shared libraries (or dynamic link libraries) overcome this problem.


Go back to the Contents Page 2


Copyright © 2004 Sethu Subramanian All rights reserved.