【开发工具】Window下MinGW下载安装gcc,g++编译器

本文详细指导了在Windows平台上安装MinGW,包括下载、安装过程、常见问题解决、动态链接库生成以及gcc、g++、cc和CC的区别。重点介绍了如何配置环境变量和使用gcc/g++进行C/C++程序编译。
摘要由CSDN通过智能技术生成

一.前言

要知道,GCC 官网提供的 GCC 编译器是 无法直接安装到 Windows 平台上的,如果我们想在 Windows 平台使用 GCC 编译器,可以安装GCC 的移植版本

  • 目前适用于 Windows 平台、受欢迎的 GCC 移植版主要有 2 种,分别为MinGW 和 Cygwin
  • MinGW 侧重于服务 Windows 用户可以使用 GCC 编译环境,直接生成可运行 Windows 平台上的可执行程序,相比后者体积更小,使用更方便
  • Cygwin 则可以提供一个完整的 Linux 环境,借助它不仅可以在 Windows 平台上使用 GCC 编译器,理论上可以运行 Linux 平台上所有的程序。有更高的需求(例如运行 POSIX应用程序),就选择安装 Cygwin。

仅 Windows 平台上使用 GCC,可以使用 MinGW 或者 Cygwi

二.须知

注意搭建GCC编译环境时,一定要选择正确的GCC编译版本(32位和64位)。如果你本地安装的JDK是64位的,那么选择64位GCC,否则选择32位。这是为了使得编译后的库文件跟JVM的位一致,否则后面JVM无法调用dll(或so)而抛出异常

java.lang.UnsatisfiedLinkError: E:\dev_tools\jdk\jdk1.8.0_181\bin\HelloJNI.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform

在这里插入图片描述

因此

  1. Linux下编译后的可执行文件只能在Linux下运行,Windows编译后的程序只能在Windows下运行。
  2. 64位的Linux编译后的程序只能在64位Linux下运行,32位的Linux编译后的只能在32位的Linux下运行。
  3. 32位Windows编译后的程序可以在64位或32位下windows下运行,64位windows编译后的程序只能在64位windows下运行。

三.MinGW下载

http://sourceforge.net/projects/mingw/  #32位
https://sourceforge.net/projects/mingw-w64/  #64位

四.安装器安装

1.打开下载好的mingw-get-setup.exe ,这里选择安装目录为E:\dev_tools\MinGW

在这里插入图片描述

2.下载安装gcc/g++

  • 通过MinGW Installation Manager下载gcc/g++。勾选mingw32-gcc-g++-bin,点击Apply Changes,下载安装gcc/g++

在这里插入图片描述

在这里插入图片描述

  • 为使 GCC 同时支持编译 C 语言和 C++,需勾选上图图中标注的 2 项。我们知道,GCC 还支持其它编程语言,读者可借助此配置器,随时实际需要安装自己需要的编译环境。勾选完成后,在菜单栏中选择Installation -> Apply Changes,弹出如下对话框:
    在这里插入图片描述

3.配置环境变量

  • 打开控制面板 -> 系统和安全 -> 系统 ->高级系统设置 -> 环境变量。
  • ​ 编辑系统变量Path,添加一个环境变量E:\dev_tools\MinGW\bin,具体为MinGW根目录下的\bin目录,用英文分号 ;隔开。

在这里插入图片描述

4.检验是否成功下载安装gcc/g++

  • 打开cmd终端,输入gcc -vg++ -v检验gcc与g++是否安装并配置变量成功。若如下图所示,则成功。
    在这里插入图片描述

五.出现的问题

1.在步骤2(下载安装gcc/g++)时,出现部分package无法成功下载:

  • 具体是因为URL丢失libgmp-6.1.2-2-mingw32-dll-10.tar.xzlibmpfr-3.1.5-1-mingw32-dll-4.tar.xz,两个package包,从而导致在进行g++编译的时候出现重要组件libgmp-10.dlllibmpfr-4.dll的丢失。

在这里插入图片描述
在这里插入图片描述

解决方案:

  • ​ 跟已经下载安装好的同学拷贝这2个package包,又或者从搭配有MinGW的轻便型IDE如Dev-cpp、Code Blocks路径下拷贝这两个package包,其路径为MinGW根目录下的\var\cache\mingw-get\packages。
    ​ 然后解压缩这两个压缩包,将其中的组件libgmp-10.dlllibmpfr-4.dll复制到MinGW根目录下的\bin

六.解压安装

  1. 点击下面箭头指向的链接,在官网下载连接速度可能会非常慢…在这个链接下载会好很多。
    1

  2. 下载好之后,解压,得到下图文件:
    在这里插入图片描述
    可以看到bin文件夹里面有g++.exe和gcc.exe
    在这里插入图片描述

3.配置环境变量

  • 打开控制面板 -> 系统和安全 -> 系统 ->高级系统设置 -> 环境变量。
  • ​ 编辑系统变量Path,添加一个环境变量E:\dev_tools\MinGW64\bin,具体为MinGW根目录下的\bin目录,用英文分号 ;隔开。
    在这里插入图片描述

4.检验是否成功下载安装gcc/g++

  • 打开cmd终端,输入gcc -vg++ -v检验gcc与g++是否安装并配置变量成功。若如下图所示,则成功。
    在这里插入图片描述

七.MinGW的使用

以运行一个 C 语言程序为例(存储路径为:D:\demo.c)

#include <stdio.h>
#include <stdlib.h>
int main(){
    printf("Hello, World!");
    system("pause");
    return 0;
}

在此基础上,在命令行窗口中执行如下指令:

C:\Users\mengma>gcc D:\demo.c -o D:\demo.exe

其会在 D 盘生成一个 demo.exe 可执行文件,找到该文件并双击,即可看到程序的执行结果:

Hello, World!

八.如何生成动态链接库

在gcc环境准备好的条件下,接下来使用下面的命令将c生成dll动态链接库:

gcc -m64 -Wl,--add-stdcall-alias -I"D:\Program Files\Java\jdk1.8.0_261\include" 
-I"D:\Program Files\Java\jdk1.8.0_261\include\win32" 
-shared -o MyNativeDll.dll JniTestImpl.c

简单的解释一下各个参数的含义:

  • -m64 :将c代码编译为64位的应用程序
  • -Wl,--add-stdcall-alias:-Wl表示将后面的参数传递给连接程序,参数--add-stdcall-alias表示带有标准调用后缀@NN的符号会被剥掉后缀后导出
  • -I:指定头文件的路径,在生成的头文件代码中引入的jni.h就在这个目录下
  • -shared:指定生成动态链接库,如果不使用这个标志那么外部程序将无法连接
  • -o:指定目标的名称,这里将生成的动态链接库命名为MyNativeDll.dll
  • JniTestImpl.c:被编译的源程序文件名,当前文件为c

在指令的执行过程中,都做了什么事呢,可以参考下面这张图:
在这里插入图片描述
在执行过程中,以.c源代码.h头文件作为源文件,先进行了预处理、编译、汇编的操作,图中省略了这一阶段产生的一些中间文件,编译完成后生成的.o二进制文件相对重要,依赖这个文件,最终生成动态链接库。

九.编译器 cc、gcc、g++、CC 的区别

  • gcc 是GNU Compiler Collection,原名为Gun C语言编译器,因为它原本只能处理C语言,但gcc很快地扩展,包含很多编译器(C、C++、Objective-C、Ada、Fortran、 Java),可以说gcc是GNU编译器集合

  • g++ 是C++编译器

  • cc 是 Unix系统C Compile,一个是古老的 C 编译器。而Linux 下 cc 一般是一个符号连接,指向 gcc;可以通过 $ ls -l /usr/bin/cc来简单察看,该变量是 make 程序的内建变量,默认指向 gcc 。 cc 符号链接和变量存在的意义在于源码的移植性,可以方便的用 gcc 来编译老的用cc编译的Unix软件,甚至连 makefile 都不用改在,而且也便于 Linux 程序在 Unix下 编译。
    CC 则一般是 makefile 里面的一个名字标签,即宏定义,表示采用的是什么编译器(如:CC = gcc)。

误区一:gcc只能编译C代码,g++只能编译C++代码。

gcc两者都可以,但请注意:

  • 后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序;后缀为.cpp的,两者都会认为是C++程序,注意,虽然C++是C的超集,但是两者对语法的要求是有区别的。C++的语法规则更加严谨一些。
  • 编译阶段,g++会调用gcc,对于C++代码,两者是等价的,但是因为gcc命令不能自动和C++程序使用的库联接,所以通常用g++来完成链接,为了统一起见,干脆编译/链接统统用g++了,这就给人一种错觉,好像cpp程序只能用g++似的。

误区二:gcc不会定义__cplusplus宏,而g++会

  • 实际上,这个宏只是标志着编译器将会把代码按C还是C++语法来解释,如上所述,如果后缀为.c,并且采用gcc编译器,则该宏就是未定义的,否则,就是已定义。

误区三:编译只能用gcc,链接只能用g++

  • 严格来说,这句话不算错误,但是它混淆了概念,应该这样说:编译可以用gcc/g++,而链接可以用g++或者gcc -lstdc++。因为gcc命令不能自动和C++程序使用的库联接,所以通常使用g++来完成联接。但在编译阶段,g++会自动调用gcc,二者等价。

MinGW下载和安装教程
windows下安装MinGW及C++的环境配置

Installing c++/g++ on Windows Disclaimer: This page is being maintained mainly for my students. Use these instructions at your own risk. There is no warranty in any form or shape whatsoever!. There is no guarantee that these instructions are up-to-date. With that understood you may continue with the rest of this page if you choose to accept these terms. This page was last updated on September 13, 2005, but still good as of April 30, 2009. Follow these steps to install g++ (the GNU C++ compiler) for Windows. There is no room for creativity here; you must follow the directions exactly. Pick the drive and a folder in which you want to install g++. I'll assume that it is C:, but you can choose a different one. If you choose a different drive or a different folder, you'll need to adapt the directions below accordingly. Download full.exe, an about 14 megabyte executable, to C:\full.exe by right-clicking on the link. Use Save Link As... or Save Target As... Be sure the browser saves the file as C:\full.exe. Run the downloaded executable. This will install g++ (and a lot of other things that you don't really need) on your hard drive. Go to the C: drive using Windows Explorer and double-click on full.exe. Or, open a DOS window (Start > Programs > Command Prompt), connect to the C: drive using the cd command, and type full. Locate where the bin folder was created for the g++ installation. On my Windows XP machine, it was created in the following path: C:\cygnus\cygwin-b20\H-i586-cygwin32\bin You now should add it to the PATH environment variable. You do that by following: Start -> Control Panel -> System -> Advanced -> Environment Variables At this point you can see the PATH variable either in the User Variables or in the System Variables. Add the g++ path into the PATH variable. You add it to the end of the existing value separated by a semicolon (';'). Make sure that you do not lose the original value. You are just appending more to the end separated by a semicolon. Restart your computer. A Cygnus Solutions entry will appear in your Programs menu, and an icon may appear on your desktop. Don't use them! You will use it using the g++ command on a DOS prompt as explained below. You should now be able to run g++ from a DOS (Command Prompt) window. For example, to compile a file called C:\mine\hello.cpp, connect to the C:\mine folder and enter g++ -g hello.cpp -o hello -lm You'll then be able to run the compiled program by entering hello in the DOS window. If you've installed Emacs as described here, you will also be able to run g++ from Emacs. If, when you do this, Emacs tries to compile with the command make -k, you made a mistake during the Emacs installation. If you want to learn how to run g++ on emacs, see here. If you'd like to learn more about where this free compiler came from, we downloaded it from an older site of http://sourceware.org/cygwin/. If you wish to clean up a little, you may delete the file: full.exe at this point. Your g++ compiler is installed under C:\cygnus.
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墩墩分墩

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值