vs创建和调用dll和lib

来源:互联网 发布:晕3d的人是天才知乎 编辑:程序博客网 时间:2024/05/21 08:48

简单记录一下在VS2008下创建dll和lib,以及调用方法

一、生成DLL和LIB

        创建控制台应用程序 、在应用程序类型中选择DLL。DLL工程创建完成。

 创建一个MyDll.h

#ifndef __MYDLL_H__#define __MYDLL_H__#ifdef MYLIBDLL#define MYLIBDLL extern "C" _declspec(dllimport) #else#define MYLIBDLL extern "C" _declspec(dllexport) #endifMYLIBDLL int Add(int plus1, int plus2);#endif

 

在创建一个MyDll.cpp

#include "stdafx.h"#include "MyDll.h"#include <iostream>using namespace std;int Add(int plus1, int plus2){int add_result = plus1 + plus2;return add_result;}

 

点击生成,则dll文件就生成了,vs2008不能直接生成lib文件,这个时候就需要我们在建立dll工程的时候再新建一个def文件,默认生成然后重新生成就能够得到lib文件了。

 

调用动态库

静态调用

#pragma comment(lib,"..\\debug\\MyDll.lib") 


动态调用......后续再添加

 

 

动态库调用实例

#include "stdafx.h"#include <Windows.h>#include <iostream>using namespace std;#pragma comment(lib, "JhemrParser.lib") typedef int (*GETEMRDATA)(string &strPath, string &strData);int _tmain(int argc, _TCHAR* argv[]){string strPath = "D:\\Dtest11-32dll-to-64dll\\00020842\\00040001";string strData = "";//int npoint = GetEmrData(strPath, strData); //静态调用char* strdll = (char*)(L"JhemrParser.dll");  //动态调用HMODULE hInst = LoadLibrary((LPCWSTR)strdll); GETEMRDATA pGetEmrData = (GETEMRDATA)GetProcAddress(hInst, "GetEmrData"); int npoint = pGetEmrData(strPath, strData);cout<<npoint<<endl;cout<<strData<<endl;return 0;}


 

 

--------------------------------------------------------------------------------------------------------

下面在资料来源于网络

(1). Dependencies (推荐使用,要求有lib源代码)

   一个项目被分成多个工程来做,一个主工程exe,其他为静态库lib

    Project-->dependencies,设置主工程的依赖为其他静态库lib

    这时,主工程的Resource Files中自动添加了lib

    在主工程中需要用到其他库的位置加入库的头文件

 

 

(2). 直接将lib添加到需要用的工程中(不太推荐,lib没能统一管理)

    提供了lib和其头文件

    选择工程-->右键-->Add Files to Project

    这时,主工程的Resource Files中自动添加了lib

    在主工程中需要用到其他库的位置加入库的头文件

 

(3).  通过工程的Link设置(推荐,lib可以统一管理)

     提供了lib和其头文件

     Project-->settings-->Link,选择Categery中的Input

     在object/library modules里输入的动态链接库对应的.lib文件名

     在Additional library path中输入动态链接库对应的.lib的路径

     在主工程中需要用到其他库的位置加入库的头文件

 

(4).  #pragma (lib, "filename.lilb")(不太推荐,lib没能统一管理)

      提供了lib和其头文件

      在主工程中需要用到其他库的位置加入#pragma (lib, "filename.lilb")

      在主工程中需要用到其他库的位置加入库的头文件

 

 

0 0