2

私は XP SP3 で codeblocks 10.05 mingw を使用しています。基本的に、dll の作成とそれにリンクする mingw サイトに従って、dll と lib ファイルをビルドしています。私が抱えている問題は、ライブラリにリンクしようとしている 2 番目の部分です。コンソール アプリケーションを作成し、次のソースを使用しました。

#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 ファイルのビルドに使用する 2 つのファイルも含めました (プロジェクトではなく、このサイトを参考にします。プロジェクト全体としてエラーのあるソースしか持っていません。それ以外の場合、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 ファイルのコンパイルに次の変更を加えました。

まず、プロジェクト マネージャー ツリーでアクティブなプロジェクトのプロパティを右クリックして、ビルド ターゲットが [ビルド ターゲット] タブの下にある動的ライブラリであることを確認しました。create import library と check .def exports file も確認しました。次に、コードブロックビルドオプションに入り、コンパイラ設定->その他のオプションタブに追加しました

-c -共有

#defines タブの下に追加しました

BUILDING_EXAMPLE_DLL BUILD_DLL

私のリンカー設定には、リンクライブラリに user32 があり、リンカー設定の下には -mwindows --out-implib がありました

ビルドは、エラーや警告なしでコンパイルおよびリンクされるはずです。

int main() を使用してソースをコンパイルするために、ヘッダー ファイルと次の設定を含めました。

リンクライブラリの下に libexample_dll.dll.a があり、リンカー設定の下にありました

-L. -lexample_dll

4

1 に答える 1

3

私には、exeとdllのディレクトリが異なるようです。

だからL.OKではありません。する必要がありますLpointsToThelibexample_dll.aFolder

exe ディレクトリに example_dll.h をコピーするか、-IpointsToTheexample_dll.hFolder.

確かに、コマンドが正しくないため、実行は機能しません。
メイクファイルを作りました。

メイクファイルをテストするには

  • 新しいディレクトリを作成します。
  • そこに 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

コピペミスのため。
このメイクファイルを使用する前に、コマンド (g++ および rm) の前のすべての空白を 2 つのタブに置き換えます。
または、このようなエラーが発生します。makefile:9: *** missing separator. Stop.

次のコマンドを実行します。

  • builddllを作る
  • buildexeを作る

これで example_exe.exe をテストできます

完成度が高いからこそメイククリーンもあります。

  • きれいにする

アップデート

ヘッダーファイルなしで 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 に答える