C - 为什么我可以在不包含源文件的情况下使用另一个源文件中的函数?

C - Why can I use a function from another source file without including the source file?

早上好,

我有以下内容:

//Simulator.h
#ifndef SIMULATOR_H
#define SIMULATOR_H

inline void functionn1(void);
#endif

然后我有它的实现:

//Simulator.c
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"

inline void functionn1(void){printf("Functionn 1\n");}

最后是主程序:

//main.c
#include <stdio.h>
#include <stdlib.h>
#include "simulator.h"

int main(int argc, char** argv) 
{
functionn1();
return (EXIT_SUCCESS);
}

它按预期工作,但在试用它之后我注意到:

  1. 当我注释掉 Simulator.h
  2. 中的函数原型时它也有效
  3. 此外,当我注释掉函数原型并故意不在 main.c 中包含 Simulator.h 时,该函数仍然有效。

为什么我做了上面两步之后就可以了?这里发生了什么样的恶作剧?

由于历史原因,C 也可以在没有任何函数原型的情况下工作。缺点是不会有参数校验。

只要有导出函数的目标文件,链接器就会知道如何将各个部分组合在一起。

当 C 解析器遇到一个函数用法并且它对这个函数一无所知时,它会生成一个虚拟签名并继续。

链接器将尝试找到合适的符号(即在传递给链接器的任何代码对象/库中找到带有函数名称的符号),这似乎出现在您的案例中。

你通常会收到警告,因为它会导致几个错误:自动生成的函数被假定为 return 一个 int。如果指针实际上是 returned 并且 sizeof(int) != sizeof(intptr_t) 你会得到一个微妙的错误,因为地址将被...... t运行cated.

例如下面的编译

int main(int argc, char**argv) {
  return foo();
}

(如果 foo 未在另一个编译单元中定义,则可能 运行 进入链接器错误),但以下 not 编译:

struct test {};
// missing declaration:
// struct test foo();
int main(int argc, char**argv) {
  // foo is not available, it defaults to returning an int
  struct test t = foo();
  return 0;
}

因为从隐式 return 类型 intstruct test 的转换无效。