在DLL中导出另一静态库中的函数

时间:2023-03-09 01:54:57
在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工具查看导出函数

相关文章