Creating statically linked libraries (lib s)
In this page, you will learn how to create a statically linked library with a class packaged in it and how to link with it using another application and use the class packaged in the library.
To successfully link with a lib, the following conditions should be satisfied.
- Both the library and the application should be built for same runtime environment.
- Both the library and the application should be built in same configuration, either debug or release.(if lib is built in debug, the app also should be built in debug. if lib is built in release, the app should also be built in release)
So, follow the instructions carefully when you are creating the lib and the application.
Creating the library
- Open Visual Studio and select File->New->Project. From the dialog you get, select Visual C++->Win32->Win32 Project. Give name "libtest" to the project and select a location for save your project. Click OK.
- Click Next. Choose Static Library for Application type and click Finish.
- Replace the stdafx.h header file in the project folder with this one. This header contains the additional headers you need.
- Select project name in the solution explorer pane, which is in the left of the window and go to project->properties in the main menu.
- In configuration properties->C/C++->Code generation, change Runtime Library to Multi-Threaded (/MT)
- Select Project->Add Class from main menu. Select C++ and select Add. In the dialog you get, enter "testclass" for class name and click Finish.
- Go to Class View in the left pane and find the class you added. Right click on it and select Add->Add Function. Enter "void" for return type and "Greeting" for function name and click Finish.
- Add the following code to the function "Greeting".
void testclass::Greeting(void) { ::MessageBox(0, TEXT("have a nice day!"), TEXT("hello...."), 0);}
- Build the project. You can find the library file you built ("libtest.lib") in the "Debug" folder in project directory.
Creating the tester application which uses the library we built
- Create a dialog based MFC application which uses MFC in static library. Leave the other settings in their default values. You can read article getting started with MFC for more information. Use name "tester app" to the project.
- By using step 4 and 5 of creating lib, Change the runtime library to Multi-Threaded. As mentioned at the beginning, both the library and the application which uses the library should use same runtime library.
- Copy the library file "libtest.lib" which we built to the folder where the source files of tester application are in.
- Open project properties. In configuration properties->Linker->Input->Additional Dependencies, type libtest.lib.
- Delete 2 buttons and the static text in the main dialog of your MFC application and add a button to the dialog.
- Double click the button you added to go to the handler function for that button. Add following code to the function.
void CtesterappDlg::OnBnClickedButton1() { testclass a; a.Greeting(); }
In this function, we simply create a variable from class "testclass". We implemented this class in the library we built. Then we use the member function "Greeting()" to display the greeting. So, when the button is clicked, The greeting message should appear.
- Copy "testclass.h" which is in the folder where the source files of our library are kept to the source file folder of tester application. Then include that header file in the page where above code is written. The header file should be included after the include statements which are already there. The class definition of the class "testclass" we added to the library is in that header file. This step is done because we are using the class "testclass" in above code. In this header file, only the definition of the class is available. The implementation of the class is in the lib file "libtest.lib".
- Build and run the application. If you press the button you added to the main dialog of the application, you will get the greeting, which we wrote when we are creating the library.
If you want to build the tester application in release mode, you have to compile the library in the release mode and use that library file instead of the previous "libtest.lib". Also, remember you have to change the runtime library to Multi-Threaded again for release mode for both library and tester applications. That is because the settings for release and debug modes are kept separately (i.e. if you change the runtime library for debug mode, that do not change the runtime library for release mode).
Download source code of library
Download source code of tester application
|