2014-01-18 52 views
0

夥計!我以某種方式發現了一個更容易理解C語言中關於DLL的教程,但現在我似乎無法擺脫這個問題。DLL的問題

我創建了這個使用DLL的簡單程序。我打算在DLL中調用一個名爲「HelloWorld()」的函數,看它是否會顯示出我所希望的。

「dllmain.c」

/* Replace "dll.h" with the name of your header */ 
#include "dll.h" 
#include <windows.h> 

DLLIMPORT void HelloWorld() 
{ 
     printf("HAI!"); 
} 

BOOL WINAPI DllMain(HINSTANCE hinstDLL,DWORD fdwReason,LPVOID lpvReserved) 
{ 
    switch(fdwReason) 
    { 
     case DLL_PROCESS_ATTACH: 
     { 
      break; 
     } 
     case DLL_PROCESS_DETACH: 
     { 
      break; 
     } 
     case DLL_THREAD_ATTACH: 
     { 
      break; 
     } 
     case DLL_THREAD_DETACH: 
     { 
      break; 
     } 
    } 

    /* Return TRUE on success, FALSE on failure */ 
    return TRUE; 
} 

「dll.h」

#ifndef _DLL_H_ 
#define _DLL_H_ 

#if BUILDING_DLL 
#define DLLIMPORT __declspec(dllexport) 
#else 
#define DLLIMPORT __declspec(dllimport) 
#endif 

DLLIMPORT void HelloWorld(); 

#endif 

「的main.c」(可執行)

#include <stdio.h> 
#include <stdlib.h> 
#include "dll.h" 
#include <windows.h> 

main() 
{ 
    HelloWorld(); 
     getch(); 
} 

這就是問題:

錯誤消息 {C:\文檔\ DLLTest \ main.o

main.c:(.text+0x10): undefined reference to `__imp_HelloWorld'} 

{C:\...\Documents\DLLTest\collect2.exe [Error] ld returned 1 exit status 

Can someone tell me why this appears?! Thanks in advance!} 

哦,順便說一句,這是與DEVC++ TDM-GCC 4.7.1的64位版本

回答

0

您需要編譯main.c-L. -ldllmain.表示當前目錄wh該DLL位於此處)。例如,我有在當前目錄中的這些文件:

> dir 
... 
01/18/2014 05:53 PM <DIR>   . 
01/18/2014 05:53 PM <DIR>   .. 
01/18/2014 05:48 PM    191 dll.h 
01/18/2014 05:49 PM    615 dllmain.c 
01/18/2014 05:50 PM    150 main.c 
... 
> 

編譯DLL,然後編譯main.cdllmain.dll鏈接它:

> gcc -Wall dllmain.c -shared -o dllmain.dll 
> gcc -Wall main.c -L. -ldllmain 

現在你是好去:

> dir 
... 
01/18/2014 05:53 PM   29,141 a.exe 
01/18/2014 05:48 PM    191 dll.h 
01/18/2014 05:49 PM    615 dllmain.c 
01/18/2014 05:53 PM   26,466 dllmain.dll 
01/18/2014 05:50 PM    150 main.c 
... 
> a.exe 
HAI! 
> 
+0

我可以問一下這可以做到嗎?我的意思是,我在哪裏可以在DevC++中找到這種選項? – Wix

+0

@Wix我猜它使用gcc作爲編譯器?看看[本頁](http://www.tecgraf.puc-rio.br/iup/en/ide_guide/dev-cpp.html)如何將參數傳遞給「鏈接器」,傳遞「-ldllmain」在它之後有「庫目錄」,你在那裏傳遞dllmain.dll所在的目錄。 – 2014-01-18 15:49:41

+0

對不起,但我還是不明白。你使用MS命令提示符做這個,還是使用Dev-C++ GUI有更簡單的方法嗎? – Wix

0

你需要做添加編譯dll庫(* .LIB或類似的東西)到項目

單擊項目 - >「項目選項」 - >「參數」,並在部分鏈接添加庫