我想做什么?...
首先,使用 MinGW 的 g++ 编译器创建静态库。
所以,简单的示例文件是......
测试.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
测试.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 控制台应用程序项目,该项目将从库中调用函数“whatever”。
主文件
#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 中:“Properties-->Linker-->General-->Additional Library Directories”我添加了以前创建的库的路径,并在“Properties-->Linker-->Input-->Additional Dependencies”中添加了“libtest.一份文件。当我构建项目时,编译和链接正常,生成 exe 文件,但是当我尝试运行 exe 时...发生分段错误(是的,“libtest.dll”与 .exe 文件位于同一文件夹中)!!!我不知道为什么?代码中使用了“__stdcall”,因此将东西推入堆栈应该没有问题......
请问有什么建议吗?