实验平台
1. MinGW
2. notepad...
步骤
- 下载安装mingw,并配置环境变量
- ..\MinGW\bin
- ..\MinGW\include
- ..\MinGW\lib
- (上面那些其实都是根据感觉走的,应该是要用到的吧…)
编写程序
#include<stdio.h> int main() { int a = 1; printf("hello world\n"); return 0; }
生成dll文件
命令行将目录改为程序文件夹目录
CD C:\test
开始生成dll文件
gcc -shared -o test.dll test.c
test.c是要编译的C程序
简单gcc命令行
无选项编译链接
gcc test.c
将test.c预处理、编译、链接,并输出一个a.exe的可执行文件
-o选项
gcc test.c -o test.exe
将test.c预处理、编译、链接,并输出一个test.exe的可执行文件
-E选项
gcc -E test.c -o test.i
将test.c预处理输出test.i文件(输出的文件名和后缀均可以任意指定虽然输出的文件可以用记事本打开,但是其他的后缀的文件无法链接=。=)
-S选项
gcc -S test.i
将预处理输出文件test.i汇编成test.s文件
-c选项
gcc -c test.s
将汇编输出文件test.s编译输出test.o文件
(我把后面的test.s改为了test.o,也可以生成相同的test.o文件)-O选项
gcc -O1 test.c -o test
使用编译优化级别1编译程序。级别为1~3,级别越大优化效果越好,但编译时间越长;输入./test执行程序
运行程序
./test
多文件编译
同时编译、链接、生成可执行文件
test.c代码如下
`//test.c #include<stdio.h> extern int flag; int main() { int a = 1; printf("hello world\n"); printf("%d\n",flag); return 0; }`
test2.c代码如下
#include<stdio.h> int flag = 200;//为了观察是否成功调用
命令行编译如下
gcc -test.c test2.c -o test
得到名为test.exe的可执行文件
* 分别编译、链接
gcc -c test2.c
gcc -c test.c
gcc test.o test.o -o test
* 执行.\test
查看可执行文件输出
* 以上两种方法相比较,第一中方法编译时需要所有文件重新编译,而第二种方法可以只重新编译修改的文件,未修改的文件不用重新编译