【问题标题】:Cannot Compile My Program or Identify the Issue Here无法编译我的程序或在此处识别问题
【发布时间】:2021-05-11 07:22:32
【问题描述】:

我可以认为这些“全局”数组在某些方面存在问题, 但我就是不知道到底是什么问题。

汽车.h:

#ifendf ...
#define ...

#define N 10000

typedef struct car { <some code here> } Car;

extern Car carList[N];

<function declarations here>

#endif

汽车.c:

#include "car.h"

<function definitions here>

client.h:

#ifendf ...
#define ...

#define M 8000

typedef struct client { <some code here> } Client;

extern Client clientList[M];

<function declarations here>

#endif

client.c:

#include "car.h"
#include "client.h"

<function definitions here>

供应商.h:

#ifendf ...
#define ...

#define K 50

typedef struct supplier { <some code here> } Supplier;

extern Supplier supplierList[K];

<function declarations here>

#endif

供应商.c:

#include "supplier.h"

<function definitions here>

编译器消息:(带有 GCC 的 CLion IDE)

...
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles/untitled1.dir/main.c.o:main.c:(.rdata$.refptr.supplierList[.refptr.supplierList]+0x0): undefined reference to `supplierList'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles/untitled1.dir/main.c.o:main.c:(.rdata$.refptr.clientList[.refptr.clientList]+0x0): undefined reference to `clientList'
/usr/lib/gcc/x86_64-pc-cygwin/10/../../../../x86_64-pc-cygwin/bin/ld: CMakeFiles/untitled1.dir/main.c.o:main.c:(.rdata$.refptr.carList[.refptr.carList]+0x0): undefined reference to `carList'
collect2: error: ld returned 1 exit status
...

【问题讨论】:

  • 每个全局数组都需要在一个 .c 文件中定义。
  • 我试图在您的代码中搜索 supplierList 的出现,但我无法在图像中搜索文本。如果代码,请不要图像。
  • Hellmar- 我该如何定义这些数组?它们是空的,我不允许使用 malloc

标签: c gcc cmake clion


【解决方案1】:

对`carList'的未定义引用

错误表明链接器没有找到任何带有 carList(和其他)的目标文件。 您使用 extern 关键字定义了 carList,这告诉编译器实际数据在另一个文件中,但您没有提供任何数据。

要定义这些数组,您需要在 [only] 一个源文件中使用不带 extern 关键字的行。

【讨论】:

  • "要定义这些数组,你需要一行 with extern 关键字" - 你的意思可能是“... 没有 @ 987654324@关键字:该关键字总是表示声明,而不是定义。
猜你喜欢
  • 2017-09-29
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-29
  • 2022-01-09
  • 2019-12-21
  • 1970-01-01
相关资源
最近更新 更多