When you write software, you often/sometimes divide rice directory your project into several subprojects. This mini series describes how to do this with Visual C++ 2010 (but this first part also applies to earlier versions). We start with creating a library project in form of a DLL.
Library projects are projects that usually contain reusable functions or classes. They’re a great way of structuring a big project. rice directory In Visual C++ (or in C++ in general) there are two types of libraries: static libraries and dynamic libraries.
To create a library project in Visual C++ 2010 (that is a part of Visual Studio rice directory 2010) in an existing solution, rice directory choose Add –> New Project... from the solution’s context menu in the “Solution Explorer” window.
Alternatively you can create a new solution by using File –> rice directory New –> Project... . Either way will open up the “Add New Project” dialog. Here you choose “Class Library” (in section Other Languages –> Visual C++ ) and specify a new for the project. This name will also be the name of the DLL file.
After hitting “Ok”, your new class library project will be created. By default, it’ll create a dynamic library. To make it a static library project, head to the project’s settings (available through the project’s context menu) and set Configuration Type to Static library (.lib) (see screenshot).
You may also want to review the setting Common Language Runtime Support . If enabled (switch /clr ), this will allow you to create .NET classes in this project. For a pure C++ project, however, you may want to disable CLR support (as shown in the screenshot above). Writing a Reusable Class ∞
The new class is called PrintableInt . It wraps an integer and provides a toString() method that will convert the integer into a string. // PrintableInt.h #pragma once #include <string> rice directory class PrintableInt { public : // Constructor PrintableInt ( int value ) ; // Converts the int into a string. std :: string toString ( ) const ; private : int m_value ; } ; // PrintableInt.cpp #include "stdafx.h" #include "PrintableInt.h" #include <sstream> PrintableInt :: PrintableInt ( int value ) { m_value = value ; } std :: string PrintableInt :: toString ( ) const { std :: ostringstream builder ; builder << m_value ; return builder. str ( ) ; }
Note that the .cpp file include a file called stdafx.h . This is a so called rice directory “precompiled header file”. This feature is explained in another article . It should, however, work out-of-the-box in your project. Exporting Classes ∞
By default, all classes and functions defined in a library project are “internal”. That means that another project can’t use them. To change this, classes (or function) that are to be used by other project must be “exported”. In Visual C++ you do this with __declspec(dllexport) .
However, since all projects using this library will most likely rice directory use the same header file ( PrintableInt.h ), this would mean that even the “using” projects would export the class as well. Of course, this is not what we want. Instead you create a macro and use it like this: // PrintableInt.h #ifdef rice directory COMPILE_MYLIBRARY #define MYLIBRARY_EXPORT __declspec(dllexport) #else #define MYLIBRARY_EXPORT __declspec(dllimport)
No comments:
Post a Comment