我可以在C标头上使用extern函数声明,该标头也用于包含函数定义的C源文件吗?

问题描述 投票:0回答:1

我在C89中具有以下源代码:

routine_a.c

struct DataRoutineA routineA(int a, int b) {
  struct DataRoutineA data = (struct DataRoutineA *) malloc(sizeof(DataRoutineA));
  data.a = a;
  data.b = b;
  return data;
}

和以下头文件:

routine_a.h

struct DataRoutineA {
  int a;
  int b;
};

extern struct DataRoutineA routineA(int a, int b);

routine_a.h的目的是可以将其用作其他源代码文件的头。因此,结构被定义,外部函数也被定义。在这种情况下,我的理解是标题已正确定义。

但是,如果此标头也用于routine_a.c,则extern子句会如何?哪种方法可以解决ANSI C / C89问题?在这种情况下,我需要两个不同的标题吗?

c header-files c89
1个回答
2
投票

默认情况下,C中的所有函数都是extern。因此,

之间没有区别
extern struct DataRoutineA routineA(int a, int b);

struct DataRoutineA routineA(int a, int b);

[完全声明函数原型时,您确实需要extern关键字。

© www.soinside.com 2019 - 2024. All rights reserved.