1.编写一个主程序,在主程序中调用某个子程序
thank.c#include <stdio.h>int main(void){ PRintf("Hello World/n"); thank2();}thank2.c#include <stdio.h>void thank2(void){ printf("Thank you/n");}如何将两个程序连接到一起,在linux的终端中执行命令
gcc -c thank.c thank2.c //gcc -c xxx.c 会生成.o目标文件gcc -o thank thank.o thank2.o //gcc -o xxx.o会生成可执行二进制文件,若没有指定名字,默认为a.out./thank //执行ll thank* //查看thank的文件显示警告信息 gcc -Wall -c thank.c thank2.c 2.调用外部函数:加入链接的函数库
sin.c中#include <stdio.h>int main(void){ float value; value = sin(3.14/2); printf("%f/n",value);}若直接使用gcc -c sin.c会出现无法找到sin函数的错误(undefines reference to sin) 所以 必须要连接必要的库(C语言里面的sin函数在linm.so这个函数库中)
gcc -c sin.c -lm -L/lib -L/usr/lib总结gcc的用法
//仅编译生成链接文件gcc -c hello.c//直接编译生成可执行文建(不加参数./a.out)gcc hello.c//在编译的时候依据操作环境给予优化执行速度gcc -O hello.c -c//在制作二进制文件的时候,将链接的函数库与相关路径填入gcc sin.c -lm -L/usr/lib -I/usr/include//-lm是指libm.so或libm.a这个函数库文件//-L后面接的是路径是刚才上面那个函数库的搜索目录//-I后面接的是源码内的include文件所在目录//生成某个特定名字的可执行文件gcc -o hello hello.c//显示警告信息gcc -o hello hello.c -Wall2.Makefile的基本用法 一、基本规则: (1)makefile中#表示批注 (2)在命令行前一定要用 (3)目标与相关文件之间用:隔开
vi makefilemain:main.o hh.o sin.o cos.o gcc -o main main.o hh.o sin.o cos.oclean: rm -f main main.o hh.o sin.o cos.o想要先清除信息在建立main信息,可以make clean main 二、可以使用shell script简化脚本
LIBS = -lmOBJS = main.o hh.o sin.o cos.omain:${OBJS} gcc -o main ${OBJS} ${LIBS}clean: rm -f main ${OBJS}新闻热点
疑难解答