[转]How do I use cygwin1.dll with Visual Studio or MinGW?

Before you begin, note that Cygwin is licensed under the GNU GPL (as indeed are all other Cygwin-based libraries). That means that if your code links against the cygwin dll (and if your program is calling functions from Cygwin, it must, as a matter of fact, be linked against it), you must apply the GPL to your source as well. Of course, this only matters if you plan to distribute your program in binary form. For more information, see http://gnu.org/licenses/gpl-faq.html. If that is not a problem, read on.


If you want to load the DLL dynamically, read winsup/cygwin/how-cygtls-works.txt and the sample code in winsup/testsuite/cygload to understand how this works. The short version is:Make sure you have 4K of scratch space at the bottom of your stack.Invoke cygwin_dll_init():

 

HMODULE h = LoadLibrary("cygwin1.dll");
void (*init)() = GetProcAddress(h, "cygwin_dll_init");
init();


If you want to link statically from Visual Studio, to my knowledge none of the Cygwin developers have done this, but we have this report from the mailing list that it can be done this way:

Use the impdef program to generate a .def file for the cygwin1.dll (if you build the cygwin dll from source, you will already have a def file)

 

impdef cygwin1.dll > cygwin1.def

Use the MS VS linker (lib) to generate an import library

lib /def=cygwin1.def /out=cygwin1.lib

Create a file "my_crt0.c" with the following contents

 

#include <sys/cygwin.h>
#include <stdlib.h>

typedef int (*MainFunc) (int argc, char *argv[], char **env);

void
      my_crt0 (MainFunc f)
      {
        cygwin_crt0(f);
      }

Use gcc in a Cygwin prompt to build my_crt0.c into a DLL (e.g. my_crt0.dll). Follow steps 1 and 2 to generate .def and .lib files for the DLL.

 

Download crt0.c from the cygwin website and include it in your sources. Modify it to call my_crt0() instead of cygwin_crt0().

 

Build your object files using the MS VC compiler cl.

Link your object files, cygwin1.lib, and my_crt0.lib (or whatever you called it) into the executable.

Note that if you are using any other Cygwin based libraries that you will probably need to build them as DLLs using gcc and then generate import libraries for the MS VC linker.Thanks to Alastair Growcott (alastair dot growcott at bakbone dot co dot uk) for this tip.

 

17.How do I link against a .lib file?

If your .lib file is a normal static or import library with C-callable entry points, you can list foo.lib as an object file for

gcc/g++, just like any *.o file. Otherwise, here are some steps:

 

Build a C file with a function table. Put all functions you intend to use in that table. This forces the linker to include all the

object files from the .lib. Maybe there is an option to force LINK.EXE to include an object file.

 

Build a dummy 'LibMain'.

Build a .def with all the exports you need.

Link with your .lib using link.exe.

or

Extract all the object files from the .lib using LIB.EXE.

Build a dummy C file referencing all the functions you need, either with a direct call or through an initialized function pointer.

 

Build a dummy LibMain.

Link all the objects with this file+LibMain.

Write a .def.

Link.

 

You can use these methods to use MSVC (and many other runtime libs) with Cygwin development tools.

Note that this is a lot of work (half a day or so), but much less than rewriting the runtime library in question from specs...

Thanks to Jacob Navia (root at jacob dot remcomp dot fr) for this explanation.

 

你可能感兴趣的:([转]How do I use cygwin1.dll with Visual Studio or MinGW?)