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



http://blog.csdn.net/songbohr/article/details/5276128


前几天做一个模拟器时要把sdk代码在从linux移植到windows,我当初选用方案使用cygwin,然而却碰到一个crash的问题而搁浅。于是上周发了一个email到cygwin 的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:

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

  2. 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:

  1. 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
    
  2. Use the MS VS linker (lib) to generate an import library

    lib /def=cygwin1.def /out=cygwin1.lib
    
  3. 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);
      }
    
  4. 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.

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

    1. /* crt0.c. 
    2.  
    3.    Copyright 2001, 2005 Red Hat, Inc. 
    4.  
    5. This software is a copyrighted work licensed under the terms of the 
    6. Cygwin license.  Please consult the file "CYGWIN_LICENSE" for 
    7. details. */  
    8.   
    9. /* In the following ifdef'd i386 code, the FPU precision is set to 80 bits 
    10.    and all FPU exceptions are masked.  The former is needed to make long 
    11.    doubles work correctly.  The latter causes the FPU to generate NaNs and 
    12.    Infinities instead of signals for certain operations. 
    13. */  
    14.   
    15. #ifdef __i386__  
    16. #define FPU_RESERVED 0xF0C0  
    17. #define FPU_DEFAULT  0x033f  
    18.   
    19. /* For debugging on *#!$@ windbg.  bp for breakpoint.  */  
    20. int __cygwin_crt0_bp = 0;  
    21. #endif  
    22.   
    23. typedef int (*MainFunc) (int argc, char *argv[], char **env);  
    24.   
    25. void  my_crt0 (MainFunc f);  
    26. //extern int main (int argc, char **argv);  
    27. extern int main (int argc, char **argv, char **env);  
    28. //void cygwin_crt0 (int (*main) (int, char **));  
    29.   
    30. void  
    31. mainCRTStartup ()  
    32. {  
    33. #ifdef __i386__  
    34.   (void)__builtin_return_address(1);  
    35.   asm volatile ("andl $-16,%%esp" ::: "%esp");  
    36.   if (__cygwin_crt0_bp)  
    37.     asm volatile ("int3");  
    38.   
    39.   {  
    40.     volatile unsigned short cw;  
    41.   
    42.     /* Get Control Word */  
    43.     __asm__ volatile ("fnstcw %0" : "=m" (cw) : );  
    44.   
    45.     /* mask in */  
    46.     cw &= FPU_RESERVED;  
    47.     cw |= FPU_DEFAULT;  
    48.   
    49.     /* set cw */  
    50.     __asm__ volatile ("fldcw %0" :: "m" (cw));  
    51.   }  
    52. #endif  
    53.   
    54.   my_crt0 (main);  
    55. }  
    56.   
    57. //void WinMainCRTStartup(void) __attribute__ ((alias("mainCRTStartup")));  

  6. Build your object files using the MS VC compiler cl.

  7. 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值