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

本文介绍如何在Win32和Linux环境下创建和使用动态链接库(DLL/SO)。包括预编译指令、宏定义、类及全局函数的声明与实现,并提供了具体的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 转帖自:http://blog.chinaunix.net/u3/110228/showart_2151924.html

动态链接库的生成:
  1. Linux下动态链接库(shared object)的生成与生成可执行文件类似,源文件无需任何改动。只需要在编译的时候使用 –shared 选项,即可生成动态库文件。
  2. Win32下,可以通过VC6.0 的“Win32 Dynamic-Link Library”来生成DLL工程。Win32下要想使用动态链接库,需要在编译时准备静态库文件LIB文件,在运行时调用动态链接库文件DLL文件。
动态链接库的调用:
  1. Linux下,就像调用普通函数一样调用;在Win32下,有以下两种调用方式
  2. 先用预编译指令#pragma comment(lib,"MyDll.lib") 导入库文件,之后像调用普通函数一样调用dll导出函数和导出类
  3. 对于dll导出的全局函数,可以使用LoadLibraryGetProcAddress获得函数地址,之后在通过函数指针调用。

 

以下代码在Win32Linux下都能编译生成动态链接库文件。

===========================================


//--------------------------------------------------------

// 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

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值