API.cpp:
int (*theFunc) (int);
theFunc = (int (*) (int)) DlSym(hSo, "theFunc");
到目前为止一切顺利。
现在,我想做一个header,让其他cpp文件也能调用theFunc。
Api.h: 根据 How to declare function pointer in header and c-file?
extern int (*theFunc)(int);
/usr/bin/ld: Warning: type of symbol `theFunc' changed from 2 to 1 in Api.o
好的,所以这是一个警告,即 theFunc 在一个编译元素中被视为函数,而在另一个编译元素中被视为变量。 (Reference)这似乎在运行时会发生不好的事情。
这似乎是一个正确的声明,我做错了什么?
编辑:实际上是在 linux 中运行,所以使用 DlSym 而不是 Microsoft GetProcAddress() 调用
最佳答案
根据链接的帖子,将其压缩为一行,而不是 .cpp 文件中的两行。所以 .h 似乎是正确的,并将其放入 .cpp 文件中:
int (*theFunc) (int) = (int (*) (int)) GetProcAddress(hDll, "theFunc");
您不会像链接的答案那样一次完成声明和赋值。有了我在上面发布的内容,您将成为。
关于c++ - 使用指向函数指针的外部引用时的链接器警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10217866/