What am I trying to do? ...
First, create static library with MinGW's g++ compiler.
So, simple example files are ...
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; } When I use compilers commands:
g++ -c -DEXPORT_DLL_FUNCT test.cpp -o test.o and
g++ -shared test.o -o libtest.dll -Wl,--out-implib=libtest.a files "libtest.dll" and "libtest.a" are created. Why need both? Because, if you intend to use library in VS2008 project (MSVC++), both files are necessary - I read that on MinGW's site.
Next ... I created VS2008 Win32 Console Application project which is going to call function "whatever" from library.
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; } In VS2008: "Properties-->Linker-->General-->Additional Library Directories" I added path to previously created library and in "Properties-->Linker-->Input-->Additional Dependencies" I added "libtest.a" file. When I build the project, comile and linking is OK, exe file is generated, but when i try to run exe ... segmentatin fault happens (yes, "libtest.dll" is in same folder as .exe file) !!! I have no idea why? "__stdcall" is used in code, so there should be no problems with pushing things on stack ...
Any suggestions, please?