OpenMPI 1.3编译结构体重定义错误,如何定义HAVE_SYS_TIME_H宏?
我正在试用旧版本的OpenMPI 1.3,通过autogen.sh生成了configure脚本,但执行make时碰到了结构体重定义的错误,具体报错如下:
In file included from event.c:37:0: ../../opal/event/compat/sys/_time.h:47:8: error: redefinition of 'struct timeval' struct timeval { ^ In file included from /usr/include/x86_64-linux-gnu/sys/select.h:45:0, from /usr/include/x86_64-linux-gnu/sys/types.h:219, from /usr/include/stdlib.h:314, from ../../opal/include/opal_config_bottom.h:372, from ../../opal/include/opal_config.h:1341, from event.c:28: /usr/include/x86_64-linux-gnu/bits/time.h:30:8: note: originally defined here struct timeval ^ In file included from event.c:37:0: ../../opal/event/compat/sys/_time.h:55:8: error: redefinition of 'struct timespec' struct timespec { ^ In file included from /usr/include/x86_64-linux-gnu/sys/select.h:43:0, from /usr/include/x86_64-linux-gnu/sys/types.h:219, from /usr/include/stdlib.h:314, from ../../opal/include/opal_config_bottom.h:372, from ../../opal/include/opal_config.h:1341, from event.c:28: /usr/include/time.h:120:8: note: originally defined here struct timespec ^
检查了opal/event/event.c里的代码,发现有条件编译逻辑:
#ifdef HAVE_SYS_TIME_H #include <sys/time.h> #else #include <sys/_time.h> #endif
推测问题出在项目没有正确定义HAVE_SYS_TIME_H宏,导致同时包含了系统的time头文件和OpenMPI自带的兼容头文件,引发重定义。下面是几个可行的解决方法:
方法一:configure时直接定义宏
既然手动指定CPPFLAGS=-I/usr/include/没用,换个思路,直接在configure命令里定义HAVE_SYS_TIME_H宏:./configure CPPFLAGS="-DHAVE_SYS_TIME_H"这个命令会让预处理器在编译时自动定义该宏,强制代码走
#include <sys/time.h>的分支,避免包含自带的兼容头文件。方法二:手动修改兼容头文件
如果方法一无效,可以直接修改opal/event/compat/sys/_time.h,给里面的结构体定义加上条件编译保护:#ifndef HAVE_SYS_TIME_H struct timeval { // 原结构体内容 }; struct timespec { // 原结构体内容 }; #endif这样即使宏没被定义,也不会重复定义系统已经有的结构体。
关于sys目录映射的补充说明
/usr/include/linux/time.h不是标准的sys/time.h,标准的sys/time.h一般在/usr/include/sys/time.h路径下。你可以先执行ls /usr/include/sys/time.h确认这个文件是否存在——如果存在,说明OpenMPI 1.3的configure检测逻辑有bug,没法自动识别到,所以才需要手动定义宏。
另外你提到运行autoreconf -i报错,必须用autogen.sh,这是老版本OpenMPI的特性,所以不用纠结,继续用autogen.sh生成configure就行。
内容的提问来源于stack exchange,提问作者JACK M

