使用 extern 关键字调用函数

skm*_*ey1 3 c extern storage-class-specifier

我想从 other.c 调用 test.c 中定义的函数。

我可以extern打电话function1吗?另外,我是否必须使用正在调用的externinfunction2和?function3function1

其他.c

extern function1();
function1();
Run Code Online (Sandbox Code Playgroud)

测试.c

void function1()
{
    function2();
    function3();
}

void function2()
{

}

void function3()
{

}
Run Code Online (Sandbox Code Playgroud)

P__*_*J__ 5

实际上,默认情况下每个函数都是 extern :) - 除非您声明它们不是 :)。如果你在第一次调用之前有原型就足够了;

int xxx(int, int, float, double);  // prototype


int main(void)
{
  int x,y;
  float z;
  double v;

  xxx(x,y,z,v);

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

函数可以位于另一个 .c 文件中。您需要将目标文件包含到链接中。

int xxx(int a, int b, float c, double d)
{
  int result;
  /* do something */

  return result;
}
Run Code Online (Sandbox Code Playgroud)