将函数指针分配给 DLL 导入的函数时出现无法解析的外部符号
我无法找到有关此特定问题的帖子。这一切都是在 Windows 目标上用 C 语言编写的。
我有一个我创建的 DLL,以及一些我创建的用于测试该 DLL 的客户端代码。当我尝试在客户端代码中使用指向 DLL 中定义的函数的函数指针时,DLL 中定义的函数和类型都可以由客户端代码成功解析除外。
示例:
/*--- DLL_Header.h ---*/
#ifdef BUILD_DLL
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
DLL_API void library_function(int foo, int bar);
/* --- DLL_source.c --- */
void library_function(int foo, int bar)
{
/* Do anything */
}
/* --- Client Code --- */
#include "DLL_Header.h"
void client_function_A()
{
int foo = 1;
int bar = 2;
library_function(foo,bar); /* This compiles and links with no problem! */
}
void client_function_B()
{
void (*lib_exec)(int foo, int bar);
lib_exec = &library_function; /* Compiles but then linker says unresolved external symbol __imp__library_function */
int x = 1;
int y = 2;
lib_exec(x,y);
}
如果我只是静态链接所有内容,client_function_B
工作正常,因此似乎我对 DLL 和函数指针之间的交互并没有从根本上理解。有人可以帮助我理解我做错了什么,为什么?
编辑:确切的错误消息
client_code.obj:错误LNK2019:函数_client_function_B中引用了无法解析的外部符号__imp__library_function
I wasn't able to find a post with this particular issue. This is all in C, on a Windows target.
I have a DLL that I created, and some client code I created to test the DLL. The functions and types defined in the DLL are all successfully resolved by the client code except when I try to use a function pointer in the client code to a function defined in DLL.
Example:
/*--- DLL_Header.h ---*/
#ifdef BUILD_DLL
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
DLL_API void library_function(int foo, int bar);
/* --- DLL_source.c --- */
void library_function(int foo, int bar)
{
/* Do anything */
}
/* --- Client Code --- */
#include "DLL_Header.h"
void client_function_A()
{
int foo = 1;
int bar = 2;
library_function(foo,bar); /* This compiles and links with no problem! */
}
void client_function_B()
{
void (*lib_exec)(int foo, int bar);
lib_exec = &library_function; /* Compiles but then linker says unresolved external symbol __imp__library_function */
int x = 1;
int y = 2;
lib_exec(x,y);
}
client_function_B
works fine if I just statically link everything, so it seems there is something I am not fundamentally understanding about the interaction between the DLL and function pointers. Can someone please help me understand what I am doing wrong, and why?
Edit: Exact error message
client_code.obj : error LNK2019: unresolved external symbol __imp__library_function referenced in function _client_function_B
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论