You wish to use your IDE to build a static library from a collection of C++ source files, such as those listed in Example 1-1.
The basic outline is as follows:
Create a new project and specify that you wish to build a static library rather than an executable or a dynamic library.
Choose a build configuration (e.g., debug versus release, single-threaded versus multithreaded).
Specify the name of your library and the directory in which it should be created.
Add your source files to the project.
If necessary, specify one or more directories where the compiler should search for included headers. See Recipe 1.13.
Build the project.
The steps in this outline vary somewhat depending on the IDE; for example, with some IDEs, several steps are combined into one or the ordering of the steps is different. 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 as much as possible.
For example, here’s how to build a static library from the source code in Example 1-1 using the Visual C++ IDE.
Select New→ Project from the File menu, select Visual C++[9] in the left pane, select Win32 Console Application, and enter libjohnpaul as your project’s name. From the Win32 Application Wizard go to Application Settings, select Static library, uncheck Precompiled header, and press Finish. You should now have an empty project with two build configurations, Debug and Release, the former being the active configuration.
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 Properties→ Librarian→ 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 libjohnpaul.lib.
Finally, use Add Existing Item... from the Project menu to add the source files listed in Example 1-1 to your project. Your project’s property pages should now contain a node labeled “C/C++.” Go to Configuration Properties → C/C++ → 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 a file named libjohnpaul.lib has been created in the directory binaries.
Tip
Instead of using Add Existing Item... to add the source files from Example 1-1 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-1 into the newly created files. Similar remarks hold for other IDEs
IDEs differ much more than toolsets. Each IDE provides its own way to create a project, specify its configuration properties, and add files to it. Nonetheless, after you have learned to use several IDEs, learning to use an additional IDE is generally easy.
When learning to use a new a new IDE, the features you should concentrate on are these:
How to create a new project
How to specify the type of project (executable, static library, or dynamic library)
How to add existing files to a project
How to create new files and add them to a project
How to specify the name of a project’s output file
How to specify include paths
How to specify library search paths
How to specify libraries on which a project depends
How to build a project
How to organize collections of projects in to a group and specify their dependencies
This recipe demonstrates many of these features. Most of the other features are covered in Recipe 1.12 and Recipe 1.13.
Let’s look at how to build a static library using CodeWarrior, C++Builder, and Dev-C++.
Select New... from the File menu,
and select the Project tab of the New dialog. Enter libjohnpaul.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→ Target Settings
and enter libjohnpaul
Debug
in the field labeled Target Name.
Next, from the Target Settings Window, go to Target→ PPC Mac OS X Target. Specify Library as the Project Type, and
enter libjohnpaul.a
in the field labeled File
Name. Go to Target→ Target Settings and
press Choose... to specify the directory binaries
as the location where the output file libjpohnpaul.a should be created.
Finally, select the Files tab on your project’s window and remove the existing source files and libraries files by dragging them to Trash. Then use Add Files... from the Project menu to add the source files listed in Example 1-1 to your project. You can now build your project by selecting Make from the Project menu. Verify that a file named libjohnpaul.a has been created in the directory binaries.
Select New → Other... from the File menu and then select Library. You should now have an empty project. Select Save Project As... on the File menu, select a directory for storing your project’s configuration files and enter libjohnpaul.bpr as your project’s name.
Next, select Options... from the Project menu to display the Project Options dialog. Then go Directories and Conditionals and use the control next to Final output to specify where your project’s output file, libjohnpaul.lib, should be created. By default this file will be created in the same directory as libjohnpaul.bpr, but you should tell C++Builder to create it in the directory binaries. 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.
Finally, use Add to Project... from the Project menu to add the source files listed in Example 1-1 to your project. You can now build your project by selecting Make libjohnpaul from the Project menu. Verify that a file named libjohnpaul.lib has been created in the directory binaries.
Select New→ Project... from the File menu. From the New project dialog, select Static Library and C++ Project and enter libjohnpaul 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 Options dialog. Then go to Build Options and verify that your project’s output file is named libjohnpaul.a. 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.
Finally, use Add to project from the Project menu to add the source files listed in Example 1-1 to your project. You can now build your project by selecting Compile from the Execute menu. Verify that a file named libjohnpaul.a has been created in the directory binaries.
Recipe 1.3, Recipe 1.8, and Recipe 1.16
[9] In versions of Visual C++ prior to Visual C++ 2005, this option was labeled Visual C++ Projects.
Get C++ Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.