0

我尝试从 Verialtor 源代码制作一个 .dll,因为他们已经实现了这种可能性。

他们使用通用处理程序typedef void* svScope 来初始化范围。.dll 也使用此句柄。现在我可以使用创建新功能

__declspec(dllexport) svScope svGetScope( void );

这是头代码 svdpi.h

#ifndef INCLUDED_SVDPI
#define INCLUDED_SVDPI

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

__declspec(dllexport) typedef void* svScope;

__declspec(dllexport) svScope svGetScope( void );

#ifdef __cplusplus
}
#endif
#endif

和一个简单的实现 svdpi.cpp

#include "svdpi.h"

svScope svGetScope() {return 0;}

我已经创建了测试文件 test.cpp

#include <stdlib.h>
#include <stdio.h>
#include "svdpi.h"

int main()
{
    svScope Scope = svGetScope();
}

我编译了库并链接了它。编译器找到库但我收到此错误

g++ -o test.exe -s test.o -L。-lsvdpi

c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: test.o:test.cpp:(. text+0xf): undefined reference to `_imp__svGetScope' collect2.exe: error: ld returned 1 exit status

4

1 回答 1

0

您需要XXTERN在函数声明上使用。您没有向我们展示任何包含必须导出的函数的实际源代码,但让我们想象一下:

svScope foo();

此函数将返回一个svScope,它只是一个void *。如果您希望将其导出,则必须使用__declspec(export)(或者,在您的情况下XXTERN

XXTERN svScope foo();

请参阅使用 __declspec(dllexport) 从 DLL 导出

编辑:编辑问题后。

在您的 DLL 中,您需要:

__declspec(dllexport) svScope foo();

在使用您需要的 DLL 的应用程序中:

__declspec(dllimport) svScope foo();
于 2021-03-11T11:28:50.760 回答