一.前言
要知道,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
因此
- Linux下编译后的可执行文件只能在Linux下运行,Windows编译后的程序只能在Windows下运行。
- 64位的Linux编译后的程序只能在64位Linux下运行,32位的Linux编译后的只能在32位的Linux下运行。
- 32位Windows编译后的程序可以在64位或32位下windows下运行,64位windows编译后的程序只能在64位windows下运行。
三.MinGW下载
http://sourceforge.net/projects/mingw/ #32位
https://sourceforge.net/projects/mingw-w64/ #64位
- 链接:https://pan.baidu.com/s/11Ad8R9yeNRZk4uoEHuYOvw
- 提取码:e0n9
- 64位、32位的都有
四.安装器安装
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根目录下的\bi
n目录,用英文分号 ;
隔开。
4.检验是否成功下载安装gcc/g++
- 打开cmd终端,输入
gcc -v
和g++ -v
检验gcc与g++是否安装并配置变量成功。若如下图所示,则成功。
五.出现的问题
1.在步骤2(下载安装gcc/g++)时,出现部分package无法成功下载:
- 具体是因为URL丢失
libgmp-6.1.2-2-mingw32-dll-10.tar.xz
,libmpfr-3.1.5-1-mingw32-dll-4.tar.xz
,两个package包,从而导致在进行g++编译的时候出现重要组件libgmp-10.dll
与libmpfr-4.dll
的丢失。
解决方案:
- 跟已经下载安装好的同学拷贝这2个package包,又或者从搭配有MinGW的轻便型IDE如
Dev-cpp、Code Blocks
路径下拷贝这两个package包,其路径为MinGW根目录下的\var\cache\mingw-get\packages。
然后解压缩这两个压缩包,将其中的组件libgmp-10.dll
与libmpfr-4.dll
复制到MinGW根目录下的\bin
。
六.解压安装
-
点击下面箭头指向的链接,在官网下载连接速度可能会非常慢…在这个链接下载会好很多。
-
下载好之后,解压,得到下图文件:
可以看到bin文件夹里面有g++.exe和gcc.exe
3.配置环境变量
- 打开控制面板 -> 系统和安全 -> 系统 ->高级系统设置 -> 环境变量。
- 编辑系统变量
Path
,添加一个环境变量E:\dev_tools\MinGW64\bin
,具体为MinGW根目录下的\bi
n目录,用英文分号 ;
隔开。
4.检验是否成功下载安装gcc/g++
- 打开cmd终端,输入
gcc -v
和g++ -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.dllJniTestImpl.c
:被编译的源程序文件名,当前文件为c
在指令的执行过程中,都做了什么事呢,可以参考下面这张图:
在执行过程中,以.c源代码
和.h头文件
作为源文件
,先进行了预处理、编译、汇编的操作,图中省略了这一阶段产生的一些中间文件,编译完成后生成的.o二进制文件相对重要
,依赖这个文件,最终生成动态链接库。
九.编译器 cc、gcc、g++、CC 的区别
-
gcc 是
GNU Compiler Collection
,原名为GunC语言编译器
,因为它原本只能处理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
,二者等价。