Create and use dynamic link library dll

Source: Internet
Author: User

Create a dynamic link library Dll project 1. create a static project MathFucsDll: select the win32 console application> enter the MathFuncsDll Project Name> next> select DLL; select an empty project with additional options> finish 2. add the class MyMathFuncs to the static library: [cpp] # pragma once // MathFuncsDll. h namespace MathFuncs {class MyMathFuncs {public: static _ declspec (dllexport) double Add (double a, double B); static _ declspec (dllexport) double Substract (double, double B); static _ declspec (dllexport) double Multiply (double a, double B); static _ de Clspec (dllexport) double Divide (double a, double B) ;}}// MathFuncsDll. cpp # include "MathFuncsDll. h "# include <stdexcept> using namespace std; namespace MathFuncs {double MyMathFuncs: Add (double a, double B) {return a + B;} double MyMathFuncs :: substract (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;} 3. confirm that the generated dll file is: Project, property-> Configuration property, General-> configuration type: change to static dynamic library (. dll); compile and generate MathFuncsDll. lib and MathFuncsDll. dll creates an application that references the dynamic link library. create a console application that references the Dynamic Link Library: add the project MyExecRefsDll in the same solution: choose win32 console application> MyExecRefsDll Project Name> next> console application; select an empty project with additional options> finish 2. in the application, use the dynamic link library function 2.1 to add the header file directory so that the header file contained in the program exists (that is, it can be found): Project, property-> C/C ++-> General-> additional include directory :.. \ MathFuncsDll or: Project properties-> VC ++ directory-> include directory :.. \ MathFuncsDll2.2 add. dll file reference project, reference-> general attributes-> framework and reference-> Add reference-> The MathFuncsDll project name and project directory are displayed.-> OK or, add the library directory and additional library: Project, property-> connector-> General-> additional library directory: for example, $ (OutDir) project, property-> connector-> input-> additional dependency: MathFuncsDll. lib and mathfuncsdll.dll.pdf are in the same directory as the myexecrefsdll.exe file. 3. the program uses [cpp] # include "MathFuncsDll. h "# include <iostream> 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: Substract (a, B) <endl; cout <<"A * B =" <MathFuncs: MyMathFuncs: Multiply (a, B) <endl; cout <"a/B =" <MathFuncs :: myMathFuncs: Divide (a, B) <endl; return 0; www.2cto.com} 4. set MyExecRefsDll as the startup project, press Ctrl + F5 to compare Dll and Lib creation, and reference different creation processes during the process: _ declspec (dllexport) is added before the member function in the Dll project ), its production ~. Lib and ~. Dll files, while the lib project only produces ~. Lib file reference process is different: the Dll project reference process is almost the same as the Lib project reference process (set. h ,. the only small difference between the lib file directory and the lib file) is that the dll file created in the dllproject is in the same directory as the. exe file of the source, but the lib project does not produce the dll file.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.