我刚刚使用 header 组织了我的代码,但正如我所做的那样,我收到一条警告,该警告在链接时变成了错误。
我在test.c
中有一个代码(使用 header 内的函数)就像这样:
#include "test1.h"
/* Some code */
main()
{
Testing();
}
还有我的test1.h
标题是这样的:
void Testing();
void print(int, int, int, const char*);
然后在 test1.c
void Testing()
{
print(0xF9, 27, 5, "\xC9\\xBB");
}
void print(int colour, int x, int y, const char *string)
{
volatile char *video=(volatile char*)0xB8000 + y*160 + x*2;
while(*string != 0)
{
*video=*string;
string++;
video++;
*video=colour;
video++;
}
}
当我尝试编译代码时,我得到了这个:
ubuntu@eeepc:~/Development/Test$ gcc -o test.o -c test.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs
test.c: In function ‘main’:
test.c:11: warning: implicit declaration of function ‘Testing’
ubuntu@eeepc:~/Development/Test$
当时这只是一个简单的警告,但是当我尝试链接它时......
ubuntu@eeepc:~/Development/Test$ ld -T linker.ld -o kernel.bin loader.o test.o
test.o: In functionmain':<br/> test.c:(.text+0xfc): undefined reference to
Testing'
我需要做什么?
最佳答案
编辑:为了反射(reflect)OP的问题,尽管有人投票,我还是删除了答案中的一些内容......
为什么 kernel.c 在编译器中被标记,即使这里没有提到它?我错过了什么吗...
gcc -o test.o -c test.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs kernel.c: In function ‘main’: kernel.c:11: warning: implicit declaration of function ‘Testing’ ubuntu@eeepc:~/Development/Test$罢工>
<罢工>罢工>
也许你需要在头文件中的某个地方这样做,因为我判断你希望内核访问这个函数:
extern void Testing();
并且,取出所有函数并将它们放在单独的 .c 文件中,它们一开始就不应该在那里......例如:
Testing.c /* all your functions here */ Testing.h /* Only global, external variables and function prototypes */
希望这有帮助, 此致, 汤姆。
关于c - 隐式函数声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2144076/