私は何をしようとしていますか?...
まず、MinGW の g++ コンパイラでスタティック ライブラリを作成します。
したがって、簡単なサンプルファイルは...
test.h
#ifndef EXAMPLE_H
#define EXAMPLE_H
#include <iostream>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef EXPORT_DLL_FUNCT
#define DLL_API __declspec(dllexport)
#else
#define DLL_API __declspec(dllimport)
#endif
DLL_API void __stdcall whatever( int a, int b );
#ifdef __cplusplus
}
#endif
#endif // EXAMPLE_H
test.cpp
#include "test.h"
__stdcall void whatever( int a, int b ) {
std::cout << "whatever printout !!!" << std::endl;
int c = a + b;
}
コンパイラ コマンドを使用する場合:
g++ -c -DEXPORT_DLL_FUNCT test.cpp -o test.o
と
g++ -shared test.o -o libtest.dll -Wl,--out-implib=libtest.a
ファイル「libtest.dll」および「libtest.a」が作成されます。なぜ両方が必要なのですか?VS2008 プロジェクト (MSVC++) でライブラリを使用する場合は、両方のファイルが必要なので、MinGW のサイトでそれを読みました。
次に...ライブラリから関数「何でも」を呼び出すVS2008 Win32コンソールアプリケーションプロジェクトを作成しました。
main.cpp
#include "../mingw/test.h"
#include <iostream>
void main(void)
{
std::cout << "\n*** start ***" << std::endl;
whatever(3, 2);
std::cout << "\n*** end ***" << std::endl;
}
VS2008: 「プロパティ-->リンカー-->一般->追加のライブラリ ディレクトリ」以前に作成したライブラリへのパスを追加し、「プロパティ-->リンカー-->入力-->追加の依存関係」に「libtest.ファイル。プロジェクトをビルドしてcomileしてリンクするとOK、exeファイルが生成されるのですが、exeを実行しようとすると...セグメンテーション違反が発生します(はい、「libtest.dll」は.exeファイルと同じフォルダにあります)!!! 理由がわかりませんか?「__stdcall」はコードで使用されているため、スタックにプッシュしても問題ないはずです...
何か提案はありますか?