![](/img/trans.png)
[英]compiling C++ code with intel compiler on Mac error: expected an identifier
[英]Error with compiling DLL with intel compiler
我正在嘗試從控制台編譯DLL,而不使用任何IDE並面臨下一個錯誤。
我寫了這段代碼:
test_dll.cpp
#include <windows.h>
#define DLL_EI __declspec(dllexport)
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved){
return 1;
}
extern "C" int DLL_EI func (int a, int b){
return a + b;
}
然后用命令icl /LD test_dll.cpp
。 我試圖從另一個程序調用此func
:
prog.cpp
int main(){
HMODULE hLib;
hLib = LoadLibrary("test_dll.dll");
double (*pFunction)(int a, int b);
(FARPROC &)pFunction = GetProcAddress(hLib, "Function");
printf("begin\n");
Rss = pFunction(1, 2);
}
用icl prog.cpp
編譯它。 然后我運行它,它失敗了標准窗口“程序不工作” 。 可能存在分段錯誤錯誤。
我究竟做錯了什么?
檢查LoadLibrary()
和GetProcAddress()
成功,在這種情況下它們肯定不會因為導出的函數被稱為func
,而不是GetProcAddress()
參數中指定的"Function"
,這意味着當嘗試時函數指針將為NULL
調用它。
函數指針的簽名也與導出函數的簽名不匹配,導出的函數返回一個int
,函數指針期望為double
。
例如:
typedef int (*func_t)(int, int);
HMODULE hLib = LoadLibrary("test_dll.dll");
if (hLib)
{
func_t pFunction = (func_t)GetProcAddress(hLib, "func");
if (pFunction)
{
Rss = pFunction(1, 2);
}
else
{
// Check GetLastError() to determine
// reason for failure.
}
FreeLibrary(hLib);
}
else
{
// Check GetLastError() to determine
// reason for failure.
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.