Posted on 2009-11-10 22:25
大源 阅读(12767)
评论(2) 编辑 收藏 引用 所属分类:
GCC
问题: 如何在Windows下使用gcc编译器编译C/C++程序
答:gcc是一个优秀的编译器,在开源世界得到广泛的应用。但是开源的系统只有linux, freebsd等等,那么,怎么在windows中使用gcc呢。MinGW是GNU开发环境在windows的移植,其中包含gcc, g++, g77等编译器,以及binutil二进制工具,并且包含相关的lib文件。所以可以在windows上安装MinGW,然后就可以编译C/C++程序,编译后的程序可以在windows的dos窗口中运行。下面是一个例子:
F:\Data\Code\GCC>dir
驱动器 F 中的卷没有标签。
卷的序列号是 3CB4-DD0A
F:\Data\Code\GCC 的目录
009-11-09 22:07 <DIR> .
009-11-09 22:07 <DIR> ..
009-10-22 09:05 786 1_1a.c
1 个文件 786 字节
2 个目录 32,035,512,320 可用字节
其中1_1a.c的代码如下:
1
/*=================================================*/
2
/* 程序实例:1_1a.c */
3
/* 计算总分和平均分 */
4
/*==================================================*/
5
6
7
#include <stdio.h>
8
#include <stdlib.h>
9
10
int main(int argc, char *argv[])
11
{
12
int t1, t2, t3, t4, t5, t6, t7, t8, t9, t10; /*各次的成绩*/
13
int sum; /*总分*/
14
int average; /*平均分*/
15
16
t1= 81;
17
t2= 93;
18
t3 = 77;
19
t4 = 59;
20
t5 = 69;
21
t6 = 85;
22
t7 = 90;
23
t8 = 83;
24
t9 = 100;
25
t10 = 75;
26
27
sum = t1 + t2 + t3 + t4 + t5 + t6 + t7 + t8 + t9 + t10; /*计算总分*/
28
average = sum / 10; /*计算平均分*/
29
printf("输出总分:%d\n", sum); /*输出总分*/
30
printf("输出平均分:%d\n", average); /*输出平均分*/
31
32
33
system("PAUSE");
34
return 0;
35
}
36
然后在当前目录下输入命令:
1
E:\mingw\bin\gcc.exe -Wall 1_1a.c -o 1_1a.exe
2
3
其中,要完整的输出gcc的路径,还有要编译C程序,用gcc.exe。
4
5
如果编译C++程序,用g++.exe,例子如下:E:\mingw\bin\g++.exe -Wall 1_1a.cpp -o 1_1a.exe

使用Make来编译程序1_1b.c
首先,创建一个文件Makefile,内容如下:
1
1_1b:
2
E:\mingw\bin\gcc.exe -Wall 1_1b.c -o 1_1b.exe
3
1_1b.exe
注意:2、3行前面一定要使用Tab键,否则编译没法通过
然后输入命令执行:
1
make 1_1b
结果如下:
