With Mingw created static library link into VS2008 project?

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?


Thanks for your answer. Your suggestions helped me a lot! Let's see the solution now ... Fist of all, partially it was my desultoriness ... I simply "forgot" to check if in any case .exe works when bulid in VS2008 under Debug mode. The answer is YES. :) So, when in Debug mode, upper example works well! But still does not work in Release mode. Well, here is solution also for Release mode: in VS2008 project proterties set Linker-->Optimization->References to No(/OPT:NOREF) . That's all ...


I don't have a good explanation for this, but with VS 2010 if I added /Zi , /ZI , or /Z7 to the VS compilation line (all options for enabling debug symbols), the application runs correctly and invokes the function in the MinGW library. I initially thought this implied the optimizer in VS was causing problems, however specifying /Od did not prevent the segmentation fault.


Though the import library created by GCC fits the standard of "Archive" (.a, or .lib for vc), and the object files inside the archive did also fit the COFF standard, it doesn't compatible with VC's way.

I explored the import library created by vc, implib sdk and gcc, found that those created by gcc and implib sdk are just alike. The most impressive difference is that the member(object file inside the archive)'s names are the same for implib sdk's and they are different for gcc's. VC's linker doesn't refuse to link with gcc's import library but a malformed exe will be built.

You may take a look at implib sdk, it can make a import library that works with vc. But some limits are very annoying like dll's file name length, charset, etc.

Use vc's linker to create a import library is a good way. The created import library is ensured to compatible with your source code. For your situation, you may have a try:

  • write a .def file like this

    LIBRARY libtest
    EXPORTS
        whatever@8
    

    save it as libtest.def

  • make import library with linker:

    link /lib /out:libtest.lib /def:libtest.def
    
  • Now you have libtest.lib, use it instead.

  • 链接地址: http://www.djcxy.com/p/64370.html

    上一篇: 是否有可能将静态库链接到Visual Studio中的动态库?

    下一篇: 用Mingw在VS2008项目中创建了静态库链接?