har*_*ari 5 c declaration function header-files
/me/home/file1.c containes function definition:
int mine(int i)
{
/* some stupidity by me */
}
Run Code Online (Sandbox Code Playgroud)
我已经宣布了这个功能
/me/home/file1.h
int mine(int);
Run Code Online (Sandbox Code Playgroud)
如果我想使用此功能mine()在/me/home/at/file2.c
为此,我需要做的就是:
file2.c
#include "../file1.h"
Run Code Online (Sandbox Code Playgroud)
够了吗?可能不是.
做完这么多,当我编译时file2.c,我得到了undefined reference to 'mine'
您还需要链接file1中的目标文件.例:
gcc -c file2.c
gcc -c ../file1.c
gcc -o program file2.o file1.o
Run Code Online (Sandbox Code Playgroud)
或者您也可以同时提供所有文件,让GCC完成工作(不建议在少数几个文件之外);
gcc -o program file1.c file2.c
Run Code Online (Sandbox Code Playgroud)