C语言预处理之多文件编译
mkdir mutil_files
//多文件
cd mkdir mutil_files
touch main.c
//主函数文件
mkdir header
//存放自定义的头文件
mkdir source
//存放源文件
mkdir output
//输出的可执行文件
cd header
code test,h
#ifndef _TEST_H_
#define _TEST_H_
#program once
不管这个头文件被包含了多少次,只进行一次编译解析
#include<stdio,h>
void test_print_message();
//定义一个函数原型
# endif
code max.h
#ifndef _MAX_H_
#define _MAX_H_
int max_int_two (int a,int b);
#endif
cd ../source
code test.c
#include<stdio,h>
void test_print_message(){
//定义一个函数原型
printf("这是一段测试的代码");
}
code max.c
int max_int_two (int a,int b){
return a>b?a:b;
}
cd ..
code main.c
#include"header/test,h"
#include"header/max.h"
//链接过程中需要链接多个文件,o
函数虽然定义了但找不到其实现,故而错误
每一个源文件都会生成一个目标文件
int main(){
//不管编译的文件有多少个,一个c程序中,main函数只能出现一个
void test_print_message();
return 0;
}
gcc main.c source/test.c source/max.c -o output /result
//编译
output /result
输出
创建脚本
code build.sh
gcc main.c source/test.c source/max.c -o output /result