C语言 函数的隐式声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2144076/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Implicit Declaration Of Function
提问by Nathan Campos
I've just organized my code by using headers, but just as I've done this, I got a warning that turned into an error when linking.
我刚刚使用标头组织了我的代码,但正如我所做的那样,我收到一条警告,在链接时变成了错误。
I have a code(use of a function that is inside a header) in test.cthat is like this:
我有一个代码(使用标题内的函数)test.c,如下所示:
#include "test1.h"
/* Some code */
main()
{
Testing();
}
And my test1.hheader is like this:
我的test1.h标题是这样的:
void Testing();
void print(int, int, int, const char*);
And at test1.c
而在 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++;
}
}
When I try to compile the code, I got this:
当我尝试编译代码时,我得到了这个:
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$ gcc -o test.o -c test.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs
test.c:在函数'main'中:
test.c:11:警告:隐式函数 'Testing' 的声明
ubuntu@eeepc:~/Development/Test$
At the time it's just a simple warning, but when I try to link it...
当时这只是一个简单的警告,但是当我尝试链接它时......
ubuntu@eeepc:~/Development/Test$ ld -T linker.ld -o kernel.bin loader.o test.o
test.o: In functionmain':Testing'
test.c:(.text+0xfc): undefined reference to
ubuntu@eeepc:~/Development/Test$ ld -T linker.ld -o kernel.bin loader.o test.o
test.o: 在功能测试中main':
test.c:(.text+0xfc): undefined reference to
What I need to do?
我需要做什么?
回答by t0mm13b
Edit:To reflect the OP's question I have struck out some lines of my answer despite being upvoted...
编辑:为了反映 OP 的问题,尽管被点赞了,我还是删掉了我的答案的一些内容......
Why is kernel.c flagged up in the compiler, even though you don't have it mentioned here? Am I missing something...
为什么 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$
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$
maybe you need to do it this way somewhere in your header file as I'm judging you want kernel to access this function:
也许您需要在头文件中的某处以这种方式执行此操作,因为我判断您希望内核访问此函数:
extern void Testing();
And, take out all your functions and place them in a separate .c file, they should not be in there in the first place... for example:
而且,取出所有函数并将它们放在一个单独的 .c 文件中,它们首先不应该放在那里......例如:
Testing.c /* all your functions here */ Testing.h /* Only global, external variables and function prototypes */
Hope this helps, Best regards, Tom.
希望这会有所帮助,最好的问候,汤姆。
回答by McPherrinM
I can't recreate your problem. This works as expected when I try to compile your code on an Ubuntu machine (Which based on your paste, I assume you're using.)
我无法重现你的问题。当我尝试在 Ubuntu 机器上编译您的代码时,这按预期工作(根据您的粘贴,我假设您正在使用。)
Are you sure the #include is happening correctly?
您确定 #include 正确发生吗?
Try using -E instead of -c to see what the whole code the compiler is trying to compile looks like.
尝试使用 -E 而不是 -c 来查看编译器试图编译的整个代码是什么样的。
回答by vijay
i donno whats causing this , but i had this problem just now . try to delete the .h file and put the declarations of the functions on the top of .c file itself .
我不知道是什么原因造成的,但我刚才遇到了这个问题。尝试删除 .h 文件并将函数的声明放在 .c 文件本身的顶部。
in this case , delete the test1.h and put the declarations of functions of test1.c in test1.c. and include test1.c in test.c you wont get that warning message any more , nor the following linking errors .
在这种情况下,删除 test1.h 并将 test1.c 的函数声明放在 test1.c 中。并将 test1.c 包含在 test.c 中,您将不会再收到该警告消息,也不会出现以下链接错误。
回答by AnT
You included test.hinto main.c, while your declarations, according to what your wrote, are in test1.h. Note the 1in the name.
您包含test.h到 中main.c,而根据您所写的内容,您的声明在test1.h. 请注意1名称中的 。
In addition to that, you are compiling test.cand linking test.o, while in reality the name of your file is test1.c. Again, note the 1in the name.
除此之外,您正在编译test.c和链接test.o,而实际上您的文件名称是test1.c. 再次注意1名称中的 。
Edit:Now you edited the name of the file included into main.c. After the edit it is safe to assert that most of the symptoms you describe are not possblewith the current versions of the files. Re-verify what you are doing, post updated disgnostic information and/or post real code.
编辑:现在您编辑了包含在main.c. 编辑后可以安全地断言您描述的大多数症状在当前版本的文件中都不可能。重新验证您在做什么,发布更新的诊断信息和/或发布真实代码。
Still, you compiler and linker lines are referring to old file names.
尽管如此,您的编译器和链接器行是指旧文件名。
回答by Toji
Somewhat of a shot in the dark here, since my C is a bit rusty, but does C allow you to put function bodies in a header? I don't recall that it does. Try moving the definition of Testing() and print() into a .c file? You could also try compiling as C++ as see if that fixes it, if you don't need/want C.
在这里有点摸不着头脑,因为我的 C 有点生疏,但是 C 是否允许您将函数体放在标题中?我不记得有。尝试将 Testing() 和 print() 的定义移动到 .c 文件中?如果您不需要/不想要 C,您也可以尝试编译为 C++,看看是否可以修复它。

