2013-02-19 41 views
2

我在XP SP3上的代碼塊10.05 mingw上,我基本上有以下the mingw site on creating a dll and linking to it內建的dll和lib文件。我遇到的問題是我試圖鏈接到圖書館的第二部分。我創建了一個控制檯應用程序和使用以下來源:建立DLL並鏈接到它?

#include <stdio.h> 
#include "example_dll.h" 

int main(void) 
{ 
    hello("World"); 
    printf("%d\n", Double(333)); 
    CppFunc(); 

    MyClass a; 
    a.func(); 

    return 0; 
} 

然後設置我的連接器設置就像網站說,通過增加-L. -lexample_dll,我也與庫文件libexample_dll.a鏈接就像我會與其他許多使用相同設置成功運行的庫。然後,當我嘗試的可執行我得到一個錯誤,

C:\example_dll_client\example_dll_client.cpp|2|error: example_dll.h: No such file or directory| 
C:\example_dll_client\example_dll_client.cpp||In function 'int main()':| 
C:\example_dll_client\example_dll_client.cpp|6|error: 'hello' was not declared in this scope| 
C:\example_dll_client\example_dll_client.cpp|7|error: 'Double' was not declared in this scope| 
C:\example_dll_client\example_dll_client.cpp|8|error: 'CppFunc' was not declared in this scope| 
C:\example_dll_client\example_dll_client.cpp|10|error: 'MyClass' was not declared in this scope| 
C:\example_dll_client\example_dll_client.cpp|10|error: expected ';' before 'a'| 
C:\example_dll_client\example_dll_client.cpp|11|error: 'a' was not declared in this scope| 
||=== Build finished: 7 errors, 0 warnings ===| 

我還包括用於建築的dll和lib文件(而不是項目,而是這個網站的參考鏈接兩個文件,我只有來源與整個項目的錯誤,否則將點的dll是什麼?)。

//example_dll.cpp 
#include <stdio.h> 
#include "example_dll.h" 

__stdcall void hello(const char *s) 
{ 
     printf("Hello %s\n", s); 
} 
int Double(int x) 
{ 
     return 2 * x; 
} 
void CppFunc(void) 
{ 
     puts("CppFunc"); 
} 
void MyClass::func(void) 
{ 
     puts("MyClass.func()"); 
} 
//example_dll.h 
#ifndef EXAMPLE_DLL_H 
#define EXAMPLE_DLL_H 

#ifdef __cplusplus 
extern "C" { 
#endif 

#ifdef BUILDING_EXAMPLE_DLL 
#define EXAMPLE_DLL __declspec(dllexport) 
#else 
#define EXAMPLE_DLL __declspec(dllimport) 
#endif 

void __stdcall EXAMPLE_DLL hello(const char *s); 

int EXAMPLE_DLL Double(int x); 

#ifdef __cplusplus 
} 
#endif 

// NOTE: this function is not declared extern "C" 
void EXAMPLE_DLL CppFunc(void); 

// NOTE: this class must not be declared extern "C" 
class EXAMPLE_DLL MyClass 
{ 
public: 
     MyClass() {}; 
     virtual ~MyClass() {}; 
     void func(void); 
}; 

#endif // EXAMPLE_DLL_H 

編輯:

我做了如下修改DLL和lib文件的彙編。

我首先確定我的構建目標是一個動態庫,可以在構建目標選項卡下通過右鍵單擊項目管理器樹中活動項目的屬性來找到它。我還檢查了創建導入庫和檢查.def導出文件。然後我走進代碼塊構建選項和編譯器設置 - >其它選項標籤我加

-c下 -shared

和我說

BUILDING_EXAMPLE_DLL BUILD_DLL

#定義選項卡下

我的連接器設置在鏈接庫,並根據連接器設置我 有USER32 -mwindows --out-IMPLIB

構建應該現在編譯並鏈接,沒有錯誤或警告。

爲了與INT主()編譯源我包括在該頭文件以及這些設置:

根據鏈接庫我不得不libexample_dll.dll.a和下接頭設置我不得不

-L。 -lexample_dll

+0

錯誤的第一行是一個很好的提示。你是否指定了'example_dll.h'所在的include目錄? (使用'-I'編譯器開關)。 – greatwolf 2013-02-19 13:00:36

+0

正確,但不是它應該可以編譯我的int main()代碼只使用lib文件嗎?或者我如何使用.DLL文件編譯源代碼? – pandoragami 2013-02-19 14:00:24

+0

沒關係,頭文件只包含函數調用等。沒有別的。 cpp文件包含實際操作。 – pandoragami 2013-02-19 14:07:50

回答

3

對我來說,它看起來像你有不同的目錄爲exe和dll。

因此L.不正確。應該是LpointsToThelibexample_dll.aFolder

要麼在您的exe目錄中複製example_dll.h,要麼使用-IpointsToTheexample_dll.hFolder。由於命令不正確,執行不起作用。
我做了一個makefile。

要測試的makefile

  • 創建一個新的目錄。
  • 將example_dll.h,example_dll.cpp,example_dll.cpp文件複製到那裏。
  • 安全的以下代碼在文件makefile中(在同一目錄中)。

的makefile

# Environment 
MKDIR=mkdir 
CP=cp 
CCADMIN=CCadmin 


# build 
builddll: 
    g++ -c -DBUILDING_EXAMPLE_DLL example_dll.cpp 
    g++ -shared -o example_dll.dll example_dll.o -Wl,--out-implib,libexample_dll.a 

buildexe: 
    g++ -c example_exe.cpp 
    g++ -o example_exe.exe example_exe.o -L. -lexample_dll 

clean: 
    rm -f libexample_dll.a example_exe.exe example_dll.dll 

由於複製和粘貼錯誤的。
在您可以使用此生成文件之前,用兩個選項卡替換命令(g ++和rm)前的所有空白。
或者你會得到這樣的錯誤。 makefile:9: *** missing separator. Stop.

執行以下命令。

  • 化妝builddll
  • 化妝buildexe

現在可以測試example_exe.exe

只有完整的,因爲也有一個乾淨的化妝。

  • 使清潔

UPDATE

如果你想使用的DLL沒有headerfile,你必須知道所有的DLL功能。

這裏是一個加載DLL並調用Double的loadDll.cpp。

/* 
* File: loadDll.cpp 
* Author: Administrator 
* 
* Created on 19. Februar 2013, 22:42 
*/ 

#include <cstdlib> 
#include <windows.h> 
#include <stdio.h> 

using namespace std; 

typedef int (WINAPI *PFNDOUBLE)(int x); 
PFNDOUBLE idlldouble; 
HINSTANCE dllctrl_inst; 

int main(int argc, char** argv) { 
    puts("---start---"); 
    dllctrl_inst = LoadLibrary("example_dll.dll"); 
    if(dllctrl_inst != NULL) { 
     idlldouble = (PFNDOUBLE)GetProcAddress(dllctrl_inst, "Double"); 
    } 
    if(idlldouble != NULL) {printf("%d\n", idlldouble(333));} 
    return 0; 
} 
+0

但是沒有一些方法可以用int main()和dll文件編譯源代碼。頁面提到它,或者我還需要example_dll.h文件嗎?微軟的API通常是封閉的源代碼,除了windows.h之外,沒有頭文件,但是這些文件鏈接的權利? – pandoragami 2013-02-19 14:02:58

+0

完整的臉掌!在我的帖子下面查看我的評論。昨天晚上我發佈了這個消息,並且沒有意識到cpp文件沒有包含在int main()源文件中,而.h文件只包含函數調用等,謝謝你的幫助。 – pandoragami 2013-02-19 14:09:40