我有一个 218KB 的 .dll
和一个 596KB 的 .so
文件,两者的名称相同。我想链接到 .dll
以避免链接器返回“未解析的外部符号”错误,但我找不到链接到 DLL 文件的方法。
根据 this Pelles C forum topic , 我需要使用 .def
文件来创建一个 .lib
... 但我没有 .def
文件。 This forum topic显示了如何使用 polink
从命令行创建 .lib
,因此我运行了 polink/?
以获得更多选项。我注意到一个 /MAKEDEF
选项,但是同时使用 .dll
和 .so
运行它会出现“未指定库文件” fatal error .
我已经尝试这样做了三个小时,但毫无想法。我已经到了我的网络搜索出现我自己的帮助请求的地步。必须有办法做到这一点...我如何链接到 .dll
?
最佳答案
根据 header #include 中的信息和您的详细信息,这里有一种方法可以通过从您的软件动态调用它们来替换缺失的函数。 1- 以下原型(prototype)在 #include 中:
typedef float (* XPLMFlightLoop_f)(float inElapsedSinceLastCall, float inElapsedTimeSinceLastFlightLoop, int inCounter, void * inRefcon);
2- 您可以根据需要填写的一些常量:
const char *sDllPathName = "<Your XPLM_API DLL>.dll";
const char *sXPLMRegisterFlightLoopCallbackName = "XPLMRegisterFlightLoopCallback";
In order to confirm the
sXPLMRegisterFlightLoopCallbackName
, you can use the freeware Dependency Walker and check name and format of the exported functions.
3-声明外部函数的原型(prototype):
Be aware to the calling convention
__cdecl
or__stdcall
In the current case, the keyword
XPLM_API
is defined in theXPLMDefs.h
as follow:
#define XPLM_API __declspec(dllexport) // meaning __cdecl calling convention
typedef void (__cdecl *XPLMRegisterFlightLoopCallback_PROC)(XPLMFlightLoop_f, float, void *);
4- 克隆函数以在您的软件中调用它:
#include <windows.h>
void XPLMRegisterFlightLoopCallback(XPLMFlightLoop_f inFlightLoop, float inInterval, void * inRefcon)
{
HINSTANCE hInstDLL;
XPLMRegisterFlightLoopCallback_PROC pMyDynamicProc = NULL;
// Load your DLL in memory
hInstDLL = LoadLibrary(sDllPathName);
if (hInstDLL!=NULL)
{
// Search for the XPLM Function
pMyDynamicProc = (XPLMRegisterFlightLoopCallback_PROC) GetProcAddress(hInstDLL, sXPLMRegisterFlightLoopCallbackName);
if (pMyDynamicProc != NULL)
{
// Call the XPLM Function with the orignal parameter
(pMyDynamicProc)(inFlightLoop,inInterval,inRefcon);
return;
}
}
// Do something when DLL is missing or function not found
}
5- 只需添加您描述的调用:
...
XPLMRegisterFlightLoopCallback(callbackfunction, 0, NULL);
...
关于c - 链接到 Pelles C 中的 DLL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40282524/