Building a Dynamic Library with an IDE

Problem

You wish to use your IDE to build a dynamic library from a collection of C++ source files, such as those listed in Example 1-2.

Solution

The basic outline is as follows:

  1. Create a new project and specify that you wish to build a dynamic library rather than static library or an executable.
  2. Choose a build configuration (e.g., debug versus release, single-threaded versus multithreaded).
  3. Specify the name of your library and the directory where it should be created.
  4. Add your source files to the project.
  5. On Windows, define any macros necessary to ensure that your dynamic library's symbols will be exported using _ _declspec(dllexport).
  6. If necessary, specify one or more directories where the compiler should search for included headers. See Recipe 1.13.
  7. Build the project.

As with Recipe 1.11, the steps in this outline vary somewhat depending on the IDE. The second step is covered in detail in Recipe 1.21, Recipe 1.22, and Recipe 1.23. For now, you should use default settings wherever possible.

For example, here's how to build a dynamic library from the source code in Example 1-2 using the Visual C++ IDE.

Select Newimages/U2192.jpg border=0> Project from the File menu, select Visual C++[10] in the left pane, select Win32 Console Application and enter libgeorgeringo as your project's name. From the Win32 Application Wizard go to Application Settings, select DLL and Empty Project, and press Finish. You should now have an empty project with two build configurations, Debug and Release, the former being the active configuration.

[10] In versions of Visual C++ prior to Visual C++ 2005, this option was labeled Visual C++ Projects.

Next, display your project's property pages by right-clicking on the project's name in the Solution Explorer and selecting Properties. Go to Configuration Propertiesimages/U2192.jpg border=0> Linkerimages/U2192.jpg border=0> General and enter the pathname of your project's output file in the field labeled Output File. The directory portion of the pathname should point to the directory binaries which you created at the beginning of this chapter; the file name portion should be libgeorgeringo.dll. Similarly, go to Configuration Propertiesimages/U2192.jpg border=0> Linkerimages/U2192.jpg border=0> Advanced and enter the pathname of your DLL's import library in the field labeled Import Library. The directory portion of the pathname should point to the directory binaries which you created at the beginning of this chapter; the file name portion should be libgeorgeringo.lib

Next, use Add Existing Item... from the Project menu to add the source files listed in Example 1-2 to your project.

Instead of using Add Existing Item... to add the source files from Example 1-2 to your project, you can use Add New Item... to create blank source files and add them to your project. Then you can type or paste the content from Example 1-2 into the newly created files. Similar remarks hold for other IDEs.

Your project's property pages should now contain a node labeled C/C++. Go to Configuration Propertiesimages/U2192.jpg border=0> C/C++images/U2192.jpg border=0> Code Generation and define the macro GEORGERINGO_DLL, as described in Recipe 1.19. Next, go to Configuration Propertiessimages/U2192.jpg border=0> C/C++images/U2192.jpg border=0> Code Generation and specify Multi-threaded Debug DLL as the Runtime Library.

You can now build your project by selecting Build Solution from the Build menu. Verify that two files named libgeorgeringo.dll and libgeorgeringo.lib have been created in the directory binaries.

Discussion

As you saw in Recipe 1.11, each IDE provides its own way to create a project, specify its configuration properties, and add files to it. Let's look at how to build a dynamic library using CodeWarrior, C++Builder, and Dev-C++.

CodeWarrior

Select New... from the File menu, and select the Project tab of the New dialog. Enter libgeorgeringo.mcp as your project's name, select a location where your project's configuration files should be stored, and double-click Mac OS C++ Stationery. From the New Project dialog, expand the nodes Mac OS X Mach-O and Standard Console, then double-click C++ Console Mach-O. You should now have a project with two targets, Mach-O C++ Console Debug and Mach-O C++ Console Final, the former being the default target.

Since you will need to refer to these targets by name when you create a project which depends on this project, you should give the targets descriptive names. For now, rename just the debug target, as follows. Select the Targets tab on your project's window, and double-click on the name of the debug target to display the Target Settings Window. Then go to Target images/U2192.jpg border=0> Target Settings and enter libgeorgeringo Debug in the field labeled Target Name.

Next, from the Target Settings Window, go to Target images/U2192.jpg border=0> PPC Mac OS X Target. Specify Dynamic Library as the Project Type, and enter libgeorgeringo.dylib in the field labeled File Name. Go to Target images/U2192.jpg border=0> Target Settings and press Choose... to specify the directory binaries as the location where the output file libgeorgeringo.dylib should be created. Then, go to Linker images/U2192.jpg border=0> PPC Mac OS X Linker. Select Use #pragma from the drop-down list labeled Export Symbols, and make sure that the field labeled Main Entry Point is empty.

Finally, select the Files tab on your project's window and remove the existing source files and libraries files by dragging them to Trash. Use Add Files... from the Project menu to add the source files listed in Example 1-2 to your project. Then use Add Files... to add the file dylib1.o, in the directory /usr/lib, and the files MSL_All_Mach-O_D.dylib and MSL_Shared_AppAndDylib_Runtime_D.lib, in the directory Metrowerks CodeWarrior/MacOS X Support/Libraries/Runtime/Runtime_PPC/Runtime_MacOSX/Libs. If you were configuring the release target instead of the debug target, you would add the libraries MSL_All_Mach-O.dylib and MSL_Shared_AppAndDylib_Runtime.lib instead. You can now build your project by selecting Make from the Project menu. Verify that a file named libgeorgeringo.dylib has been created in the directory binaries.

C++Builder

Select New images/U2192.jpg border=0> Other... from the File menu and then select DLL Wizard. From the DLL Wizard dialog, select C++ and Multi Threaded. You should now have a project containing a single source file Unit1.cpp. Remove Unit1.cpp from the project by right-clicking and selecting Remove From Project. Select Save Project As... on the File menu, select a directory for storing your project's configuration files, and enter libgeorgeringo.bpr as your project's name.

Next, select Options... from the Project menu to display the Project Options dialog. Then go to Directories and Conditionals and use the control next to Final output to specify that your project's output files should be created in the directory binaries. By default, this file will be created in the same directory as libjohnpaul.bpr. If you wish, you can also use the control next to Intermediate output to specify where object files should be created. By default they will be created in the same directory as the source files.

Next, define the macro GEORGERINGO_DLL, as described in Recipe 1.19.

Finally, use Add to Project... from the Project menu to add the source files listed in Example 1-2 to your project. You can now build your project by selecting Make libgeorgeringo from the Project menu. Verify that two files named libgeorgeringo.dll and libgeorgeringo.lib have been created in the directory binaries.

Dev-C++

Select New images/U2192.jpg border=0> Project... from the File menu. From the New project dialog, select DLL and C++ Project and enter libgeorgeringo as your project's name. After pressing OK, specify the location where your project's configuration file should be located.

Next, select Project Options from the Project menu to display the Project Option dialog. Then go to Build Options and verify that your project's output file is named libjohnpaul.dll. Enter the pathname of the directory binaries under Executable output directory. If you wish, you can enter the directory where object files will be created under Object file output directory.

Now, define the macro GEORGERINGO_DLL, as described in Recipe 1.19.

Finally, remove any existing source files from your project by right-clicking and selecting Remove file. Use Save Project as... from the File menu to save your project's configuration file libgeorgeringo.dev. Then use Add to project from the Project menu to add the source files listed in Example 1-2 to your project. Build your project by selecting Compile from the Execute menu , and verify that a file named libjohnpaul.a has been created in the directory binaries.

See Also

Recipe 1.4, Recipe 1.9, Recipe 1.16, Recipe 1.19, and Recipe 1.23

Категории