所以..我遇到了一些先有鸡还是先有蛋的问题。
我得到了 Controller_setup.h 和 Equipment_setup.h,它们使用另一个中定义的 typedef。我可以在这里写很多关于证明这是为什么的,但我不会这样做,除非有人真的真的想知道(尽管基本上它是试图让 Equipment_setup.c/h 中的代码与不同的 Controller 系统一起工作,只需让一个不同的 Controller_setup.h)。
在之前的 Controller 和编译器( Controller 使用 ARM 处理器,我们使用 IAR 编译器和 IDE)上,我们开发了代码,我能够转发声明/重新定义 typedef,它可以工作。它会给我们很多警告,但它会编译并工作。
现在我们实际上正在尝试使用另一个 IDE 和编译器移植到另一个 Controller ( Controller 使用 TriCore 处理器,我们使用 Code::blocks IDE 和用于 TriCore V3.4.6 的 GNU GCC 编译器)它现在引发错误并且不会让代码编译。
我对此很陌生,但据我了解,我应该能够随时声明此数据类型已在某处定义,这样当我在函数声明中引用该数据类型时,编译器就不会担心。
因为它在一个编译器上工作(诚然带有警告)我希望现在有一些方法可以让它工作。我不认为它像使用不同的语法一样简单,是吗?是否可以声明 typedef 与重新定义它?
实际上,我只是用谷歌搜索了那个确切的问题,通过跟踪我现在似乎明白我想做的事情在 C11 下是允许的,但在以前的 C 标准下是不允许的。据我所知,Tricore 编译器不支持 C11,所以这可能是它过去可以工作但现在不行的原因。
但如果有人可以帮助验证这种理解,我们将不胜感激。
我真的不想寻找变通办法,因为我担心这可能意味着对程序的结构进行相当大的返工。从长远来看,这可能会更好,但现在不是必须这样做的好时机。
以下是曾经有效但现在无效的简化版本。我省略了 Controller_setup.h 引用 Equipment_setup.h 的部分。构建项目时,首先读取 Equipment_setup.h,因此在读取 Controller_setup.h 中的重新定义时出现错误。
错误信息是:
- error: [12993] redefinition of typedef 'IO_link_t'
- error: [13256] previous declaration of 'IO_link_t' was here
Equipment_setup.h
//The functions declared here are used by other .c files which
//determine how the equipment is to operate
typedef enum IO_devices_t IO_devices_t;
typedef struct IO_link_t IO_link_t;
//Declare function for checking status of a given IO device
uint8_t check_IO_device_status(IO_device_t device);
//Declare function for calibrating an input
uint8_t calibrate_input(IO_link_t* input);
Controller_setup.h
typedef enum IO_devices_t{
MAIN_CONTROLLER,
REMOTE_IO_1,
REMOTE_IO_2,
//... will vary based on controllers being used...
HMI_1,
NUMBER_IO_DEVICES
} IO_devices_t;
typedef struct IO_link_t {
IO_msg_ptr_t IO_msg_ptr;
uint8_t IO_msg_size;
IO_logic_ptr_t IO_logic_ptr;
//... other members as required by Equipment_setup.h ...
//... members that vary based on the controller's SDK ...
} IO_link_t;
最佳答案
当你这样做时:
typedef struct IO_link_t IO_link_t;
typedef
定义一个类型。您还声明 struct IO_link_t
。当您执行此操作时:
typedef struct IO_link_t {
IO_msg_ptr_t IO_msg_ptr;
uint8_t IO_msg_size;
IO_logic_ptr_t IO_logic_ptr;
//... other members as required by Equipment_setup.h ...
//... members that vary based on the controller's SDK ...
} IO_link_t;
您定义 struct IO_link_t
之前未定义,但您还重新定义 IO_link_t
类型。这就是错误的来源。 enum
也是如此。
您可以通过在定义 struct
和 enum
时删除 typedef
来解决此问题。
enum IO_devices_t{
MAIN_CONTROLLER,
REMOTE_IO_1,
REMOTE_IO_2,
//... will vary based on controllers being used...
HMI_1,
NUMBER_IO_DEVICES
};
struct IO_link_t {
IO_msg_ptr_t IO_msg_ptr;
uint8_t IO_msg_size;
IO_logic_ptr_t IO_logic_ptr;
//... other members as required by Equipment_setup.h ...
//... members that vary based on the controller's SDK ...
};
关于C: 前向声明一个 typedef,稍后定义用于现在声明一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55564545/