<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>绿色记忆 &#187; Cygwin</title>
	<atom:link href="https://blog.gmem.cc/tag/cygwin/feed" rel="self" type="application/rss+xml" />
	<link>https://blog.gmem.cc</link>
	<description></description>
	<lastBuildDate>Fri, 10 Apr 2026 07:50:36 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.14</generator>
	<item>
		<title>Windows下基于Eclipse CDT的C/C++开发</title>
		<link>https://blog.gmem.cc/eclipse-cdt-based-dev</link>
		<comments>https://blog.gmem.cc/eclipse-cdt-based-dev#comments</comments>
		<pubDate>Thu, 20 Oct 2011 09:41:07 +0000</pubDate>
		<dc:creator><![CDATA[Alex]]></dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[CDT]]></category>
		<category><![CDATA[Cygwin]]></category>
		<category><![CDATA[Eclipse]]></category>
		<category><![CDATA[LLVM]]></category>
		<category><![CDATA[MinGW]]></category>
		<category><![CDATA[MSVC]]></category>

		<guid isPermaLink="false">http://blog.gmem.cc/?p=2029</guid>
		<description><![CDATA[<p>安装和配置 安装Eclipse CDT 到Eclipse CDT官网下载：http://www.eclipse.org/cdt/并安装 Eclipse的相关配置可以参考：Eclipse 4.3.2开发环境搭建 安装工具链 下面的工具链可以根据工作环境进行选择性的安装。 MinGW 安装MinGW，下载地址：http://sourceforge.net/projects/mingw/files/ 安装MSYS，到http://sourceforge.net/projects/mingw/files/MSYS/下载 mingw-get-setup.exe，然后双击安装，选择安装目录为%MINGW_HOME%，勾选MSYS base，进行安装 将%MINGW_HOME%\bin添加到PATH环境变量，以便Eclipse CDT识别此工具链 MinGW-w64 安装MinGW-w64，下载地址：http://mingw-w64.org/doku.php 复制%MINGW_W64%\x86_64-w64-mingw32-gcc.exe（对于32位版本则是i686-w64-mingw32-gcc.exe）为mingw32-gcc.exe，便于Eclipse识别工具链 安装MSYS，下载地址：SourceForge 将%MINGW_W64%\mingw32\bin目录加入到PATH环境变量，以便Eclipse CDT识别此工具链 注意，Eclipse不能区分MinGW和MinGW-w64。因此，如果机器上同时安装这两套工具链，只应该设置其中常用的到环境变量PATH中。当需要使用另外一套工具链时，设置Eclipse项目属性 <a class="read-more" href="https://blog.gmem.cc/eclipse-cdt-based-dev">[...]</a></p>
<p>The post <a rel="nofollow" href="https://blog.gmem.cc/eclipse-cdt-based-dev">Windows下基于Eclipse CDT的C/C++开发</a> appeared first on <a rel="nofollow" href="https://blog.gmem.cc">绿色记忆</a>.</p>
]]></description>
				<content:encoded><![CDATA[<div class="wri_content_clear_both"><div class="blog_h1"><span class="graybg">安装和配置</span></div>
<div class="blog_h2"><span class="graybg">安装Eclipse CDT</span></div>
<ol>
<li>到Eclipse CDT官网下载：<a href="http://www.eclipse.org/cdt/">http://www.eclipse.org/cdt/</a>并安装</li>
<li>Eclipse的相关配置可以参考：<a href="/eclipse-kepler-sr2-setup#workspace-config">Eclipse 4.3.2开发环境搭建</a></li>
</ol>
<div class="blog_h2"><span class="graybg">安装工具链</span></div>
<p>下面的工具链可以根据工作环境进行选择性的安装。</p>
<div class="blog_h3"><span class="graybg">MinGW</span></div>
<ol>
<li>安装MinGW，下载地址：<a href="http://sourceforge.net/projects/mingw/files/">http://sourceforge.net/projects/mingw/files/</a></li>
<li>安装MSYS，到<a href="http://sourceforge.net/projects/mingw/files/MSYS/">http://sourceforge.net/projects/mingw/files/MSYS/</a>下载 mingw-get-setup.exe，然后双击安装，选择安装目录为%MINGW_HOME%，勾选MSYS base，进行安装</li>
<li>将%MINGW_HOME%\bin添加到PATH环境变量，以便Eclipse CDT识别此工具链</li>
</ol>
<div class="blog_h3"><span class="graybg">MinGW-w64</span></div>
<ol>
<li>安装MinGW-w64，下载地址：<a href="http://mingw-w64.org/doku.php">http://mingw-w64.org/doku.php</a></li>
<li>复制%MINGW_W64%\x86_64-w64-mingw32-gcc.exe（对于32位版本则是i686-w64-mingw32-gcc.exe）为mingw32-gcc.exe，便于Eclipse识别工具链</li>
<li>安装MSYS，下载地址：<a href="http://sourceforge.net/projects/mingw-w64/files/External%20binary%20packages%20%28Win64%20hosted%29/MSYS%20%2832-bit%29/">SourceForge</a></li>
<li>将%MINGW_W64%\mingw32\bin目录加入到PATH环境变量，以便Eclipse CDT识别此工具链</li>
</ol>
<p>注意，Eclipse不能区分MinGW和MinGW-w64。因此，如果机器上同时安装这两套工具链，只应该设置其中常用的到环境变量PATH中。当需要使用另外一套工具链时，设置Eclipse项目属性 - C/C++ Build - Environment，添加环境变量MINGW_HOME，并点选“Replace native environment with specified one”</p>
<div class="blog_h3"><span class="graybg">Cygwin</span></div>
<ol>
<li>安装Cygwin：<a href="http://cygwin.com/setup-x86.exe">http://cygwin.com/setup-x86.exe</a>，选择一个安装目录，例如（D:\CPP\tools\Cygwin\，以下称%CYGWIN_HOME%）</li>
<li>在Select Packages步骤下，选择Devel中的：autoconf、automake、binutils、gdb、gcc-core、gcc-g++、libtool、make，以及wget，下一步进行安装，其它需要的工具或者库，可以随时使用此安装程序安装</li>
<li>将 %CYGWIN_HOME%\bin、%CYGWIN_HOME%\usr\local\bin添加到PATH环境变量，以便Eclipse CDT识别此工具链</li>
<li>（可选步骤）为了后续安装软件包方便，可安装apt-cyg：<br />
<pre class="crayon-plain-tag">wget http://apt-cyg.googlecode.com/svn/trunk/apt-cyg
chmod +x apt-cyg
mv apt-cyg /usr/local/bin/</pre>
</li>
</ol>
<div class="blog_h3"><span class="graybg">LLVM</span></div>
<p>本节假设已经安装了MinGW工具链+MSYS，并在其上搭建LLVM工具链，步骤如下：</p>
<ol>
<li>安装Eclipse对LLVM工具链的支持：添加Update Site：<a href="http://petrituononen.com/llvm4eclipsecdt/update">http://petrituononen.com/llvm4eclipsecdt/update</a> 并安装</li>
<li>进入MSYS终端，执行以下脚本，以构建LLVM：<br />
<pre class="crayon-plain-tag">#签出LLVM源码
cd /src
svn co http://llvm.org/svn/llvm-project/llvm/trunk llvm
cd /src/llvm/tools
svn co http://llvm.org/svn/llvm-project/cfe/trunk clang
cd /src/llvm/projects
svn co http://llvm.org/svn/llvm-project/compiler-rt/trunk compiler-rt
#配置和构建
cd /src/llvm
./configure --prefix=/mingw32 --enable-optimized --disable-assertions 
make
make install</pre>
</li>
</ol>
<div class="blog_h3"><span class="graybg">MSVC</span></div>
<ol>
<li>安装Microsoft Visual Studio</li>
<li>Eclipse - Help - Install New Software，选择Eclipse CDT的Update Site，下一步中，在Optional中选择 C/C++ Visual C++ Support，进行安装</li>
</ol>
<div class="blog_h2"><span class="graybg">创建Eclipse C/C++工程</span></div>
<p>通过Eclipse - File - New - C/C++ Project即可创建C或者C++的工程，创建时需要选择工具链，某些工具链的需要额外的设置，具体如下：</p>
<div class="blog_h3"><span class="graybg">LLVM</span></div>
<ol>
<li>C/C++ General - Path and Symbols <br />Includes - GNU C++<br />
<table style="width: 100%;" border="1" cellspacing="0" cellpadding="5">
<thead>
<tr>
<td style="text-align: center;">Debug</td>
</tr>
</thead>
<tbody>
<tr>
<td>%MinGW_HOME%\include<br />%MinGW_HOME%\lib\gcc\mingw32\4.8.1\include<br />%MinGW_HOME%\lib\gcc\mingw32\4.8.1\include\c++<br />%MinGW_HOME%\lib\gcc\mingw32\4.8.1\include\c++\mingw32</td>
</tr>
</tbody>
</table>
<p>Symbols - GNU C++</p>
<table style="width: 100%;" border="1" cellspacing="0" cellpadding="5">
<thead>
<tr>
<td style="text-align: center;"> Debug</td>
<td style="text-align: center;">Release </td>
</tr>
</thead>
<tbody>
<tr>
<td> __MSVCRT__=1</td>
<td> __MSVCRT__=1</td>
</tr>
</tbody>
</table>
<p>Libraries - GNU C++</p>
<table style="width: 100%;" border="1" cellspacing="0" cellpadding="5">
<thead>
<tr>
<td style="text-align: center;"> Debug</td>
<td style="text-align: center;">Release </td>
</tr>
</thead>
<tbody>
<tr>
<td>stdc++</td>
<td>stdc++</td>
</tr>
</tbody>
</table>
<p>Library Paths- GNU C++</p>
<table style="width: 100%;" border="1" cellspacing="0" cellpadding="5">
<thead>
<tr>
<td style="text-align: center;"> Debug</td>
<td style="text-align: center;">Release </td>
</tr>
</thead>
<tbody>
<tr>
<td>%MinGW_HOME%\lib\gcc\mingw32\4.8.1</td>
<td>%MinGW_HOME%\lib\gcc\mingw32\4.8.1</td>
</tr>
</tbody>
</table>
</li>
<li>C/C++ Build - Settings <br /> Tool Settings - LLVM C++ Linker：Command改为clang</li>
<li>如果同时安装了Cygwin，可能需要把%Cygwin%下在环境变量PATH中条目移除；可能需要把MSYS相关的条目从PATH中移除</li>
</ol>
<div class="blog_h1"><span class="graybg">常见问题</span></div>
<div class="blog_h3"><span class="graybg">使用GCC时如何创建Windows窗体应用程序</span></div>
<p>设置链接标记，在Eclipse下：Properties - C/C++ Build -Settings - MinGW C++ Linker - Misc - Linker flags，添加<pre class="crayon-plain-tag">-mwindows</pre> ，即可提示链接器创建的是窗口应用，额外添加 -mconsole 则可同时获得一个控制台窗口。</p>
<div class="blog_h3"><span class="graybg">如何为应用程序添加资源文件</span></div>
<ol>
<li>Properties - C/C++ Build -Settings - Build Steps - Pre-build steps，添加调用windres的脚本，例子可以参考<a href="/wxwidgets-development-with-eclipse-cdt#prebuild-script-for-rc-file">《基于Eclipse CDT的wxWidgets开发环境搭建》</a></li>
<li>把上面编译好的*.o文件添加到Properties - C/C++ Build -Settings - MinGW C++ Linker - Misc - Other objects里</li>
</ol>
</div><p>The post <a rel="nofollow" href="https://blog.gmem.cc/eclipse-cdt-based-dev">Windows下基于Eclipse CDT的C/C++开发</a> appeared first on <a rel="nofollow" href="https://blog.gmem.cc">绿色记忆</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.gmem.cc/eclipse-cdt-based-dev/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cygwin知识集锦</title>
		<link>https://blog.gmem.cc/cygwin-faq</link>
		<comments>https://blog.gmem.cc/cygwin-faq#comments</comments>
		<pubDate>Tue, 13 Jul 2010 08:58:52 +0000</pubDate>
		<dc:creator><![CDATA[Alex]]></dc:creator>
				<category><![CDATA[C]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Cygwin]]></category>
		<category><![CDATA[FAQ]]></category>
		<category><![CDATA[Porting]]></category>

		<guid isPermaLink="false">http://blog.gmem.cc/?p=6657</guid>
		<description><![CDATA[<p>基本知识 Cygwin简介 Cygwin是一个用于Windows操作系统下的类Linux环境。包括以下组件： cygwin1.dll：一个提供大量POSIX系统调用功能的模拟层，位于Cygwin程序与Windows操作系统之间。目前有32/64位版本支持Windows XP3之后的所有NT系统。cygwin1.dll提供的API尽可能的遵守UNIX规范、Linux实践。与Linux不同，Cygwin使用的C标准库是newlib而不是glibc 一系列的工具集，提供类似Linux的行为，例如命令、类库等 当第一个基于Cygwin的进程创建时，cygwin1.dll被加载到其代码段，并创建共享内存区域、全局同步对象。这些区域和对象被所有Cygwin进程共享（虽然他们有各自的cygwin1.dll实例），可以用于跟踪打开的文件描述符，以及辅助fork、exec调用。每个Cygwin进程具有包含PID、UID、信号掩码的进程结构。 Cygwin与MinGW Cygwin与MinGW虽然都会被作为GNU工具链看待，但是二者在本质上是不同的。 Cygwin在某种程度上可以看作一个虚拟的操作系统层（如上所述，由cygwin1.dll驱动），在其上运行的应用程序可以使用绝大部分POSIX接口，这些程序感觉自己就在Linux上运行一样。 MinGW虽然也提供了POSIX风格的接口，但是这些接口的实现都是被对接到Win32 API上的。MinGW编译的程序是纯粹的Win32应用程序，因此也无法支持一些Linux下特有的系统调用，例如fork()。 Cygwin与虚拟机 Windows下的Linux虚拟机可以直接运行Linux应用程序，而Cygwin则不能运行，程序必须从源代码重新编译。Cygwin程序是标准的Windows PE格式。 不兼容性 由于Windows本身的限制，Cygwin无法保持POSIX兼容性，但是这样的情况比较少见。 Cygwin提供的POSIX兼容性和Windows API并非水乳交融，因此如果在应用中混用POSIX调用、Windows调用，可能导致意外结果。特别的，Cygwin信号与阻塞的Windows函数不能一起使用。 文件与权限 Cygwin同时支持POSIX、Win32风格的路径。传递给DLL的路径被转换为Win32格式；在Cygwin应用看来，文件系统是POSIX兼容的。 从1.7.0开始，/etc/fstab中包含了Windows文件系统到POSIX文件系统的映射关系。如果不进行默认设置，将进行类似这样的映射：[crayon-69d9299a23875458319448-i/]  <a class="read-more" href="https://blog.gmem.cc/cygwin-faq">[...]</a></p>
<p>The post <a rel="nofollow" href="https://blog.gmem.cc/cygwin-faq">Cygwin知识集锦</a> appeared first on <a rel="nofollow" href="https://blog.gmem.cc">绿色记忆</a>.</p>
]]></description>
				<content:encoded><![CDATA[<div class="wri_content_clear_both"><div class="blog_h2"><span class="graybg">基本知识</span></div>
<div class="blog_h3"><span class="graybg">Cygwin简介</span></div>
<p>Cygwin是一个用于Windows操作系统下的<span style="background-color: #c0c0c0;">类Linux环境</span>。包括以下组件：</p>
<ol>
<li>cygwin1.dll：一个提供大量POSIX系统调用功能的模拟层，位于Cygwin程序与Windows操作系统之间。目前有32/64位版本支持Windows XP3之后的所有NT系统。cygwin1.dll提供的API尽可能的遵守UNIX规范、Linux实践。与Linux不同，Cygwin使用的C标准库是newlib而不是glibc</li>
<li>一系列的工具集，提供类似Linux的行为，例如命令、类库等</li>
</ol>
<p>当第一个基于Cygwin的进程创建时，cygwin1.dll被加载到其代码段，并创建<span style="background-color: #c0c0c0;">共享内存区域、全局同步对象</span>。这些区域和对象被所有Cygwin进程共享（虽然他们有各自的cygwin1.dll实例），可以用于跟踪打开的文件描述符，以及辅助fork、exec调用。每个Cygwin进程具有包含PID、UID、信号掩码的进程结构。</p>
<div class="blog_h3"><span class="graybg">Cygwin与MinGW</span></div>
<p>Cygwin与MinGW虽然都会被作为GNU工具链看待，但是二者在本质上是不同的。</p>
<p>Cygwin在某种程度上可以看作一个<span style="background-color: #c0c0c0;">虚拟的操作系统层</span>（如上所述，由cygwin1.dll驱动），在其上运行的应用程序可以使用绝大部分POSIX接口，这些程序感觉自己<span style="background-color: #c0c0c0;">就在Linux上运行一样</span>。</p>
<p>MinGW虽然也提供了POSIX风格的接口，但是这些接口的实现都是<span style="background-color: #c0c0c0;">被对接到Win32 API上</span>的。MinGW编译的程序是<span style="background-color: #c0c0c0;">纯粹的</span>Win32应用程序，因此也无法支持一些Linux下特有的系统调用，例如fork()。</p>
<div class="blog_h3"><span class="graybg">Cygwin与虚拟机</span></div>
<p>Windows下的Linux虚拟机可以直接运行Linux应用程序，而Cygwin则不能运行，程序必须从源代码重新编译。Cygwin程序是标准的Windows PE格式。</p>
<div class="blog_h3"><span class="graybg">不兼容性</span></div>
<p>由于Windows本身的限制，Cygwin无法保持POSIX兼容性，但是这样的情况比较少见。</p>
<p>Cygwin提供的POSIX兼容性和Windows API并非水乳交融，因此如果在应用中<span style="background-color: #c0c0c0;">混用</span>POSIX调用、Windows调用，可能导致意外结果。特别的，Cygwin信号与阻塞的Windows函数不能一起使用。</p>
<div class="blog_h3"><span class="graybg">文件与权限</span></div>
<p><span style="background-color: #c0c0c0;">Cygwin同时支持POSIX、Win32风格的路径</span>。传递给DLL的路径被转换为Win32格式；在Cygwin应用看来，文件系统是POSIX兼容的。</p>
<p>从1.7.0开始，/etc/fstab中包含了Windows文件系统到POSIX文件系统的映射关系。如果不进行默认设置，将进行类似这样的映射：<pre class="crayon-plain-tag">C:\Windows -&gt; /cygdrive/c/Windows</pre>   </p>
<p>Windows NT包含了一个基于ACL的安全模型。在支持此ACL的文件系统（NTFS）上，Cygwin将Win32文件所有权、权限映射到ACL。chmod调用将<span style="background-color: #c0c0c0;">UNIX文件权限映射到Win32等价的权限</span>上。</p>
<p>具有管理员权限的用户可以chown文件所有者。</p>
<p>Cygwin 1.1.3引入了设置Real UID、Effective UID的机制。</p>
<div class="blog_h3"><span class="graybg">进程创建</span></div>
<p>Cygwin中的fork调用没有很好的映射到Win32 API上。fork()调用本身的语义要求父子进程的地址空间布局完全一致，而Windows本身没有提供在进程间拷贝地址空间的机制，此外Windows下的一些特性可能破坏fork()实现的可靠性，这导致了一些流行问题：</p>
<ol>
<li>DLL基址冲突：与类UNIX系统使用“位置无关代码”的共享库不同，Windows共享库假设了一个固定的基址。一旦同一个进程加载的两个DLL的基址冲突了，Windows就要将其中一个“rebase”到不同的地址上，这种rebase行为具有不一致性：可能每次rebase的DLL不同，移动到的目的位置也不同。Cygwin可以为动态加载的DLL补充前述的不一致性。但是对于静态链接的DLL（statically-linked）之间的冲突，是在cygwin.dll加载之前就被Windows处理的，Cygwin无能为力，只能通过<span style="background-color: #c0c0c0;">rebaseall</span>工具处理</li>
<li>Windows Vista开始引入的地址空间布局随机化（ASLR），允许线程栈、堆、内存映射文件、静态链接DLL随机的定位到进程的地址空间。此行为会干涉fork()的正常行为。如果不可移动对象（进程堆、系统DLL）被定位在错误的位置，Cygwin无法进行补偿行为</li>
</ol>
<p>总之，现有的Windows实现导致无法实现完全可靠的fork()，偶发性的fork()失败不可避免。</p>
<div class="blog_h3"><span class="graybg">信号</span></div>
<p>当Cygwin进程启动后，会有一个辅助线程被用于信号的处理，该线程等待Windows事件，并将其转换为信号，当进程收到信号后，会根据信号bitmask的设置进行合适的处理。大部分的UNIX信号被支持。</p>
<div class="blog_h3"><span class="graybg">套接字</span></div>
<p>在Cygwin中与Socket相关的调用基本上是调用了Winsock的对应物——MS的Berkeley Socket实现，但是使用了一些技巧。例如，为了允许POSIX信号来中断阻塞调用，所有的Socket调用在底层都是非阻塞的。</p>
<p>由于地址族AF_UNIX在Winsock下不可用，Cygwin利用本地AF_INET代替之，但是这对程序是透明的。</p>
<div class="blog_h3"><span class="graybg">select</span></div>
<p>UNIX的select()函数也不能清晰的映射到Win32 API。在Windows下，select()只能用于套接字的处理，而UNIX下可以用于各种类型的文件描述符。</p>
<div class="blog_h2"><span class="graybg">安装与配置</span></div>
<p>下载<a href="https://www.cygwin.com/setup-x86.exe" target="_blank">setup-x86.exe</a>到安装目录，例如D:\CPP\tools\Cygwin，双击即可安装，设置好安装目录、下载目录后，选择163的镜像即可安装。Cygwin会显示已经port的软件包的列表，根据需要安装即可。</p>
<p>如果想安装基于AMD64的cygwin，可以下载setup-x86_64.exe，但是64位的Cygwin中，类库和工具没有32位的全面。</p>
<div class="blog_h3"><span class="graybg">常用的软件包</span></div>
<table style="width: 100%;" border="1" cellspacing="0" cellpadding="5">
<thead>
<tr>
<td style="width: 150px; text-align: center;">软件包 </td>
<td style="text-align: center;">说明 </td>
</tr>
</thead>
<tbody>
<tr>
<td>apt-cyg</td>
<td>安装此软件后，不需要每次都打开setup-x86.exe进行软件安装，只需要使用类似于apt-get方式即可安装：<br />
<pre class="crayon-plain-tag">wget http://apt-cyg.googlecode.com/svn/trunk/apt-cyg 
chmod +x apt-cyg mv apt-cyg /usr/local/bin/ 
#切换源 
apt-cyg update -m http://mirrors.163.com/cygwin</pre></p>
<p>注：目前Cygwin已经修改了散列算法，对于apt-cyg 0.95，应该修改343行的md5sum为sha512sum</p>
</td>
</tr>
<tr>
<td>ncurses </td>
<td>与Terminal使用相关的小工具，例如clear命令</td>
</tr>
<tr>
<td>cygserver</td>
<td>该工具自动安装，需要运行 /usr/bin/cygserver-config进行必要的配置。cygserver用于支持以“服务”默认来运行Cygwin应用程序。配置步骤如下：<br />
<pre class="crayon-plain-tag">#下载需要的软件
apt-cyg install cygrunsrv
#只需配置，会依次提示：
#... /etc/cygserver.conf file?  yes
#install cygserver as service?  yes
/usr/bin/cygserver-config</pre></p>
<p>完毕后，在Windows系统服务中可以看到一个“CYGWIN cygserver”，启动即可。</p>
</td>
</tr>
<tr>
<td>Devel目录</td>
<td>开发人员必备的软件包都在该目录下，对于C/C++开发，需要AutoTools（autoconf、automake）、binutils、cmake、cygwin-devl、gcc-core、gcc-g++、gdb、libtool、make</td>
</tr>
<tr>
<td>Cygwin/X</td>
<td>在Cygwin下运行基于X Windows的GUI应用，例如gedit，需要用到，安装步骤：<br />
<pre class="crayon-plain-tag">apt-cyg install xorg-server xinit xorg-docs X-start-menu-icons
#通过下面的命令可以在多窗口模式下启动一个X server，并且附加一个X terminal
/usr/bin/startxwin
#可以安装桌面管理器，例如：
apt-cyg install mate-session-manager
#安装完上面的MATE后，会发现字体渲染的比较粗糙，可以选用Apple LiHei、XCross_Mono.Clear分别
#作为一般字体、等宽字体（字体文件存至~/.fonts），并通过
#System - Preferences - Appearance -fonts 设置渲染方式为Subpixel Smoothing</pre>
</td>
</tr>
<tr>
<td>OpenSSH</td>
<td>安装头SSH服务要求cygserver被安装。步骤如下：<br />
<pre class="crayon-plain-tag">apt-cyg install openssl openssh
ssh-host-config
#根据提示进行操作：
#if privilege separation should be used 选择no
#if sshd should be installed as a service 选择yes
#其余选择no
#完毕后，会多出一个NT服务，可以按如下方式启动之：
#net start "CYGWIN sshd"
#或者：
#cygrunsrv --start sshd</pre></p>
<p> 默认的用户密码就是当前Windows的用户密码</p>
</td>
</tr>
<tr>
<td>输入法</td>
<td>
<p>在Cygwin/X下使用输入法，推荐ibus-pinyin，需要安装的包包括：ibus、ibus-pinyin、dconf（不安装的话配置无法生效）。</p>
<p>安装完毕后，设置ibus为自动启动（以MATE为例：System - Perferences - Startup Applications 添加“/usr/bin/ibus-daemon -d”）</p>
</td>
</tr>
</tbody>
</table>
<div class="blog_h2"><span class="graybg">Cygwin下的编程</span></div>
<p>在Cygwin下使用gcc，与在Linux下的用法一样，下面是编译、运行HelloWorld的例子：</p>
<pre class="crayon-plain-tag">gcc hello.c -o hello.exe  #注意后缀
hello.exe
#打印：Hello, World</pre>
<div class="blog_h3"><span class="graybg">构建64位应用程序</span></div>
<p>64位的Cygwin工具链默认使用微软x64调用协定。可以使用32位的Cygwin工具链、Win32 API来构建应用程序。</p>
<p>有一点需要注意的，<span style="background-color: #c0c0c0;">64位Cygwin使用了和Windows、MinGW编译器不同的数据模型</span>。后两者使用LLP64模型，而Cygwin使用与Linux一致的LP64模型。这两个模型一个重要区别是，前者的<pre class="crayon-plain-tag">sizeof(long) == 4</pre> 而后者的<pre class="crayon-plain-tag">sizeof(long) == 8</pre> （与指针类型、size_t、ssize_t长度一致），该区别会对包括<span style="background-color: #c0c0c0;">LONG, ULONG, DWORD</span>在内的Win32类型用法产生影响。你不能假设DWORD与ssize_t的长度是一致的4字节整数。</p>
<div class="blog_h3"><span class="graybg">构建GUI应用</span></div>
<p>Cygwin内置了X Server，因此可以把GUI英语编译为X applications。此外Cygwin允许你<span style="background-color: #c0c0c0;">完整的使用Windows的GUI函数</span>。构建GUI应用与命令行应用没有什么区别，只需要添加一个选项：<pre class="crayon-plain-tag">gcc -mwindows</pre> 。</p>
<div class="blog_h3"><span class="graybg">构建和使用DLL</span></div>
<p>DLL即动态链接库，其代码在运行期间动态的链接到应用程序，而不是在编译期间就静态的链接。DLL包括三个组成部分：</p>
<ol>
<li>导出（exports ）：包含了DLL允许外部访问的函数、变量的列表，其他部分对外隐藏</li>
<li>数据和代码：是DLL作者编写的部分，包括函数、变量，被合并为一整个的目标文件</li>
<li>导入库（import library）：形式上与UNIX常规的*.a库一样，但是其中仅仅包含了很少的信息，用于告知OS，你的应用程序如何去“导入”DLL，该文件被链接到应用程序</li>
</ol>
<p>基于最新版本的gcc和binutils，构建一个DLL的过程相当简单，下面是一个例子：</p>
<pre class="crayon-plain-tag">#include 

int hello()
{
    printf("Hello World!\n");
}</pre>
<div>首先编译出目标代码（Object code）：<pre class="crayon-plain-tag">gcc -c hello.c</pre> </div>
<div>然后，告诉gcc你希望构建一个共享库：<pre class="crayon-plain-tag">gcc -shared -o hello.dll hello.o</pre> </div>
<div>要使用这个共享库，只需要在你的代码中直接调用hello()，并使用类似下面的命令链接： <pre class="crayon-plain-tag">gcc -o SayHello SayHello.c -L./ -lhello</pre> </div>
<p> 实际应用中，你可能需要更复杂的命令来构建DLL，例如：</p>
<pre class="crayon-plain-tag">#共享库的名字为module，前缀cyg用来将其与Native的Windows DLL区分
gcc -shared -o cyg${module}.dll 
#libmodule.dll.a为用于导入的库stub
  -Wl,--out-implib=lib${module}.dll.a
#导出所有变量、函数
  -Wl,--export-all-symbols
  -Wl,--enable-auto-import
#需要整个捆绑到DLL中的目标文件、静态库
  -Wl,--whole-archive ${old_libs}
#该DLL需要与之链接的DLL的导入库
  -Wl,--no-whole-archive ${dependency_libs}</pre>
<div class="blog_h3"><span class="graybg">使用已有的DLL</span></div>
<p>如果要在Cygwin中使用已有的DLL，则需要生成Cygwin兼容的导入库，下面是一个示例：</p>
<pre class="crayon-plain-tag">echo EXPORTS &gt; hello.def
#列出DLL中的符号，将其输出到文件
nm foo.dll | grep ' T _' | sed 's/.* T _//' &gt;&gt; hello.def
#生成导入库
dlltool --def hello.def --dllname hello.dll --output-lib hello.a</pre>
<div class="blog_h2"><span class="graybg">常见问题</span></div>
<div class="blog_h3"><span class="graybg">Eclipse CDT无法识别工具链：Toolchain "Cygwin GCC" is not detected</span></div>
<p>可能是环境变量配置错误，必须在PATH中包含<pre class="crayon-plain-tag">%CYGWIN_HOME%\bin</pre> Eclipse CDT才能识别成功。</p>
</div><p>The post <a rel="nofollow" href="https://blog.gmem.cc/cygwin-faq">Cygwin知识集锦</a> appeared first on <a rel="nofollow" href="https://blog.gmem.cc">绿色记忆</a>.</p>
]]></content:encoded>
			<wfw:commentRss>https://blog.gmem.cc/cygwin-faq/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
