在Visual Studio 2010中使用使用Cygwin编译的DLL

在Visual Studio 2010中使用使用Cygwin编译的DLL

问题描述:

我已经阅读了大量的文章,教程和如何解决我的问题的说明,但仍然没有收获。我只是不能把它工作。

I've read already tremendous amount of articles, tutorials, and instructions about how to solve my problem - but still - no gain. I just can't put it into work.

我的目标是很简单:我想使用Cygwin gcc工具编译一个DLL文件,然后在MSVC2010中使用它。我实际上想用我自己的DLL代码,但为了简单 - 我已经尝试了Cygwin的网站上的非常基本的例子 - 也失败了。

My goal is pretty simple: I want to compile a DLL file using Cygwin gcc tool, and then use it within MSVC2010. I'm actually want to do it with my own DLL code, but as for simplicity - I've tried the very basic example on Cygwin's site - and failed with that too..

到目前为止我做了什么:
(大多数是从Cygwin用户指南, DLL部分

What I've done so far: (Most is taken from Cygwin User's Guide, DLL section)


  1. 创建 mydll.c 文件如下:

#include <stdio.h>

int hello()    {
  printf ("Hello World!\n");
}


  • 编译 mydll.c $使用 gcc -c mydll.c gcc - 将转换为 mydll.dll 共享-o mydll.dll mydll.o

  • Compiled mydll.c into mydll.dll using gcc -c mydll.c and gcc -shared -o mydll.dll mydll.o

    在Visual Studio中打开一个空的Win32控制台项目, code> test.c (代码取自 Oleg 的代码这里,并根据):

    Opened an empty Win32 Console project in Visual Studio, with the following code as test.c (code taken from Oleg's code in here, and based on this) :

    #include <windows.h>
    
    typedef int (*PFN_HELLO)();
    typedef void (*PFN_CYGWIN_DLL_INIT)();
    
    int main()
    {
        PFN_HELLO fnHello;
        HMODULE hLib, h = LoadLibrary(TEXT("cygwin1.dll")); 
        PFN_CYGWIN_DLL_INIT init = (PFN_CYGWIN_DLL_INIT)GetProcAddress(h,"cygwin_dll_init");
        init(); 
    
        hLib = LoadLibrary (TEXT("D:\\test\\mydll.dll"));
        fnHello = (PFN_HELLO) GetProcAddress (hLib, "hello");
        return fnHello();
    }
    


  • 在Windows系统上设置路径变量,包括Cygwin \ bin \目录。

  • Set the path variable on Windows system to include "Cygwin\bin\" directory.

    构建&

    我最后遇到以下异常: 0xc0000005:访问冲突读取位置0x003a0048

    I ended up with the following exception: 0xc0000005: Access violation reading location 0x003a0048.

    这是完整的MSVC2010调试输出:

    Here is the full MSVC2010 Debug Output:

    'CygwinDLLTest.exe': Loaded 'C:\Users\xxxx\Documents\Visual Studio 2010\Projects\CygwinDLLTest\Debug\CygwinDLLTest.exe', Symbol loaded.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\kernelBase.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'D:\Cygwin\bin\cygwin1.dll', Binary was not built with debug information.
    cYgFFFFFFFF 6119F510 0cYgstd 0x27a70b d 3'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\lpk.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\usp10.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', cannot find or open the PDB file.
    'CygwinDLLTest.exe': Loaded 'D:\test\mydll.dll', Binary was not built with debug information.
    First-chance exception at 0x611075a8 in CygwinDLLTest.exe: 0xc0000005: Access violation reading location 0x003a0048
    Unhandled exception at 0x611075a8 in CygwinDLLTest.exe: 0xc0000005: Access violation reading location 0x003a0048
    The program '[784] CygwinDLLTest.exe: Native' has exited with code 0 (0x0).
    

    现在,注意到问题是没有加载这些DLL的,因为他们的处理程序都得到了不同于NULL的地址。代码中引起异常的一行是调用hello func,在DLL文件中。

    Now, it's important to notice that the problem wasn't loading those DLL's, as their handlers all got an address different from NULL. The line in the code that cause the exception, was the call to the hello func, inside the DLL file.

    在你去之前说出 externC __ declspec(dllimport / dllexport) - 不会帮助。我尝试了所有的,它没有帮助。(虽然,AFAIK - 我使用显式链接(按照MS)或动态加载,因此 __ declspec(dllimport / dllexport)不是必需的)。

    And before you go and say anything about extern "C" or __declspec(dllimport/dllexport) - won't help. I've tried all of them and it didn't help. (although, AFAIK - I'm using Explicit Linking in terms of MS, or Dynamic Loading in terms of UNIX, so __declspec(dllimport/dllexport) is not necessary).

    我真的希望问题不在堆栈定义,因为

    I'm really hope the problem is not at the stack definition, as lying over here:


    确保您的堆栈底部有4K的临时空间

    "Make sure you have 4K of scratch space at the bottom of your stack"

    因为我不知道如何使这发生在MSVC2010(显然,Oleg ...:)

    Because I've no clue how to make this happen on MSVC2010 (and apparently, neither do Oleg...:)

    现在,我知道有一个直接参考我的问题在 Hans Passant 的话超过这里 - 仍然 - 我不明白如何解决我的问题。

    Now, I know there's a direct reference to my problem in Hans Passant's words over here - still - I couldn't understand how to solve my problem.

    更新:因为我想我知道是什么原因导致的问题,我只是不知道该如何解决它。并原谅我,阿卡迪,但我真的不认为它必须做任何事情你所提到的所有。我正在考虑真的很简单的 .dll文件,只有一个函数,所以没有什么可以'wrap'在那里。

    无论如何,我认为我的问题是使用msvcrt .dll文件,而不是此处

    UPDATE: I'm updating this, because I think I know what cause the problem, I just don't know how to solve it. And forgive me, Arkady, but I really don't think that it has to do anything with all what you mentioned. I was taking about really simple .dll file, with only one function, so there's nothing to 'wrap' there..
    Anyway, I think my problem is the use of msvcrt.dll file, as opposed to what stated here:


    问:我可以同时链接MSVCRT * .DLL和cygwin1.dll吗?

    A:不,你必须使用一个或另一个,它们是互斥的。

    Q: Can I link with both MSVCRT*.DLL and cygwin1.dll?
    A:No, you must use one or the other, they are mutually exclusive.

    我知道我链接两个人,我只是不知道如何说服Visual Studio只链接到cygwin1.dll单独。

    I know I'm linking against both of them, I just don't know how to convince Visual Studio to link only against cygwin1.dll alone..

    我会为此答复。

  • 要使用gcc创建DLL并在Visual Studio中使用它,您需要遵循以下步骤:

    To create DLL using gcc and use it in Visual Studio, you need to follow next steps:

    检查包装是否相同。我的意思是:

    1) check that your packing is same. I mean:

    #pragma pack(push, N) 
    #pragma pack(pop)
    

    对于要导出的所有结构,函数和数据类型。要避免不同的默认数据打包。

    For all structures, functions and data types you want to export. To avoid different default data packing.

    2)检查您不使用外部头文件(即您的导出头不包含任何从外部头获取文件)。我的意思是windows.h可能是和将是不同的VS2010和CygWin。通常差异不重要,但由于它存在,你可能有问题。
    与STL和其他版本相同,版本可能不会(并且不会!)兼容,因此您将遇到内存问题。

    2) check that you don't use external header files (i.e. your exporting header don't contains anything that is taken from external header files). I mean "windows.h" may be and will be different in VS2010 and CygWin. Usually difference is not important, but since it exist, you may have problems with it. Same with STL and others, versions may not (and will not!) be compatibility, so you will have problems with memory.

    3)检查导出只是简单的结构和全局函数。所以,你真的要导出类似C的界面。
    Theory告诉我们如果你还要导出抽象接口,例如:

    3) check that you export just simple structures and global functions. So, you really have to export C-like interface. Theory tells that if you will export also abstract interfaces, such as in example:

    #pragma pack(push, 4)
    struct A
    {
        virtual ~A() {}
        virtual int32_t DoA() = 0;
        virtual int32_t PrepareA(const char* settings) = 0;
    };
    
    void GlobalFunction(A** ret);
    #pragma pack(pop)
    

    或者避免虚拟析构函数,这将释放在DLL中分配的对象的内存。因为gcc和msvc-10.0将有不同的数据管理,因此所有分配gcc分配器必须由gcc释放器释放。

    Or avoid virtual destructor at all, and add global function that will release memory of objects who were allocated in DLL. Because gcc and msvc-10.0 will have different data management, so all allocated with gcc allocators have to be released by gcc deallocators.

    它必须正常工作,因为通过调用GlobalFunction在DLL中初始化,并且将具有析构函数,在DLL中实现。仍然,我会提供避免它,使用C样式只是简单的数据和全局函数。

    it have to work correctly, because will be initialized inside DLL by calling GlobalFunction, and will have destructor, realized inside DLL. Still, I would offer to avoid it, using C-like style with just simple data and global functions.

    4)你还必须确保int在您的gcc和int在你的VS2010有相同的大小。你设置相同的架构。为了避免这个问题,你应该使用int32_t,例如。

    4) You also have to be sure int at your gcc and int at your VS2010 have same size. And you set same architecture. To avoid that problem you should use int32_t, for example.

    5)你应该确保所有导出的dll函数不抛出异常。在所有。由同一内存问题原因。

    5) And you should be sure that all exported dll functions don't throws exceptions. At all. By same memory problem-reason.