【发布时间】:2021-03-11 09:43:30
【问题描述】:
我尝试从 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): 未定义的对 `_imp__svGetScope' 的引用 collect2.exe:错误:ld 返回 1 个退出状态
【问题讨论】:
-
Typedefs 仅在源代码中有意义,一旦编译,它们甚至不存在于二进制文件中(我相信)
-
是的,但它忽略了 dll 属性,因此当我构建我的程序时,函数是未定义的。
-
函数的声明是否标有 dll 属性?因为它不会从 typedef 传递给函数,即使函数使用 typedef。
-
声明如下: __declspec(dllexport) typedef void* svScope;