编译OpenBabel时遭遇clock未定义错误,寻求解决方案
修复Debian 6.1.85-1编译OpenBabel 3.1.1的报错问题
问题根源
编译终止的核心原因是OpenBabel的obutil.h头文件未包含<ctime>标准库头,导致clock()函数和CLOCKS_PER_SEC宏无法被编译器识别。日志中出现的std::binary_function警告属于旧C++特性弃用提示,不影响编译,可忽略。
修复步骤
- 打开OpenBabel源码目录下的头文件:
/home/vasek/openbabel-3.1.1/include/openbabel/obutil.h - 在第39行的
#include <math.h>下方添加一行:#include <ctime> - 保存文件后,回到编译目录执行重新编译命令:
make clean && make
编译报错日志(原日志)
In file included from /home/vasek/openbabel-3.1.1/include/openbabel/format.h:22, from /home/vasek/openbabel-3.1.1/include/openbabel/obconversion.h:37, from /home/vasek/openbabel-3.1.1/include/openbabel/obmolecformat.h:36, from /home/vasek/openbabel-3.1.1/src/formats/asciiformat.cpp:19: /home/vasek/openbabel-3.1.1/include/openbabel/plugin.h:41:42: warning: ‘template<class _Arg1, class _Arg2, class _Result> struct std::binary_function’ is deprecated [-Wdeprecated-declarations] 41 | struct OBERROR CharPtrLess : public std::binary_function<const char*,const char*, bool> | ^~~~~~~~~~~~~~~ In file included from /usr/include/c++/12/string:48, from /usr/include/c++/12/bits/locale_classes.h:40, from /usr/include/c++/12/bits/ios_base.h:41, from /usr/include/c++/12/ios:42, from /usr/include/c++/12/ostream:38, from /usr/include/c++/12/iostream:39, from /home/vasek/openbabel-3.1.1/include/openbabel/obconversion.h:24: /usr/include/c++/12/bits/stl_function.h:131:12: note: declared here 131 | struct binary_function | ^~~~~~~~~~~~~~~ [ 34%] Building CXX object src/formats/CMakeFiles/asciiformat.dir/__/depict/asciipainter.cpp.o In file included from /home/vasek/openbabel-3.1.1/src/depict/asciipainter.cpp:18: /home/vasek/openbabel-3.1.1/include/openbabel/obutil.h: In member function ‘void OpenBabel::OBStopwatch::Start()’: /home/vasek/openbabel-3.1.1/include/openbabel/obutil.h:65:14: error: ‘clock’ was not declared in this scope 65 | start= clock(); | ^~~~~ /home/vasek/openbabel-3.1.1/include/openbabel/obutil.h:40:1: note: ‘clock’ is defined in header ‘<ctime>’; did you forget to ‘#include <ctime>’? 39 | #include <math.h> +++ |+#include <ctime> 40 | /home/vasek/openbabel-3.1.1/include/openbabel/obutil.h: In member function ‘double OpenBabel::OBStopwatch::Lap()’: /home/vasek/openbabel-3.1.1/include/openbabel/obutil.h:70:13: error: ‘clock’ was not declared in this scope 70 | stop= clock(); | ^~~~~ /home/vasek/openbabel-3.1.1/include/openbabel/obutil.h:70:13: note: ‘clock’ is defined in header ‘<ctime>’; did you forget to ‘#include <ctime>’? /home/vasek/openbabel-3.1.1/include/openbabel/obutil.h:71:40: error: ‘CLOCKS_PER_SEC’ was not declared in this scope 71 | return((stop - start) / (double) CLOCKS_PER_SEC); | ^~~~~~~~~~~~~~ /home/vasek/openbabel-3.1.1/include/openbabel/obutil.h:71:40: note: the macro ‘CLOCKS_PER_SEC’ had not yet been defined In file included from /usr/include/time.h:33, from /usr/include/pthread.h:23, from /usr/include/x86_64-linux-gnu/c++/12/bits/gthr-default.h:35, from /usr/include/x86_64-linux-gnu/c++/12/bits/gthr.h:148, from /usr/include/c++/12/ext/atomicity.h:35, from /usr/include/c++/12/bits/ios_base.h:39, from /usr/include/c++/12/ios:42, from /usr/include/c++/12/istream:38, from /usr/include/c++/12/sstream:38, from /home/vasek/openbabel-3.1.1/include/openbabel/depict/painter.h:25, from /home/vasek/openbabel-3.1.1/include/openbabel/depict/asciipainter.h:21, from /home/vasek/openbabel-3.1.1/src/depict/asciipainter.cpp:19: /usr/include/x86_64-linux-gnu/bits/time.h:34: note: it was later defined here 34 | #define CLOCKS_PER_SEC ((__clock_t) 1000000) | make[2]: *** [src/formats/CMakeFiles/asciiformat.dir/build.make:90: src/formats/CMakeFiles/asciiformat.dir/__/depict/asciipainter.cpp.o] Chyba 1 make[1]: *** [CMakeFiles/Makefile2:1718: src/formats/CMakeFiles/asciiformat.dir/all] Error 2 make: *** [Makefile:146: all] Error 2
内容的提问来源于stack exchange,提问作者user3120717
相关产品推荐
相关产品推荐

