私は 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