编译错误:"error_category"未使用 g++ 6.3.0 命名类型"

Compilation error: `‘error_category’ does not name a type` with g++ 6.3.0

本文关键字:quot g++ 类型 category 错误 error 编译 未使用      更新时间:2023-10-16

我尝试编译这个C++/Python库 https://bitbucket.org/fluiddyn/fluidfft

如果安装了 mpi4py,它运行良好。

如果未安装 mpi4py,则无法使用 MPI 的代码无法编译。

在编译 Cython 文件期间引发错误。 错误很长,开头为:

In file included from /usr/include/c++/6/bits/ios_base.h:46:0,
from /usr/include/c++/6/ios:42,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from src_cpp/base/base_fft.h:10,
from fluidfft/fft2d/fft2d_with_fftw1d.cpp:543:
/usr/include/c++/6/system_error:143:31: error: ‘error_category’ does not name a type
error_code(int __v, const error_category& __cat) noexcept
^~~~~~~~~~~~~~
/usr/include/c++/6/system_error:152:27: error: ‘error_category’ does not name a type
assign(int __v, const error_category& __cat) noexcept
^~~~~~~~~~~~~~
/usr/include/c++/6/system_error:172:11: error: ‘error_category’ does not name a type
const error_category&
^~~~~~~~~~~~~~
/usr/include/c++/6/system_error:191:11: error: ‘error_category’ does not name a type
const error_category*  _M_cat;
[...]

我想这可能是一个 C++11 问题 (http://en.cppreference.com/w/cpp/error/error_category(,但我看不出如何解决这个问题。

编译命令是

g++ -pthread -fwrapv -O3 -Wall -Wno-unused-result -Wsign-compare -Wno-unused-result -Wsign-compare -fwrapv -O3 -Wall -fPIC -I/home/users/me/opt/miniconda3/include/python3.6m -I/home/users/me/opt/miniconda3/include -Isrc_cy -Ifluidfft/fft2d -Ifluidfft/fft3d -Isrc_cpp/base -Isrc_cpp/3d -Isrc_cpp/2d -Iinclude -I/home/users/me/opt/miniconda3/lib/python3.6/site-packages/numpy/core/include -c fluidfft/fft2d/fft2d_with_fftw1d.cpp -o build/temp.linux-x86_64-3.6/fluidfft/fft2d/fft2d_with_fftw1d.o

编辑最小、完整和可验证的示例

感谢阿什温毗湿奴(见 https://bitbucket.org/fluiddyn/fluidfft/issues/7/fluidfft-installation-fails-without-mpi4py(,我可以发布一个最小的例子

/* test.cpp */
#include <Python.h>
#include <string.h>
#include <stdio.h>
#include <cpu.h>
#include <sys/time.h>
#include <complex>
#include <iostream>

int main() {
std::cout<<"Hello world";
return 0;
}

从 FluidFFT 目录编译如下:

g++ $(python-config --include) -Iinclude/ test.cpp

如果我们注释掉 cpu.h 包含,则没有错误。

文件 cpu.h 取自 pyfftw 代码:https://github.com/pyFFTW/pyFFTW/blob/master/include/cpu.h

发生这种情况是因为软件包 fluidfft 的 Cython 源文件依赖于C++头文件cpu.h其中以下预处理器行导致了问题:

#if __STDC_VERSION__ >= 199901L
/* "inline" is a keyword */
#else
# define inline
#endif

我的猜测是较新的 g++ 编译器严格重新定义保留关键字。根据一篇关于内联函数的文章的提示,此代码块被替换为:

#if __STDC_VERSION__ >= 199901L
/* "inline" is a keyword */
#else
# define INLINE
#endif
#ifndef INLINE
# if __GNUC__ && !__GNUC_STDC_INLINE__
#  define INLINE static inline
# else
#  define INLINE inline
# endif
#endif