【问题标题】:Struct declared twice?结构声明两次?
【发布时间】:2015-11-14 22:51:35
【问题描述】:

当我尝试使用 Tiny C 编译器编译源文件时,我收到以下错误:

arch/x86/include/asm/ptrace.h:38: error: struct/union/enum already defined

这是头文件 ptrace.h 中的第 9-68 行,我的源文件间接包括:

#ifdef __i386__

struct pt_regs {
    unsigned long bx;
    unsigned long cx;
    unsigned long dx;
    unsigned long si;
    unsigned long di;
    unsigned long bp;
    unsigned long ax;
    unsigned long ds;
    unsigned long es;
    unsigned long fs;
    unsigned long gs;
    unsigned long orig_ax;
    unsigned long ip;
    unsigned long cs;
    unsigned long flags;
    unsigned long sp;
    unsigned long ss;
};

#else /* __i386__ */

struct pt_regs {
/*
 * C ABI says these regs are callee-preserved. They aren't saved on kernel entry
 * unless syscall needs a complete, fully filled "struct pt_regs".
 */
    unsigned long r15;
    unsigned long r14;
    unsigned long r13;
unsigned long r12;
    unsigned long bp;
    unsigned long bx;
/* These regs are callee-clobbered. Always saved on kernel entry. */
    unsigned long r11;
    unsigned long r10;
    unsigned long r9;
    unsigned long r8;
    unsigned long ax;
unsigned long cx;
    unsigned long dx;
    unsigned long si;
    unsigned long di;
/*
 * On syscall entry, this is syscall#. On CPU exception, this is error code.
 * On hw interrupt, it's IRQ number:
 */
    unsigned long orig_ax;
/* Return frame for iretq */
    unsigned long ip;
    unsigned long cs;
    unsigned long flags;
    unsigned long sp;
    unsigned long ss;
/* top of stack page */
};

#endif /* !__i386__ */

间接包含是指我的源文件包含一个包含此头文件的不同头文件。 该结构未声明两次。出了什么问题?

【问题讨论】:

  • 我的猜测是您还包含了不同的系统头文件,可能也间接包含相同的内容。尝试使用-E 选项并在输出中搜索结构定义;您应该能够使用-E 输出中的# 行来跟踪包含。

标签: c include c-preprocessor


【解决方案1】:

在黑暗中拍摄,但尝试对结构进行单个声明,但在预编译指令中只包含 field 声明。

【讨论】:

    【解决方案2】:

    我必须放入一个 -nostdinc 标志。结构 pt_regs 是在标准包含文件中定义的。

    【讨论】: