2013-02-19 43 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