C中外部函数实例
内部函数:static声明,只对本文件域生效,外部不可引用
外部函数,使用extern声明,默认可以去掉,在其他文件中引用时先用extern声明,可以去掉
在一个文件中定义时使用extern声明
#include <stdio.h>
void main()
{
extern char* myfunction();
}
编译
gcc testfun.c test.c -o test
gcc中使用编译自定义头文件
自定义文件 //foo.c
char* foo()
{
return "i am foo function!!\n";
}
头文件 foo.h
char* foo();
//main.c
#include <stdio.h> //引用库函数,系统环境目录下使用<>
#include "foo.h" //引用自定义函数, 当前目录下使用双引号
void main()
{
// extern char* get1();
printf("main function!\n");
char *str = foo();
printf("%s\n",str);
}
编译:
gcc main.c -o main -include foo.h foo.c