Cygwin和VC++一起使用

VC可直接调用cygwin编译生成的*.a库,但是当调用与ipc相关的函数时(如shmgetshmat等时会存在问题,此问题上网搜索后,发现有人也遇到过类似问题,也提出了解决方法,但是我尝试后不可行,以下把解决方法贴出来,供以后参考。

http://www.cygwin.com/ml/cygwin/2004-01/msg00972.html

http://blog.csdn.net/songbohr/archive/2010/02/01/5276128.aspx

cygwin跨平台移植开发系列3--GCC+VC联合使用 收藏

前几天做一个模拟器时要把sdk代码在从linux移植到windows,我当初选用方案使用cygwin,然而却碰到一个crash的问题而搁浅。于是上周发了一个emailcygwin maillist,没想到得到cygwin官方的回复,原来faq里早就有了。另外我补充了几点注意的地方,faq还是说的不严谨。

Re: gcc and vc compiled library problem

On 01/28/2010 08:14 PM, phil song wrote:

> Hi,cygwin

>      pls forgive my poor english at first.>

> I have compiled one sdk_lib dll using gcc in cygwin,and I writed a tool

> convert the dll to .lib so I can call the interface function in sdk_lib

> dll using vc. Then I wrote a application using VC2005,when  it ran at

> beginning,everything looks like ok,but when I call a function

> "tti_readfile" which call "open write close" in sdk_lib many times,The

> application exited.By debuging,I find the  it crashed  in function

> "_aclcheck" in "cygwin1.dll".  But I found the file had been write

> success,however,after the app called it,it crashed.>

> who can tell me what cause this?How to resolve it?Can the dll(compiled by

> gcc) and app(compiled by vc) run in the same process?

<http://cygwin.com/faq/faq-nochunks.html#faq.programming.msvcrt-and-cygwin>

<http://cygwin.com/faq/faq-nochunks.html#faq.programming.msvs-mingw>

--

Larry Hall                              http://www.rfk.com

RFK Partners, Inc.                      (508) 893-9779 - RFK Office

216 Dalton Rd.                          (508) 893-9889 - FAX

Holliston, MA 01746

_____________________________________________________________________

A: Yes.

 > Q: Are you sure?

 >> A: Because it reverses the logical flow of conversation.

 >>> Q: Why is top posting annoying in email?--

Problem reports:       http://cygwin.com/problems.html

FAQ:                   http://cygwin.com/faq/

Documentation:         http://cygwin.com/docs.html

Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple

下面是我的详细步骤,测试成功,GCC+VC可以完全兼容了

  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 include it in your sources. Modify it to call my_crt0() instead of cygwin_crt0().

view plaincopy to clipboardprint?

/* crt0.c.  

   Copyright 2001, 2005 Red Hat, Inc.  

This software is a copyrighted work licensed under the terms of the 

Cygwin license.  Please consult the file "CYGWIN_LICENSE" for 

details. */   

/* In the following ifdef'd i386 code, the FPU precision is set to 80 bits 

   and all FPU exceptions are masked.  The former is needed to make long 

   doubles work correctly.  The latter causes the FPU to generate NaNs and 

   Infinities instead of signals for certain operations. 

*/   

#ifdef __i386__  

#define FPU_RESERVED 0xF0C0  

#define FPU_DEFAULT  0x033f    

/* For debugging on *#!$@ windbg.  bp for breakpoint.  */ 

int __cygwin_crt0_bp = 0;  

#endif    

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

void  my_crt0 (MainFunc f);  

//extern int main (int argc, char **argv);  

extern int main (int argc, char **argv, char **env);  

//void cygwin_crt0 (int (*main) (int, char **));    

void 

mainCRTStartup ()  

{  

#ifdef __i386__  

  (void)__builtin_return_address(1);  

  asm volatile ("andl $-16,%%esp" ::: "%esp");  

  if (__cygwin_crt0_bp)  

    asm volatile ("int3");    

  {  

    volatile unsigned short cw;    

    /* Get Control Word */ 

    __asm__ volatile ("fnstcw %0" : "=m" (cw) : );    

    /* mask in */ 

    cw &= FPU_RESERVED;  

    cw |= FPU_DEFAULT;    

    /* set cw */ 

    __asm__ volatile ("fldcw %0" :: "m" (cw));  

  }  

#endif    

  my_crt0 (main);  

}    

//void WinMainCRTStartup(void) __attribute__ ((alias("mainCRTStartup"))); 

/* crt0.c.

   Copyright 2001, 2005 Red Hat, Inc.

This software is a copyrighted work licensed under the terms of the

Cygwin license.  Please consult the file "CYGWIN_LICENSE" for

details. */

 

/* In the following ifdef'd i386 code, the FPU precision is set to 80 bits

   and all FPU exceptions are masked.  The former is needed to make long

   doubles work correctly.  The latter causes the FPU to generate NaNs and

   Infinities instead of signals for certain operations.

*/

 

#ifdef __i386__

#define FPU_RESERVED 0xF0C0

#define FPU_DEFAULT  0x033f

 

/* For debugging on *#!$@ windbg.  bp for breakpoint.  */

int __cygwin_crt0_bp = 0;

#endif

 

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

 

void  my_crt0 (MainFunc f);

//extern int main (int argc, char **argv);

extern int main (int argc, char **argv, char **env);

//void cygwin_crt0 (int (*main) (int, char **));

 

void

mainCRTStartup ()

{

#ifdef __i386__

  (void)__builtin_return_address(1);

  asm volatile ("andl $-16,%%esp" ::: "%esp");

  if (__cygwin_crt0_bp)

    asm volatile ("int3");

 

  {

    volatile unsigned short cw;

 

    /* Get Control Word */

    __asm__ volatile ("fnstcw %0" : "=m" (cw) : );

 

    /* mask in */

    cw &= FPU_RESERVED;

    cw |= FPU_DEFAULT;

 

    /* set cw */

    __asm__ volatile ("fldcw %0" :: "m" (cw));

  }

#endif

  my_crt0 (main);

}

//void WinMainCRTStartup(void) __attribute__ ((alias("mainCRTStartup")));

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.

另外

1.int main(int argc,char *argv[])改为int main(int argc,char *argv[], char **env)

2.设置VC的入口点为mainCRTStartup

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值