Skip to content

Commit 8b524a2

Browse files
authored
Create Windows安装GNU编译器使用makefile.md
1 parent e36a9f4 commit 8b524a2

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
### 下载安装MinGW
2+
MinGW下载网页:http://sourceforge.net/projects/mingw/files/latest/download?source=files
3+
下载后,运行程序:mingw-get-setup.exe,选择download latest repository catalogues. 选择编译器是勾选C Compiler 与C++ Compiler,点击next进行下载及安装。
4+
5+
### 设置环境变量
6+
右击计算机->属性->高级系统设置->环境变量,在系统变量中找到PATH,将MinGW安装目录里的bin文件夹的地址添加到PATH里面。
7+
打开MinGW的安装目录,打开bin文件夹,将mingw32-make.exe复制一份,重命名为make.exe。
8+
9+
### 测试GCC编译
10+
创建一下test.c,用记事本打开该文件,将以下内容复制到文件中。
11+
``` c
12+
#include<stdio.h>
13+
#include<stdlib.h>
14+
15+
int main(void){
16+
printf("Hello, world!\n");
17+
system("pause");
18+
return 0;
19+
}
20+
```
21+
22+
打开命令提示符,更改目录到test.c的位置,键入
23+
24+
gcc -o test.exe test.c
25+
26+
可生成test.exe可执行文件。
27+
28+
### 测试makefile
29+
新建文件夹,在文件夹内创建max_num.c、max.h、max.c、makefile四个文件。
30+
31+
max_num.c内容如下:
32+
33+
``` c
34+
#include <stdio.h>
35+
#include <stdlib.h>
36+
#include "max.h"
37+
38+
int main(void)
39+
{
40+
printf("The bigger one of 3 and 5 is %d\n", max(3, 5));
41+
system("pause");
42+
return 0;
43+
}
44+
```
45+
46+
max.h内容如下:
47+
48+
``` c
49+
int max(int a, int b);
50+
```
51+
52+
max.c内容如下:
53+
54+
``` c
55+
#include "max.h"
56+
57+
int max(int a, int b)
58+
{
59+
return a > b ? a : b;
60+
}
61+
```
62+
63+
makefile内容如下:
64+
65+
``` makefile
66+
max_num.exe: max_num.o max.o
67+
gcc -o max_num.exe max_num.o max.o
68+
69+
max_num.o: max_num.c max.h
70+
gcc -c max_num.c
71+
72+
max.o: max.c max.h
73+
gcc -c max.c
74+
```
75+
76+
77+
注意所有含有gcc的行前面是一个制表符,而非若干空格。否则可能会保存,无法编译。
78+
79+
打开命令提示符,更改目录到新建的文件夹,键入make,可生成指定的应运程序。
80+
81+
测试完成。
82+
83+
————————————————
84+
85+
版权声明:本文为CSDN博主「pdcxs007」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
86+
原文链接:https://blog.csdn.net/pdcxs007/article/details/8582559

0 commit comments

Comments
 (0)