在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工具查看导出函数

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值