【问题标题】:Syntax error porting C code from Linux to FreeBSD将 C 代码从 Linux 移植到 FreeBSD 时出现语法错误
【发布时间】:2010-07-15 09:35:10
【问题描述】:

这对我来说非常令人费解,因为代码在 Debian 5 系统上编译时没有错误,但在 FreeBSD 7 上,例如在第 98 行出现语法错误。

int ipmi_fru_get_board_info_mfg_time(ipmi_fru_t   *fru, time_t *time);

最初在 *fru 和 time_t 之间有一个换行符。不确定是什么导致了这些编译器错误,但提到换行符感觉很重要。

或者第 298 行中的这一行的格式完全没有改变。

int ipmi_fru_get(ipmi_fru_t                *fru,
     int                       index,
     char                      **name,
     int                       *num,
     enum ipmi_fru_data_type_e *dtype,
     int                       *intval,
     time_t                    *time,
     char                      **data,
     unsigned int              *data_len);

这些是输出到终端的未更改错误。

In file included from out_fru.c:37:
../include/OpenIPMI/ipmi_fru.h:98: error: expected declaration specifiers or '...' before 'time_t'
../include/OpenIPMI/ipmi_fru.h:298: error: expected declaration specifiers or '...' before 'time_t'
../include/OpenIPMI/ipmi_fru.h:474: error: expected declaration specifiers or '...' before 'time_t'
../include/OpenIPMI/ipmi_fru.h:559: error: expected declaration specifiers or '...' before 'time_t'
../include/OpenIPMI/ipmi_fru.h:627: error: expected declaration specifiers or '...' before 'time_t'

随后的错误似乎是相关的,因为它们影响在 ipmi_fru.h 头文件的上述行中声明的函数。

out_fru.c: In function 'ipmi_cmdlang_dump_fru_info':
out_fru.c:87: warning: passing argument 7 of 'ipmi_fru_get' from incompatible pointer type
out_fru.c:87: warning: passing argument 8 of 'ipmi_fru_get' from incompatible pointer type
out_fru.c:87: error: too many arguments to function 'ipmi_fru_get'

什么可能导致这些奇怪的平台特定语法错误?我的第一个想法是一些不可打印的字符,但我尝试过检查 cat -e include/OpenIPMI/ipmi_fru.h |少,我看到的只是空格和换行符。

【问题讨论】:

  • time_t 结构的包含文件怎么样?您是否检查过它是否存在以及该结构的定义是什么?
  • 您说的对,我的好先生,显然您不需要在 Linux 上为 time_t 包含 time.h。给我的消息。

标签: c freebsd


【解决方案1】:

在这些类型的神秘错误中,最好的办法是自己运行预处理器,看看会发生什么。有时,标头中的某个标记是#defined,因此几乎不可能知道发生了什么。

为了做到这一点,找到这个 .c 文件的编译行并将其运行为:

cpp <all -I switches from the compilation line> <all -D switches> yourfile.c outfile.tmp

尝试在outfile.tmp 中找到相关行——它可能看起来有点乱,但搜索原始文件名和行号——应该不会太难。当你找到那条线时,希望它不会太难找到实际问题。

【讨论】:

    【解决方案2】:

    您/原作者必须包含包含在编译成功时定义time_t 的标头的文件。但是,您需要正确找到该文件才能知道问题的正确解决方案。

    你不能假设 linux 不要求你包含动摇所有编程基础的文件:)。

    【讨论】:

    • 很可能包含了其他一些系统头文件,它间接包含在 Linux 上定义 time_t 的头文件,但在 FreeBSD 上没有。
    【解决方案3】:

    用户 Praveen 很好地回答了我的问题,但为了避免未回答问题,我将提及我的发现。

    该软件似乎定义了自己的 time_t,或者 Linux 不需要您为 time_t 数据类型包含 time.h。

    无论哪种方式,我都设法通过在 FreeBSD 上简单地包含 time.h 来继续我的移植。

    【讨论】:

    • 您应该阅读标准 (POSIX) 以确定哪些标头定义 time_t,而不是简单地依靠反复试验。或者更确切地说,编写原始软件的人应该这样做......
    • 是的,这个移植过程主要是研究标头及其在这两个系统之间的可用性,现在我正在使用 resolv.h。不,这不是我的软件,我只是希望最终能在 FreeBSD 上使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    相关资源
    最近更新 更多