LINUX C中有关多个文件中宏的小问题

来源:百度知道 编辑:UC知道 时间:2024/06/21 12:29:06
假设我们有下面这样的一个程序,源代码如下:
/* main.c */
#include "mytool1.h"
#include "mytool2.h"
int main(int argc,char **argv)
{
mytool1_print("hello");
mytool2_print("hello");
}
/* mytool1.h */
#ifndef _MYTOOL_1_H
#define _MYTOOL_1_H
void mytool1_print(char *print_str);
#endif
/* mytool1.c */
#include "mytool1.h"
void mytool1_print(char *print_str)
{
printf("This is mytool1 print %s\n",print_str);
}
/* mytool2.h */
#ifndef _MYTOOL_2_H
#define _MYTOOL_2_H
void mytool2_print(char *print_str);
#endif
/* mytool2.c */
#include "mytool2.h"
void mytool2_print(char *print_str)
{
printf("This is mytool2 print %s\n",print_str);
}
当然由于这个程序是很短的我们可以这样来编译
gcc -c main.c
gcc -c mytool1.c
gcc -c mytool2.c
gcc -o main main.o mytool1.o mytool2.o

我要问的是

void mytool2_print(char *print_str); 这是函数原型

#ifndef _MYTOOL_2_H
#define _MYTOOL_2_H

#endif
这是防止重复include, 实际上已经是个写头文件的“标准技巧”了。。

是防止重复编译。
因为如果一个工程很大,会有很多文件有以下语句:
#include <mytool2.h>
如果多次编译纯属浪费.