一个lib和dll的例子 来自MSDN
2007-06-22 14:10
281 查看
环境:Visual studi
To create a new static library project
From the File menu, select New and then Project….From the Project types pane, under Visual C++, select Win32.
From the Templates pane, select Win32 Console Application.
Choose a name for the project, such as MathFuncsLib, and enter it in the Name field. Choose a name for the solution, such as StaticLibrary, and enter it in the Solution Name field.
Press OK to start the Win32 application wizard. From the Overview page of the Win32 Application Wizard dialog, press Next.
From the Application Settings page of the Win32 Application Wizard, under Application type, select Static library.
From the Application Settings page of the Win32 Application Wizard, under Additional options, deselect Precompiled header.
Press Finish to create the project.
To add a class to the static library
To create a header file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code. From the Templates pane, select Header File (.h). Choose a name for the header file, such as MathFuncsLib.h, and press Add. A blank file will be displayed.Add a simple class named MyMathFuncs to do common mathematical operations, such as addition, subtraction, multiplication, and division. The code should resemble the following:
// MathFuncsLib.h namespace MathFuncs { class MyMathFuncs { public: // Returns a + b static double Add(double a, double b); // Returns a - b static double Subtract(double a, double b); // Returns a * b static double Multiply(double a, double b); // Returns a / b // Throws DivideByZeroException if b is 0 static double Divide(double a, double b); }; } |
Implement the functionality for MyMathFuncs in the source file. The code should resemble the following:
// MathFuncsLib.cpp // compile with: /c /EHsc #include "MathFuncsLib.h" #include <stdexcept> using namespace std; namespace MathFuncs { double MyMathFuncs::Add(double a, double b) { return a + b; } double MyMathFuncs::Subtract(double a, double b) { return a - b; } double MyMathFuncs::Multiply(double a, double b) { return a * b; } double MyMathFuncs::Divide(double a, double b) { if (b == 0) { throw new invalid_argument("b cannot be zero!"); } return a / b; } } |
Note |
---|
If building from the command line, you must build the program in two steps. First, compile the code using Cl.exe with the /c compiler option (cl /c /EHsc MathFuncsLib.cpp). This will create an object file named MathFuncsLib.obj. For more information, see /c (Compile Without Linking). Second, link the code using the Library Manager Lib.exe ( lib MathFuncsLib.obj). This will create the static library MathFuncsLib.lib. For more information on the Library Manager, see LIB Reference. |
To create an application that references the static library
To create an application that will reference and use the static library that was just created, from the File menu, select New and then Project….From the Project types pane, under Visual C++, select Win32.
From the Templates pane, select Win32 Console Application.
Choose a name for the project, such as MyExecRefsLib, and enter it in the Name field. Next to Solution, select Add to Solution from the drop down list. This will add the new project to the same solution as the static library.
Press OK to start the Win32 Application Wizard. From the Overview page of the Win32 Application Wizard dialog, press Next.
From the Application Settings page of the Win32 Application Wizard, under Application type, select Console application.
From the Application Settings page of the Win32 Application Wizard, under Additional options, deselect Precompiled header.
Press Finish to create the project.
To use the functionality from the static library in the console application
After you create a new Console Application, an empty program is created for you. The name for the source file will be the same as the name you chose for the project above. In this example, it is named MyExecRefsLib.cpp.To use the math routines that were created in the static library, you must reference it. To do this, select References… from the Project menu. From the Property Pages dialog, expand the Common Properties node and select References. Then select the Add New Reference… button. For more information on the References… dialog, see References, Common Properties, <Projectname> Property Pages Dialog Box.
The Add Reference dialog is displayed. This dialog lists all the libraries that you can reference. The Project tab lists all the projects in the current solution and any libraries they contain. From the Projects tab, select MathFuncsLib. Then select OK. For more information on the Add Reference dialog, see Add Reference Dialog Box.
To reference the header files of the static library, you must modify the include directories path. To do this, from the Property Pages dialog, expand the Configuration Properties node, then the C/C++ node, and select General. Next to Additional Include Directories, type in the path to the location of the MathFuncsLib.h header file. //把头文件包括进来.
You can now use the MyMathFuncs class in this application. Replace the contents of MyExecRefsLib.cpp with the following code:
Copy Code | |
---|---|
// MyExecRefsLib.cpp // compile with: /EHsc /link MathFuncsLib.lib #include <iostream> #include "MathFuncsLib.h" using namespace std; int main() { double a = 7.4; int b = 99; cout << "a + b = " << MathFuncs::MyMathFuncs::Add(a, b) << endl; cout << "a - b = " << MathFuncs::MyMathFuncs::Subtract(a, b) << endl; cout << "a * b = " << MathFuncs::MyMathFuncs::Multiply(a, b) << endl; cout << "a / b = " << MathFuncs::MyMathFuncs::Divide(a, b) << endl; return 0; } |
To run the application
Make sure MyExecRefsLib is selected as the default project. From the Solution Explorer, select MyExecRefsLib, and then select Set As StartUp Project from the Project menu.To run the project, select Start Without Debugging from the Debug menu. The output should look like this:
Copy Code | |
---|---|
a + b = 106.4 a - b = -91.6 a * b = 732.6 a / b = 0.0747475 |
To create a new dynamic link library (DLL) project
From the File menu, select New and then Project….
From the Project types pane, under Visual C++, select Win32.
From the Templates pane, select Win32 Console Application.
Choose a name for the project, such as MathFuncsDll, and enter it in the Name field. Choose a name for the solution, such as DynamicLibrary, and enter it in the Solution Name field.
Press OK to start the Win32 application wizard. From the Overview page of the Win32 Application Wizard dialog, press Next.
From the Application Settings page of the Win32 Application Wizard, under Application type, select DLL if it is available or Console application if DLL is not available. Some versions of Visual Studio do not support creating a DLL project using wizards. You can change this later to make your project compile into a DLL.
From the Application Settings page of the Win32 Application Wizard, under Additional options, select Empty project.
Press Finish to create the project.
To add a class to the dynamic link library
To create a header file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code. From the Templates pane, select Header File (.h). Choose a name for the header file, such as MathFuncsDll.h, and press Add. A blank file will be displayed.Add a simple class named MyMathFuncs to do common mathematical operations, such as addition, subtraction, multiplication, and division. The code should resemble the following:
Copy Code | |
---|---|
// MathFuncsDll.h namespace MathFuncs { class MyMathFuncs { public: // Returns a + b static __declspec(dllexport) double Add(double a, double b); // Returns a - b static __declspec(dllexport) double Subtract(double a, double b); // Returns a * b static __declspec(dllexport) double Multiply(double a, double b); // Returns a / b // Throws DivideByZeroException if b is 0 static __declspec(dllexport) double Divide(double a, double b); }; } |
To create a source file for a new class, from the Project menu, select Add New Item…. The Add New Item dialog will be displayed. From the Categories pane, under Visual C++, select Code. From the Templates pane, select C++ File (.cpp). Choose a name for the source file, such as MathFuncsDll.cpp, and press Add. A blank file will be displayed.
Implement the functionality for MyMathFuncs in the source file. The code should resemble the following:
Copy Code | |
---|---|
// MathFuncsDll.cpp // compile with: /EHsc /LD #include "MathFuncsDll.h" #include <stdexcept> using namespace std; namespace MathFuncs { double MyMathFuncs::Add(double a, double b) { return a + b; } double MyMathFuncs::Subtract(double a, double b) { return a - b; } double MyMathFuncs::Multiply(double a, double b) { return a * b; } double MyMathFuncs::Divide(double a, double b) { if (b == 0) { throw new invalid_argument("b cannot be zero!"); } return a / b; } } |
Note |
---|
If building from the command line, use the /LD compiler option to specify that the output file should be a DLL. For more information, see /MD, /MT, /LD (Use Run-Time Library). |
To create an application that references the dynamic link library
To create an application that will reference and use the dynamic link library that was just created, from the File menu, select New and then Project….From the Project types pane, under Visual C++, select Win32.
From the Templates pane, select Win32 Console Application.
Choose a name for the project, such as MyExecRefsDll, and enter it in the Name field. Next to Solution, select Add to Solution from the drop down list. This will add the new project to the same solution as the dynamic link library.
Press OK to start the Win32 Application Wizard. From the Overview page of the Win32 Application Wizard dialog, press Next.
From the Application Settings page of the Win32 Application Wizard, under Application type, select Console application.
From the Application Settings page of the Win32 Application Wizard, under Additional options, deselect Precompiled header.
Press Finish to create the project.
To use the functionality from the class library in the console application
After you create a new Console Application, an empty program is created for you. The name for the source file will be the same as the name you chose for the project above. In this example, it is named MyExecRefsDll.cpp.To use the math routines that were created in the dynamic link library, you must reference it. To do this, select References… from the Project menu. From the Property Pages dialog, expand the Common Properties node and select References. Then select the Add New Reference… button. For more information on the References… dialog, see References, Common Properties, <Projectname> Property Pages Dialog Box.
The Add Reference dialog is displayed. This dialog lists all the libraries that you can reference. The Project tab lists all the projects in the current solution and any libraries they contain. From the Projects tab, select MathFuncsDll. Then select OK. For more information on the Add Reference dialog, see Add Reference Dialog Box.
To reference the header files of the dynamic link library, you must modify the include directories path. To do this, from the Property Pages dialog, expand the Configuration Properties node, then the C/C++ node, and select General. Next to Additional Include Directories, type in the path to the location of the MathFuncsDll.h header file.
Dynamic link libraries are not loaded by the executable until runtime. You must tell the system where to locate MathFuncsDll.dll. This is done using the PATH environment variable. To do this, from the Property Pages dialog, expand the Configuration Properties node and select Debugging. Next to Environment, type in the following:
PATH=<path to MathFuncsDll.dll file>, where <path to MathFuncsDll.dll file> is replaced with the actual location of MathFuncsDll.dll. Press OK to save all the changes made.
Note |
---|
If you intend to run the executable from the command line rather than from within Visual Studio, then you must manually update the PATH environment variable from the command prompt as follows: set PATH=%PATH%;<path to MathFuncsDll.dll file>, where <path to MathFuncsDll.dll file> is replaced with the actual location of MathFuncsDll.dll. |
Copy Code | |
---|---|
// MyExecRefsDll.cpp // compile with: /EHsc /link MathFuncsDll.lib #include <iostream> #include "MathFuncsDll.h" using namespace std; int main() { double a = 7.4; int b = 99; cout << "a + b = " << MathFuncs::MyMathFuncs::Add(a, b) << endl; cout << "a - b = " << MathFuncs::MyMathFuncs::Subtract(a, b) << endl; cout << "a * b = " << MathFuncs::MyMathFuncs::Multiply(a, b) << endl; cout << "a / b = " << MathFuncs::MyMathFuncs::Divide(a, b) << endl; return 0; } |
To run the application
Make sure MyExecRefsDll is selected as the default project. From the Solution Explorer, select MyExecRefsDll, and then select Set As StartUp Project from the Project menu.To run the project, select Start Without Debugging from the Debug menu. The output should look like this:
Copy Code | |
---|---|
a + b = 106.4 a - b = -91.6 a * b = 732.6 a / b = 0.0747475 |
相关文章推荐
- 一个lib和dll的例子 来自MSDN
- 一个调试、利用msdn的经典例子
- 来自sklearn中的一个例子——Classifier comparison
- 委托和事件的一个例子解读From msdn of Microsoft
- 自制简单的Java下载器——来自《Java高级编程》的一个关于线程的例子(带上部分注释)
- stl(三)------这是一个MSDN中的一个关于map 中find函数的说明 很好的一个例子
- DDK中生成一个DLL source文件写的例子
- .NET通过 P/Invoke调用win32 dll的一个实际例子
- 一个例子,理解java中的继承(来自《疯狂java讲义》)
- mingw 将windows lib.dll 转成 lib.a 例子 (转)
- 自制简单的Java下载器——来自《Java高级编程》的一个关于线程的例子(带上部分注释)
- 在Debug模式下,如何给.lib和.dll添加一个d标记(*d.lib,*d.dll)
- Java使用JNI调用VC的DLL的一个小例子
- MSDN中关于结构成员对齐的一个例子
- 一个 ORACLE 调用 DLL 的例子(原创)
- ICSharpCode.SharpZipLib.dll实现压缩解压一个树形目录
- 供LoadRunner调用的一个简单Dll例子
- 关于模板编程的一个例子 来自深度探索c++对象模型
- 利用MFC调用libvlc.dll作一个简单的播放器
- wxpython和matplotlib显示图像的一个例子