zoukankan      html  css  js  c++  java
  • 在DLL中导出另一静态库中的函数

    开发环境:
    win7_x64、VS2013

    应用场景:
    动态库A依赖动态库B,而动态库B又使用了静态库C;有些情况下,我们需要将C从B里面导出,然后提供给A使用。

    正文:
    Step1:
    1、新建测试静态库TestStatic
    2、添加TestA.h、TestA.cpp、TestB.h和Testb.cpp文件
    3、添加以下代码导出TestA类和testB函数
    TestA.h

    #pragma once
    #if defined _Use_Static_Export_
    #define _Static_Export_ _declspec(dllexport)
    #elif defined _Use_Static_Import
    #define _Static_Export_ _declspec(dllimport)
    #else
    #define _Static_Export_
    #endif
    
    class TestA {
    public:
        _Static_Export_ TestA();
        _Static_Export_ ~TestA();
        _Static_Export_ int test();
        _Static_Export_ void testb();
    };

    TestA.cpp

    #include "TestA.h"
    TestA::TestA() { int i = 10; }
    TestA::~TestA() { int i = 10; }
    int TestA::test() { return 10; }
    void TestA::testb() {}

    TestB.h

    #pragma once
    
    #if defined _Use_Static_Export_
        #ifndef _Static_Export_
            #define _Static_Export_ __declspec(dllexport)
        #endif
    #elif defined _Use_Static_Import
        #ifndef _Static_Export_
            #define _Static_Export_ __declspec(dllimport)
        #endif
    #else
        #ifndef _Static_Export_
            #define _Static_Export_
        #endif
    #endif
    
    namespace Test {
        _Static_Export_ void testB();
    }

    TestB.cpp

    #include "TestB.h"
    
    namespace Test {
        void testB() {}
    }

    Step2:新建测试动态库TestDll
    1、在TestDll.cpp文件中添加以下代码,从当前DLL中导出TestA类和test函数
    TestDll.cpp

    #define _Use_Static_Export_
    #include "./../TestStatic/TestA.h"
    #include "./../TestStatic/TestB.h"
    
    void dll_export()
    {
        TestA a;
        Test::testB();
    }

    Step3:编译项目TestStatic和项目TestDll,在生成的TestDll.dll中可利用DEPENDS.EXE工具查看导出函数

  • 相关阅读:
    [zz][openstack swift]0 swift介绍
    [zz]/usr、/var和/etc目录
    [zz]使用 watchdog 构建高可用性的 Linux 系统及应用
    [zz]sheep dog的readme
    [zz] Consistent Hashing Ring
    [zz]为什么这些死脑筋们在用 VI ?
    libvirt 网络
    [zz]libcapng
    libvirt 创建的文件
    电商购物网站如何调用第三方支付平台(支付宝,财付通,盛付通等)
  • 原文地址:https://www.cnblogs.com/dongc/p/5225103.html
Copyright © 2011-2022 走看看