前段时间做一个把linux代码移植到vc上的工程,选择cygwin环境,直接交叉编译通过。后来为了调试方便需要把源代码全部放到vc下编译,在网上搜索相关资料,发现在cygwin官方的FAQ中给出了具体的方法。下面我就里面的关键步骤做一个简单的解释:首先是官方的原文:
How do I use cygwin1.dll with Visual Studio or MinGW?
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 inc