以下,将介绍如何搭建工程。
1.文件分析,在Linux开发中,工程所建的文件类型主要有
.h 头文件
.c 源文件
.so 动态库
.a 静态库
2.文件放置(由于目前的开发还没用到库,为了防止误导,省略涉及.so以及.a文件。)
inc/
.h 文件
src/
.c 文件
3.各文件内容编写
main.c 如果工程规模不大(#include时小于2个自定义头文件),则可以不新建main.h,直接包含外部头文件即可。
即预处理 + 外部函数声明 + 主函数(包括错误处理)
#include <stdio.h> //常用头文件
#include <head1.h>
#include <head2.h>
int main((int argc, const char *argv[])
{
function_from_head1();
...
return 0;
err:perror("The description about the reason caused the fault.");
return -1;
}
main.h 如果工程规模较大(#include时大于等于2个自定义头文件),则将main.c的预处理语句放置到main.h中,并在main.c中包含外部头文件
即预处理语句(main.h)
#ifndef _MAIN_H_
#define _MAIN_H_
#include <stdio.h>
#inlucde <head1.h>
...
#inlcude <headn.h>
...
#endif
包含头文件(main.c)
#include <main.h>
...
head.h
条件宏定义,函数声明语句,所引用的头文件
即
#ifndef _HEAD1_H_
#define _HEAD1_H_
typedef XXX(变量类型) returntpye;
returntype function1_from_head1();
...
returntype functionn_from_head1();
head.c
函数主体,函数注释,函数外部接口等,预处理语句
4.编译输出
一般大工程我们使用makefile(暂时略),但是小工程可以直接使用shell命令
即arm-linux-gcc *.c -o #OutPutName -I #HeadFilePathName