我有 2 个 dll test_dll1.c 和 test_dll2.c。test_dll2 应该充当 test_dll1 的代理。我还有一个主程序 test.c,它与 test_dll2.lib 链接,但链接器给出了一个错误,说“未解析的符号”,我不确定为什么。
下面是附上的代码
test_dll1.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
__declspec(dllexport) void dummy1(void)
{
printf("Inside %s %s\n", __FILE__, __FUNCTION__);
return;
}
__declspec(dllexport) void dummy2(void)
{
printf("Inside %s %s\n", __FILE__, __FUNCTION__);
return;
}
cl.exe /LD test_dll1.c
test_dll2.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#pragma comment(linker, "/export:dummy3=test_dll1.dummy1")
#pragma comment(linker, "/export:dummy4=test_dll1.dummy2")
cl.exe /LD test_dll2.c test_forwards.c
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
__declspec(dllimport) void dummy3(void);
__declspec(dllimport) void dummy4(void);
int main()
{
dummy3();
dummy4();
}
$ cl.exe test_forwards.c test_dll2.lib
test_forwards.obj:错误 LNK2019:函数 _main test_forwards.obj 中引用的未解析的外部符号 __imp__dummy3:错误 LNK2019:函数 _main test_forwards.exe 中引用的未解析的外部符号 __imp__dummy4:致命错误 LNK1120:2 个未解析的外部
我错过了什么?为什么链接器无法找到符号?dummy3 和 dummy4 被定义为“test_dll2.dll”中的转发导出,因为我可以看到,如果我将导出转储到 test_dll2.dll 中,那么导出的符号 dummy3 和 dummy4 确实存在。
更新:顺便说一句,我将 test_dll2.c 更新为 #pragma comment(linker, "/export:_dummy3=test_dll1.dummy1") #pragma comment(linker, "/export:_dummy4=test_dll1.dummy2")
我将 dummy3 和 dummy4 更改为 _dummy3 和 _dummy4 并且它起作用了。为什么我需要将符号名称更改为 _dummy3 和 _dummy4?