隐式函数声明

发布于 2024-08-19 03:50:04 字数 1154 浏览 7 评论 0原文

我刚刚使用标头组织了我的代码,但正如我所做的那样,我收到一条警告,该警告在链接时变成了错误。

我在 test.c 中有一个代码(使用标头内的函数),如下所示:

#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:~/开发/测试$ gcc -o test.o -c test.c -Wall -Wextra -nostdlib -nostartfiles -nodefaultlibs
test.c:在函数“main”中:
test.c:11:警告:函数“测试”的隐式声明
ubuntu@eeepc:~/开发/测试$

当时这只是一个简单的警告,但是当我尝试链接它时......

ubuntu@eeepc:~/开发/测试$ ld -T linker.ld -o kernel.bin loader.o test.o
test.o:在函数 main':
中 test.c:(.text+0xfc):未定义的引用
测试'

我需要做什么?

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.c that is like this:

#include "test1.h"

/* Some code */
main()
{
   Testing();
}

And my test1.h header is like this:

void Testing();
void print(int, int, int, const char*);

And at 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$

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 function main':
test.c:(.text+0xfc): undefined reference to
Testing'

What I need to do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

纸短情长 2024-08-26 03:50:04

编辑:为了反映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 */

希望这有帮助,
此致,
汤姆.

Edit: To reflect the OP's question I have struck out some lines of my answer despite being upvoted...

Why is kernel.c flagged up in the compiler, even though you don't have it mentioned here? Am I missing something...

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:

Testing.c
/* all your functions here */

Testing.h
/* Only global, external variables and function prototypes */

Hope this helps,
Best regards,
Tom.

愁以何悠 2024-08-26 03:50:04

我无法重现你的问题。当我尝试在 Ubuntu 机器上编译您的代码时,这按预期工作(根据您的粘贴,我假设您正在使用。)

您确定 #include 正确发生吗?

尝试使用 -E 而不是 -c 来查看编译器尝试编译的整个代码是什么样的。

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.)

Are you sure the #include is happening correctly?

Try using -E instead of -c to see what the whole code the compiler is trying to compile looks like.

野心澎湃 2024-08-26 03:50:04

这里有点盲目,因为我的 C 有点生疏,但是 C 允许你将函数体放在标头中吗?我不记得是这样的。尝试将 Test() 和 print() 的定义移到 .c 文件中?如果您不需要/想要 C,您也可以尝试编译为 C++,看看是否可以解决问题。

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.

踏月而来 2024-08-26 03:50:04

您将 test.h 包含到 main.c 中,而根据您编写的内容,您的声明位于 test1.h 中。请注意名称中的 1

除此之外,您正在编译 test.c 并链接 test.o,而实际上您的文件名称是 test1.c 。再次注意名称中的 1

编辑:现在您编辑了 main.c 中包含的文件的名称。编辑后,可以肯定地断言,您所描述的大多数症状在当前版本的文件中不可能。重新验证您正在做什么,发布更新的诊断信息和/或发布真实的代码。

尽管如此,编译器和链接器行引用的是旧文件名。

You included test.h into main.c, while your declarations, according to what your wrote, are in test1.h. Note the 1 in the name.

In addition to that, you are compiling test.c and linking test.o, while in reality the name of your file is test1.c. Again, note the 1 in the name.

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 possble with the current versions of the files. Re-verify what you are doing, post updated disgnostic information and/or post real code.

Still, you compiler and linker lines are referring to old file names.

帝王念 2024-08-26 03:50:04

我不知道是什么原因造成的,但我刚才遇到了这个问题。
尝试删除 .h 文件并将函数声明放在 .c 文件本身的顶部。

在这种情况下,删除test1.h并将test1.c的函数声明放在test1.c中。
并在 test.c 中包含 test1.c
您将不会再收到该警告消息,也不会收到以下链接错误。

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 .

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 .

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文