使用MinGW来编译程序 (代替VC++)

1. MinGW 是什么?

        MinGW 提供了一套简单方便的Windows下的基于GCC 程序开发环境。MinGW 收集了一系列免费的Windows 使用的头文件和库文件;同时整合了GNUhttp://www.gnu.org/ )的工具集,特别是GNU 程序开发工具,如经典gcc, g++, make等。MinGW是完全免费的自由软件,它在Windows平台上模拟了Linux下GCC的开发环境,为C++的跨平台开发提供了良好基础支持,为了在Windows下工作的程序员熟悉Linux下的C++工程组织提供了条件。

2. 为什么使用MinGW?

        因为我和很多IT人士一样,不愿意吊死在$M这棵树上。且不说开源这个理念的伟大,仅仅就Windows的安全性也已经让人们很不安心了。如果再深入一点从编写程序来看,在C++标准尚不成熟时期产生的MFC今天看来基本上就是盖子大叔凭着个人(或许是$M公司^^)理解所使用的C++方言。在我们期盼C++0x时代到来之前,没有任何实际行动能比严格遵循C++的ISO标准更能表达我们对于C++这个伟大的哲学体系的尊重。从这个角度来说,遵循ISO标准的实现就是好的实现。所以,MinGW不会比VC差,况且,他既是开源软件家族中的一员,也是*nix平台下事实标准GCC的翻版。如果有一天我们开始使用Linux平台了,会因为今天透过MinGW所了解的GCC知识而受益。


3. MinGW 和 Cgwin的区别

MinGW
        MinGW 官方网站为  http://www.mingw.org/      
        MinGW,即 Minimalist GNU For Windows(GCC compiler suite)。它是一些头文件和端口库的集合,该集合允许人们在没有第三方动态链接库的情况下使用 GCC(GNU Compiler C)产生 Windows32 程序。
 
        MinGW:一个可自由使用和自由发布的Windows特定头文件和使用GNU工具集导入库的集合,在基本层,MinGW 是一组包含文件和端口库,其功能是允许控制台模式的程序使用微软的标准C运行时间库(MSVCRT.DLL),该库在所有的 NT OS 上有效,在所有的 Windows 95 发行版以上的 Windows OS 有效,使用基本运行时,你可以使用 GCC 写控制台模式的符合美国标准化组织(ANSI)程序,可以使用微软提供的 C 运行时扩展。该功能是 Windows32 API 不具备的。下一个组成部分是 w32api 包,它是一组可以使用 Windows32 API 的包含文件和端口库。与基本运行时相结合,就可以有充分的权利既使用 CRT(C Runtime)又使用 Windows32 API 功能。
 
       实际上 MinGW 并不仅是一个 C/C++ 编译器,而是一套 GNU 工具集合。除开 GCC (GNU 编译器集合) 以外,MinGW 还包含有一些其他的 GNU 程序开发工具 (比如 gawk bison 等等)。
 
       开发 MinGW 是为了那些不喜欢工作在 Linux(FreeBSD) 操作系统而留在 Windows 的人提供一套符合 GNU 的 GNU 工作环境。所以,使用 MinGW 我们就可以像在 Linux 下一样使用 GNU 程序开发工具。
 
      GCC 就是 MinGW 的核心所在,GCC 是一套支持众多计算机程序语言的编译系统,而且在语言标准的实现上是最接近于标准的。并且 GCC 几乎可以移植到目前所有可用的计算机平台。
 
      GCC 本身不像VC那样拥有IDE界面(但是有很多的开源的IDE支持使用MinGW,例如codeblocks,eclipse等)。源代码编辑你可以选用任何你喜欢的文本编辑器(据说微软的开发人员包括 VC 的开发都不用 VC 所带的 IDE 编辑器,而是选用 GNU 的 VIM 编辑器)。然后使用 make 等工具来进行软件项目的编译、链接、打包乃至发布。而像 cvs(svn) 源代码版本控制工具可以让世界上任何一个角落的人都可以参与到软件项目中来。
 
  关于 MFC,微软基础库类,这个随 VC++ 携带的一个源代码公开的开发包,和其他 Windows 程序开发包是一样的。如果有 VC++ 的授权,你完全可以使用 MFC 的源代码,也就是你使用 GCC 来编译 MFC 程序是完全可以的。
 
  当然,GNU 下也有很多 Windows 程序开发包,甚至有一些是支持跨平台使用的。不仅仅可以直接把源代码编译为 Windows 程序,也可以不经修改编译为其他操作系统的图形程序。
 
二 MSYS 
       官方网站为  http://www.mingw.org/ 
       MSYS:Unix-like command line utilities,包括基本的bash, make, gawk and grep 等等。通常也可以认为是小型的UNIX on Windows。提供在windows上模拟Unix环境来使用MinGW。
 
       MSYS在windows下模拟了一个类unix的终端,它只提供了MinGW的用户载入环境,在MSYS模拟的unix环境下使用MinGW,就像在Unix使用gcc一样。
 
三 cygwin/gcc
       cygwin/gcc和MinGW都是gcc在windows下的编译环境,但是它们有什么区别,在实际工作中如何选择这两种编译器。
 
       cygwin/gcc完全可以和在linux下的gcc化做等号,这个可以从boost库的划分中可以看出来端倪,cygwin下的gcc和linux下的gcc完全使用的是相同的Toolsets。所以完全可以和linux一起同步更新gcc版本,而不用担心问题,并且在cygwin/gcc做的东西(不用win32的)可以无缝的用在linux下,没有任何问题。是在windows下开发linux程序的一个很好的选择。
 
       但是在cygwin/gcc下编译出来的程序,在windows执行必须依赖cygwin1.dll,并且速度有些慢,如果不想依赖这个东西的化,必须在gcc的编译选项中加入-mno-cygwin。加入这个选项其实gcc编译器就会自动的选择在安装cygwin/gcc时安上的mingw,这个mingw就是gcc的一个交叉编译。
 
      对于mingw作为gcc在windows上的一个实现,由于不像cygwin的gcc在一个模拟linux上运行,同时相当一部分linux的工具不能够使用,不过现在已经有Msys这个模拟unix的shell,可以解决很多的问题。
 
四 总结
      MinGW是windows版本的gcc集合,不需要依赖中间层。
      MSYS是小型的linux的环境的模拟,可以与MinGW结合来模拟linux环境下使用MinGW的gcc。
      Cygwin是功能强大的linux环境,由于有cygwin1.dll实现了底层的windows api到linux api的转化。所以在Cygwin里开发就相当于在linux上开发,对于开发人员来说就相当于调用linux类型的api,所以这样开发的程序也可以直接移植到linux上。但是如果这样的程序要在windows上执行的话,运行时必须要cygwin1.dll支持。
 
      根据以上的分析,如果在windows开发linux跨平台的程序,linux模拟器Cygwin以及所包含的gcc是很好的选择,但是开发的程序必须依赖一个cygwin1.dll。如果你只是想在windows下使用gcc编译器也不想依赖其他的dll,mingw是很好的一个选择。

4. 安装MinGW

MinGW may have problems with paths containing spaces, and if not, usually other programs used with MinGW will experience problems with such paths. Thus, we strongly recommend that you do not install MinGW in any location with spaces in the path name reference. You should avoid installing into any directory or subdirectory having names like "Program Files" or "My Documents", etc.

No version numbering convention exists for MinGW as a whole. Each package has its own version number, and the installer version number does not correlate, in any way, with the version number of any individual package which it installs.

The preferred installation target directory is C:\MinGW

The preferred installation method is using the automated installer tool. While a manual installation is possible, we do not reccommend it. Instructions are provided below if you really want to risk it.

After installation you will need to perform some additional tasks yourself to ensure your MinGW applications will run. Most importantly, you will need to make changes to your PATH environment variable. Be careful: there are two PATH variables which can be changed, and changing the wrong one can have significant consequences. Instructions for changing the correct PATH variable are provided below.

You will also need to ensure you specify the correct installation target directory to the MinGW environment. While it is possible to install MinGW to a location other than C:\MinGW (ie on a different drive), you must ensure that the MinGW environment knows where it is located. Instructions for doing this are also provided below.

Graphical User Interface Installer

An automated GUI first time installer tool called mingw-get-inst is currently the preferred method of installation.

The mingw-get-inst tool will only facilitate the initial set up of the CLI (Command Line Interface) mingw-get installer. Note that the mingw-get-inst tool is not an official GUI installer; it is intended for first time use only, to set up the CLI installer which should be used for all subsequent updates or modifications.

An official GUI installer is currently under development, but remains temporarily unpublished. For those willing to assist with the evaluation and testing of the GUI installer development snapshots, these will be made available from the developer's personal SourceForge project pages, (see below).

To use the mingw-get-inst automated first time installer tool, do the following:

  1. Click on this mingw-get-inst link to take you to the installation tool server directory.
  2. Click on the directory that has the most recent date string (yyyymmdd) in its name.
  3. Locate the file named "mingw-get-inst" ending in ".exe", and having the most recent date string (yyyymmdd) in its name.
  4. Click on that link and save the file to your local hard drive.
  5. Locate the file you have downloaded, and double click on it to start the installer.
  6. Locate the directory into which you want to install MinGW (ie C:\MinGW). Use the advice given above: that the absolute path to this directory should not contain any spaces.
  7. Select the option to update package catalogues to latest available. NOTE: If you attempt to use the bundled catalogues, you may install a hopelessly out-of-date system.
  8. Select the optional components to be installed, including the C++, Fortran, Objective-C, and Ada compilers and MSYS.
  9. The set up tool will download and run the mingw-get CLI installer, to install the selected component packages. NOTE: The make and GDB packages are installed automatically by the set up tool.

NOTE: The preferred installation is to always include the MSYS addon, regardless of other packages you also choose.

NOTE: In many cases, you will probably need to add the C++ compiler as well

(The MinGW release notes contain more detailed information about the configuration and use of the mingw-get-inst set up tool).

After Installing You Should

If you used the automated installation tool and installed MSYS as an addon to MinGW, you do not need to change your PATH environment variable.

If you used either of the GUI installers (the mingw-get-inst tool or the unofficial installer snapshot), and you installed the MSYS add on, they will create a start menu "MinGW Shell" shortcut in your "Start \ Programs \ MinGW" folder. Double clicking the shortcut will open a command window with the correct environment setup for you, including the correct path references, allowing you to run any of the MinGW or MSYS applications within that command window.

Regardless of how you installed MinGW or MSYS, you should check your MSYS configuration (if you have installed MSYS). You need to ensure that MSYS knows where it is located:

  1. Open a Windows Explorer window and locate your installation directory (ie C:\MinGW)
  2. Below your installation directory, you should find a directory named "msys" in lower case letters.
  3. Find (and open) the additional subdirectories "1.0" and "etc".
  4. Within the "etc" directory, there is a file named "fstab". Open it with a text editor. You should be able to do this by right clicking on it, and in the popup menu select "Open" or "Open With"
  5. Edit the file and ensure that it reads as follows:
<installation-directory>   /mingw

NOTE: Substitute <installation-directory> with the FULL and absolute path to where you installed MinGW (ie C:\MinGW). Make sure you follow the <installation-directory> entry with at least one space before typing the "/mingw" entry.

NOTE: Before you save the file, make sure there is at least one blank line at the bottom below all of the entries that may exist, then save and close the file.

Your MinGW and MSYS installations should now be ready for use.

Caveats: (exceptions)

If you ever intend to run MinGW or its applications using a cmd.exe window, you will need to make changes to your PATH environment variable. Instructions for doing so are listed in the Environment Settings section below.


5. 下载一个文本编辑器(vim)来写代码

这又是一个有着传统的“悠久”历史,很好很强大的,免费开源的,牛x文本编辑器。官方主页是:
http://www.vim.org/
        不用怀疑,你可以很容易的下载Vim的安装程序,并且很容易的安装,不需要任何的“破解”-_-!!
        因为Vim很好很强大,所以,太多的内容就留给包括我自己在内的大家自己去学了(我的逻辑Orz)。我们这里简单的只需要了解如下几点:
1) 建立一个名为 abc.def 的文本文件,在console窗口下输入:
vim abc.def
你就进入了vim的编辑界面。
2) 按"i"键,就可以输入文本了。退回到编辑方式,按Esc,再进入插入方式,再按i,就这样...
3) vim在编辑方式下本来的光标移动是h, j, k, l,不过,上下左右箭头也是可以使用的。
4) 编辑完成后,(编辑方式下)按Shift+Z两次,或者:wq就可以保存退出。使用:q!可以不保存强制退出。


6.  MinGW的简单使用.


        继续刚才的话题,我们建立一个叫 hello.cpp 的文件(据说Hello World是程序员的咒语...)

#include  < iostream >

int  main( )
{
    std::cout 
<<   " Hello World. "   <<  std::endl;
    
return   0 ;
}

        我们可以简单的使用命令:g++ hello.cpp,与最传统的Unix系统下的C编译器一样,MinGW为我们生成了一个名为a的可执行文件,当然,在win32下,名字叫做a.exe。
        可以使用参数 -o 来指定编译的可执行文件的名字:g++ -o hello.exe hello.cpp (或者也可以不写名exe,直接g++ -o hello hello.cpp 结果一样).这样,得到的文件就是hello.exe。
        可以使用参数 -c 来生成与源文件名相同的目标文件:g++ -c hello.cpp ,得到的文件是hello.o .
        目标文件可以继续连接成可执行文件:g++ hello.o ,得到文件a.exe .
        多个源文件,目标文件和库文件都可以同时编译和连接.


7. 注意1

   用MinGW编译出来的可执行文件在执行的时候出现

 

原因是MinGW在使用g++编译的时候默认是动态连接,所以可执行程序在执行的时候需要这个链接这个DLL. 这个DLL文件在MinGW/bin里面,所以可以把这个DLL文件拷贝到你可执行程序相同的目录下,或者在编译的时候采用静态链接,把这个DLL编译到可执行程序里面去,这当然会增加可执行程序文件的大小。


在正常编译的后面加入-static参数,exe文件的大小也从几十K变成了1M多。但是这样的话,既然拷贝你的程序到别人的机子上也是能够运行的。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值