2

我在 XP SP3 上的代码块 10.05 mingw 上,我基本上在 mingw 站点创建 dll 并链接到它之后构建了 dll 和 lib 文件。我遇到的问题是我试图链接到图书馆的第二部分。我创建了一个控制台应用程序并使用了以下源:

#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 -共享

在我添加的#defines 选项卡下

BUILDING_EXAMPLE_DLL BUILD_DLL

我的链接器设置在链接库中有 user32,在链接器设置下我有 -mwindows --out-imlib

构建现在应该编译和链接,没有错误或警告。

为了使用 int main() 编译源代码,我包含了头文件以及这些设置:

在我有 libexample_dll.dll.a 的链接库和我有的链接器设置下

-L。-lexample_dll

4

1 回答 1

3

对我来说,看起来你有不同的 exe 和 dll 目录。

所以L.不行。应该是LpointsToThelibexample_dll.aFolder

将 example_dll.h 复制到您的 exe 目录中,或者使用-IpointsToTheexample_dll.hFolder.

可以肯定的是,由于命令不正确,执行不起作用。
我做了一个makefile。

测试生成文件

  • 创建一个新目录。
  • 复制 example_dll.h , example_dll.cpp , example_dll.cpp 文件在那里。
  • 文件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

因为复制和粘贴错误。
在您可以使用这个 makefile 之前,将命令(g++ 和 rm)前面的所有空白替换为两个制表符。
或者你会得到这样的错误。makefile:9: *** missing separator. Stop.

执行以下命令。

  • 制作 builddll
  • 制作 buildexe

现在您可以测试 example_exe.exe

仅仅因为完整性,还有一个make clean。

  • 打扫干净

更新

如果你想使用没有头文件的 DLL,你必须知道所有的 DLL 函数。

这是加载 DLL 并调用的 loadDll.cpp Double

/* 
 * 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;
}
于 2013-02-19T12:32:34.130 回答