我在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
錯誤的第一行是一個很好的提示。你是否指定了'example_dll.h'所在的include目錄? (使用'-I'編譯器開關)。 – greatwolf 2013-02-19 13:00:36
正確,但不是它應該可以編譯我的int main()代碼只使用lib文件嗎?或者我如何使用.DLL文件編譯源代碼? – pandoragami 2013-02-19 14:00:24
沒關係,頭文件只包含函數調用等。沒有別的。 cpp文件包含實際操作。 – pandoragami 2013-02-19 14:07:50