VS 下 C++ 制作DLL

来源:互联网 发布:比价软件推荐 编辑:程序博客网 时间:2024/05/21 17:49

博主从事C++开发已有好多年,然而只是在linux下做开发。 现如今转战于windows下面,感觉还是有很多东西不一样。

特此重新学习,顺便做下笔记。今天学的就是用VS 制作C++的DLL文件。

正式学微软的东西,当然要上微软的官网。 之前也看过其他博客的这类文章,然后只是一个傻瓜式操作,并没有做出解释,这对于业余初学者或者只用一时的情况来说已然足够,然而

博主学习网址: https://msdn.microsoft.com/en-us/library/ms235636.aspx

对于程序员来说英文不应该成为阻挡前进的绊脚石。 当然在这个页面下面可以选择看中文版本,然而比较坑的是,两个版本之前提供的例子都不一样了。而且好几段话直接就跳过去没翻译了,比如我下面贴的第一段英文。。。

下面说正题

/*-------------------------------------------------------------------------------------------*/

使用dll的好处在官网上的描述是:

1. 代码的重复使用。

2. 修改dll里边的东西,可以不用重新编译其他调用这个dll的代码。 原文如下:

Using a library is a great way to reuse code. Rather than re-implementing the same routines in every program that you create, you write them one time and then reference them from apps that require the functionality. By putting code in the DLL, you save space in every app that references it, and you can update the DLL without recompiling all of the apps that use it.


另外一个要注意的点就是 制作C++ DLL 跟 调用dll的C++程序  最好是使用同一个编译工具或者环境。 这样子里边的命名规格或者风格才会一样。当然其他语言的程序也可以调用该DLL, 只要他们的编译器是使用的C的调用约定就可以了。    (这个调用约定 我也只是半理解。C++代码在编译的中间过程的时候,函数的名字会变得有点乱七八糟,估计这个就是C++的编译器命名约定。)

This walkthrough creates a DLL that can be called from apps that use C++ calling conventions. This requires both the DLL and the client app to be built by using the same compiler toolset, so that the internal naming conventions match. It's also possible to create DLLs that can be called from apps written in other languages and built using other compilers by using the C calling convention. For more information about specifying C linkage, see Exporting C++ Functions for Use in C-Language Executables. For information about how to create DLLs for use with other languages, see Calling DLL Functions from Visual Basic Applications.


接下来就动手做dll吧。 

先打开vs,然后新建项目,选择C++的控制台程序,在设置向导处点击下一步之后,选择DLL。然后点击完成。  我在这里给项目命名为MathLibrary



创建完成之后发现 里边只有MathLibrary.cpp 并没有对应的头文件。 

先添加一个头文件,起一个一样的名字放进去。 然后把下面这份代码拷贝到里边。

// MathLibrary.h - Contains declaration of Function class  #pragma once  #ifdef MATHLIBRARY_EXPORTS  #define MATHLIBRARY_API __declspec(dllexport)   #else  #define MATHLIBRARY_API __declspec(dllimport)   #endif  namespace MathLibrary  {      // This class is exported from the MathLibrary.dll      class Functions      {      public:          // Returns a + b          static MATHLIBRARY_API double Add(double a, double b);          // Returns a * b          static MATHLIBRARY_API double Multiply(double a, double b);          // Returns a + (a * b)          static MATHLIBRARY_API double AddMultiply(double a, double b);      };  }  

可以先注意一下预编译那行,dll默认的预编译头格式是 PROJECTNAME_EXPORTS   跟平常的头文件有差别。 还有就是 __declspec(dllexport)这两个,一个导出,一个导入。 我顺着网站给处的几个网站点了进去看了看,硬是没有说导入是怎么玩的。 然而希望这跟我要做个dll出来关系不大。


接下来就是在cpp文件里边实现这个类,注意一下的是,如果想要函数可以导出,就在函数前面加上static MATHLIBRARY_API,一般来说,通过向导创建的dll文件,会在预处理工具里边定义了MATHLIBRARY_EXPORTS,也就不会出现dllimport这个选项了。不想写代码的同学可以直接把下面这段代码抄上。

// MathLibrary.cpp : Defines the exported functions for the DLL application.  // Compile by using: cl /EHsc /DMATHLIBRARY_EXPORTS /LD MathLibrary.cpp  #include "stdafx.h"  #include "MathLibrary.h"  namespace MathLibrary  {      double Functions::Add(double a, double b)      {          return a + b;      }      double Functions::Multiply(double a, double b)      {          return a * b;      }      double Functions::AddMultiply(double a, double b)      {          return a + (a * b);      }  }  

接下来就是使用dll,这个时候按照官网上的描述,不要close这个项目,直接新建一个项目,然后添加到这个解决方案里边。然后我的VS就崩溃了。。。




预编译处没有写#ifndef  不知道是否具有防止重复加载的功能?






原创粉丝点击