2010-07-15 135 views
0

这对我来说非常困惑,因为代码在Debian 5系统上编译时没有错误,但在FreeBSD 7上编译时出现语法错误,例如第98行。将C代码从Linux移植到FreeBSD的语法错误

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 |更少,我看到的只有空格和换行符。

+3

如何为time_t结构包含文件?你有没有检查这个结构是否存在以及结构的定义是什么? – 2010-07-15 09:39:17

+0

你对我的好先生绝对正确,显然你不需要time.h在Linux上包含time.h。新闻给我。 – 2010-07-15 09:57:52

回答

1

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

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

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

试图找到相关的行outfile.tmp - 它可能看起来有点乱,但搜索原始的文件名和linenumber - 它不应该太难。当你找到这条线时,希望找到实际问题不应该太难。

0

用户Praveen很好地回答了我的问题,但只是这样我就不会回答问题,我会提及我发现的内容。

该软件似乎定义了自己的time_t,无论是Linux还是不要求您将time.h包含在time_t数据类型中。

无论采用哪种方法,我都通过在FreeBSD中包含time.h来设法继续移植。

+1

您应该阅读标准(POSIX)来确定哪些头文件定义了'time_t'而不是简单地依赖于反复试验。或者说,谁写的原始软件应该做到这一点...... – 2010-07-15 10:48:39

+0

是的,这个移植过程主要是研究这两个系统之间的头文件和它们的可用性,现在我正在使用resolv.h。不,这不是我的软件,我只是希望最终在FreeBSD上使用它。 – 2010-07-15 19:48:34

1

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

你只是不能假定linux不需要你包含所有的编程基础知识的文件:)。

+0

可能还包含一些其他系统头文件,它们间接包含在Linux上定义'time_t'的头文件,而不是在FreeBSD上。 – caf 2010-07-16 05:28:37