您的位置:首页 > 运维架构 > Linux

同时能在Win32和Linux下编译的动态链接库(dll、so)导出类示例

2010-03-13 22:46 351 查看
转帖自:http://blog.chinaunix.net/u3/110228/showart_2151924.html

动态链接库的生成:
Linux下动态链接库(shared object)的生成与生成可执行文件类似,源文件无需任何改动。只需要在编译的时候使用 –shared 选项,即可生成动态库文件。

Win32下,可以通过VC6.0 的“Win32 Dynamic-Link Library”来生成DLL工程。Win32下要想使用动态链接库,需要在编译时准备静态库文件LIB文件,在运行时调用动态链接库文件DLL文件。

动态链接库的调用:
在Linux下,就像调用普通函数一样调用;在Win32下,有以下两种调用方式

先用预编译指令#pragma comment(lib,"MyDll.lib") 导入库文件,之后像调用普通函数一样调用dll导出函数和导出类

对于dll导出的全局函数,可以使用LoadLibrary和GetProcAddress获得函数地址,之后在通过函数指针调用。

以下代码在Win32和Linux下都能编译生成动态链接库文件。
===========================================

//--------------------------------------------------------
// MyDll.h
//--------------------------------------------------------

#ifndef _MY_DLL_H_
#define _MY_DLL_H_

#undef DLL_SPEC
#if defined (WIN32)
#if defined (DLL_DEFINE)
#define DLL_SPEC _declspec(dllexport)
#else
#define DLL_SPEC _declspec(dllimport)
#endif
#else
#define DLL_SPEC
#endif

namespace MyDll{

// class in dll
class DLL_SPEC CDllClass{
public:
static CDllClass* NewInstance(int x, int y);
void Print();

protected:
CDllClass(int x,int y);
protected:
int m_cx;
int m_cy;
};

// function in dll
extern "C" DLL_SPEC int global_mul(int a, int b);
}

#endif // _MY_DLL_H_

//--------------------------------------------------------
// MyDll.cpp
//--------------------------------------------------------

#include <cstdio>

#if defined (WIN32)
#define DLL_DEFINE
#endif

#include "MyDll.h"

namespace MyDll{
// functions in class
CDllClass* CDllClass::NewInstance(int x,int y)
{
return new CDllClass(x,y);
}

void CDllClass::Print()
{
printf("x=%d,y=%d/n",m_cx,m_cy);
}

CDllClass::CDllClass(int x, int y)
:m_cx(x),m_cy(y)
{
}

// global functions
int global_mul(int a, int b)
{
return a * b;
}

}
对于上述动态库文件,可以使用以下的代码调用
===========================================
//--------------------------------------------------------
//test.cpp
//--------------------------------------------------------
#include <cstdlib>
#include <cstdio>

#ifdef WIN32
#include <windows.h>
#endif // WIN32

#undef DLL_DEFINE
#include "../MyDll/MyDll.h"

// a simple way to import a dynamic lib
// can be used both in win32 and linux
#ifdef WIN32
#pragma comment(lib,"MyDll.lib")
#endif

void test_dll(int x, int y)
{
MyDll::CDllClass *pDll;
int z = 0;

pDll = MyDll::CDllClass::NewInstance(x,y);
pDll->Print();

z = MyDll::global_mul(x,y);

printf("x=%d,y=%d,z=%d,x*y=%d/n",x,y,z,x*y);
}

// an efficient way to import functions in dynamic lib
void test_dll_func(int x,int y)
{
int z = 0;

#ifdef WIN32
typedef int (*P_FUNC) (int,int);
P_FUNC fMul = 0;
HINSTANCE hDll = LoadLibrary("MyDll.dll");

if ( hDll == NULL){
printf("Error: can not load library/n");
return;
}
fMul = (P_FUNC)GetProcAddress(hDll,"global_mul");
if ( fMul == NULL){
printf("Error: can not get function/n");
return;
}
z = fMul(x,y);
#else
z = MyDll::global_mul(x,y);
#endif

printf("In test_dll_func: ");
printf("x=%d,y=%d,",x,y);
printf("mul=%d,x*y=%d/n",z,x*y);

#ifdef WIN32
FreeLibrary(hDll);
#endif
}

//--------------------------------------------------------
//main.cpp
//--------------------------------------------------------
#include <cstdlib>
#include <cstdio>
#include <ctime>

void test_dll(int x, int y);
void test_dll_func(int x,int y);

int main()
{
srand(time(0));

int x = rand() % 100 + 1;
int y = rand() % 100 + 1;

test_dll(x,y);
test_dll_func(x,y);

return 0;
}

上述代码都既能在Win32下编译,也能在Linux下编译。

示例文件:UseDll.zip

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: