多文件编程

//添加一个源文件  main1.c

#include<stdio.h>

//"":导入自己的头文件

#include"fun2.h"

int main()

{

//gcc-o hello.exe main1.c fun2功能实现.c fun2.h head.h

//没有什么先后顺序,编译器会自动找到主函数,再进行文件的整合

  int a=10;

  int b=20;

  printf("%d\n",max(a,b));

  return 0;

}

 

//添加一个源文件  fun2功能实现.c

#include"fun2.h"

//函数定义

int max(int a,int b)

{

  return a>b?a:b;

}

 

//添加一个头文件  fun2.h

//为避免同一个文件被include多次,C/C++中有两种方试:一种是#ifndef;一种是#pragma once

/*格式:

#ifndef __SOMEFILE_H__
#define __SOMEFILE_H__
 // 声明语句
 #endif

*/

//#pragma once//防止头文件重复包含,能够保证头文件只被编译一次。

 

#ifndef __FUN2_H__

#define __FUN2_H__

#include"head.h"

//全局变量的定义

//函数的声明 

extern int max(int a,int b);

#endif // !1

 

//添加一个头文件  head.h

//#pragma once

#include"fun2.h"

 

//包含头文件的错误 

 

posted @ 2020-08-20 10:38  wh19991213  阅读(147)  评论(0编辑  收藏  举报