我正在try 以这种方式编写一个C头文件:
// header.h
#ifndef HEADER_H
#define HEADER_H
extern int add(int a, int b);
#endif
// header.c
#include "header.h"
int add(int a, int b)
{
return (a+b);
}
现在假设我有一个main.c文件,如下所示:
// main.c
#include <stdio.h>
#include "header.h"
int main(void)
{
print("%d", add(2,3));
return 0;
}
编译main.c时出错,GCC说"未定义对‘Add’的引用".你能解释一下我哪里出错了吗?
EDIT:
I think I didn't make myself very clear previously. What I am actually trying to say is that, I've seen that most C programmers follow the above mentioned style of writing the header files, and I've also heard that this reduces the size of the final binary that's generated. I'm trying to do something similar but I get an error every time I try to compile my code. I added that extern
keyword so that the compiler understands that the function is defined somewhere externally, but it still failed to compile.
如何在实现上述编程风格的情况下成功编译上述代码?