cygwin跨平台移植开发系列3--GCC+VC联合使用
来源:互联网 发布:法兰克机器人编程 编辑:程序博客网 时间:2024/06/05 08:28
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 inwinsup/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().
- /* 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
- cygwin跨平台移植开发系列3--GCC+VC联合使用
- cygwin跨平台移植开发系列3--GCC+VC联合使用
- cygwin跨平台移植 gcc+vc联合使用的方法和注意事项
- cygwin跨平台移植 gcc+vc联合使用的方法和注意事项
- cygwin跨平台移植 gcc+vc联合使用的方法和注意事项
- cygwin跨平台移植 gcc+vc联合使用的方法和注意事项
- cygwin跨平台移植开发系列1--在cygwin下使用VC编译器
- cygwin跨平台移植开发系列2--so2lib实用工具(把cygwin生成的so或dll转换成vc可调用的lib)
- 跨平台开发Cygwin&MinGW
- Eclipse+Cygwin+gcc构建Windows下类Unix开发平台
- Cocos2dx之使用cygwin移植win32项目到Android平台
- Cygwin,gcc的使用
- cocos2d-x项目移植到android平台 手把手教程 Windows7+Eclipse+cocos2d-x平台搭建(无需Cygwin和VC)
- win平台下的Cygwin安装GCC
- cygwin中如何使用gcc
- 使用Cygwin在windows搭建linux开发平台
- Cygwin和VC++一起使用
- SAP企业移动平台开发探索系列7 – 使用SUP开发跨平台HWC应用
- XHR finished loading […] log message
- undefined symbols for architecture x86_64
- Delphi中如何调用Excel VBA
- JVM内存回收策略介绍
- Socket Programming
- cygwin跨平台移植开发系列3--GCC+VC联合使用
- 程序员常去的14个顶级开发社区
- 严蔚敏 数据结构习题6.47
- 【解决方案】Cmder v1.2 解决 “profile.ps1”和"Join-Path"方法
- CSS入门(3)-background:transparent的作用
- fastJson解析
- poj 1061/2142 扩展的欧几里得(青蛙的约会/砝码称重)
- Codeforces Round #333 (Div. 2)C. The Two Routes(最短路)
- 黑马程序员 JavaSE-09 Map