1.熟悉Linux下C语言程序设计的基本步骤
2.掌握gcc编译器的各种参数的使用方法
3.掌握gcc编译器创建函数库的方法
4.掌握gdb调试程序的方法
5.掌握多文件编译中的makefile的用法
1.CPU:P4 1.6GHz 内存:1G
2.Windows7操作平台,Vmware虚拟机
1.Linux编辑器vi的使用
2. GCC编译器
3. GDB调试器
4. Make工程管理器
#include
int main(void)
{
double counter;
double result;
double temp;
for (counter = 0; counter < 4000.0 * 4000.0 * 4000.0 / 20.0 + 2030;counter += (5 - 3 +2 + 1 ) / 4)
{
temp=counter/1239;
result=counter;
}
printf("运算结果是:%lf\n", result);
}
gcc -E test.c -o test.i
gcc -S test.i -o test.s
gcc -c test.s -o test.o
gcc -O0 -o m0 test.c
gcc -O1 -o m1 test.c
gcc -O2 -o m2 test.c
gcc -O3 -o m3 test.c
ls
time ./m0
time ./m1
time ./m2
time ./m3
hello.h
void hello(const char *name);
hello.c
#include
void hello(const char *name)
{ printf(“hello %s”,name);
}
main.c
#include“hello.h”
int main()
{ hello(“everyone”);
return 0;
}
cat >>hello.h
cat >>hello.c
cat >>main.c
gcc -c hello.c -o hello.o
好家伙,报错
两个引号的问题
ls
ar crv libmyhello.a hello.o
注:静态库的命名规范是以lib为前缀,紧跟静态库名,扩展名为.a
gcc -o hello main.c -L. -lmyhello
./hello
rm libmyhello.a
./hello
gcc –shared -fPIC -o libmyhello.so hello.o
gcc -fPIC -shared hello.c -o libmyhello.so
用自己的命令来
gcc -o hello main.c -L. -lmyhello
若出错,执行mv libmyhello.so /usr/lib
(main.c,average.c,average.h),并编写makefile文件;用make编译后改成返回最小值再编译。
//main.c
#include "average.h"
main()
{ float a,b,ave;
float average(float a,float b);
ave=average(a,b);
}
//average.c
#include
float average(float a,float b)
{ float ave;
scanf("%f,%f",&a,&b);
ave=(a+b)/2;
printf("average is %f\n",ave);
return(ave);
}
//average.h
float average(float a,float b);
Makefile
#It is an example for describing makefile
edit:main.o average.o
gcc -o edit main.o average.o
main.o: main.c average.h
gcc -c main.c
average.o:average.c average.h
gcc -c average.c
cat >>main.c
cat >>average.c
cat >>average.h
cat >>Makefile
这里的average.h里面要记得删除makefile部分
gcc average.c main.c -o test
./test
make
./edit
五、实验体会
实验比较简单,gcc命令需要加强练习。