1、在其他文件中定义要调用的函数
main.c文件
#include<stdio.h>
#include "weak.h"
char
__attribute__((weak))Fun_Sum(char a, char b)
{
return a + b;
}
int
main(int argc, char *argv[])
{
char sum1 = 0;
sum1 = Fun_Sum(20, 1);
printf("%d \r\n",sum1);
return 0;
}
/***程序输出结果:19 ***/
/**************end of file**********/
weak.c文件
#include "weak.h"
char
Fun_Sum(char a, char b)
{
return a - b;
}
/*******end of file***/
weak.h文件
#ifndef __WEAK_H_
#define __WEAK_H_
char Fun_Sum(char a, char b);
#endif // __WEAK_H_
/******** end of file **********/
2、在其他文件中不定义要调用的函数
main.c文件
#include<stdio.h>
#include "weak.h"
char
__attribute__((weak))Fun_Sum(char a, char b)
{
return a + b;
}
int
main(int argc, char *argv[])
{
char sum1 = 0;
sum1 = Fun_Sum(20, 1);
printf("%d \r\n",sum1);
return 0;
}
/****程序输出结果: 21 ********/
/**************end of file**********/
weak.c文件
#include "weak.h"
#if 0
char
Fun_Sum(char a, char b)
{
return a - b;
}
#endif // 0
/*******end of file***/
weak.h文件
#ifndef __WEAK_H_
#define __WEAK_H_
//char Fun_Sum(char a, char b);
#endif // __WEAK_H_
/******** end of file **********/