c - 隐式函数声明

标签 c linker compiler-warnings ld

我刚刚使用 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 function main':<br/> test.c:(.text+0xfc): undefined reference toTesting'

我需要做什么?

最佳答案

编辑:为了反射(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/

相关文章:

c - 可以用相同的数字进行可能的组合

c - 如何以编程方式从 Web 下载文件?

c++ - 头文件如何安全地包含标准库供消费者导入它们

c++ - 聚合的默认初始化

c - 如何利用对 updateConfigParams() 函数的缓冲区溢出攻击来禁用该程序?

c++ - 在 C/C++ 中检查 NULL 指针

c++ - 如何在 C/C++ 中使用带有 OpenSSL 的静态链接

编译器找不到所需的 SSL 库 - undefined reference

c# - 如果有人使用外部类库中的某些类,我如何报告编译错误

c++ - 在特定变量上禁用 GCC "may be used uninitialized"