标识符"creal"未定义 - 在 Mac 上可见,但在 Linux 上看不到

identifier "creal" is undefined - seen on Mac but not on Linux

本文关键字:Linux 看不到 但在 Mac creal 未定义 标识符      更新时间:2023-10-16

问题

以下代码是错误的还是 Mac 上的标头有问题? 该错误不会出现在 Linux 上,这使我得出结论,这不是不正确的代码,尽管在 Linux 上成功编译并不是对 ISO 合规性的特别严格的测试。

源代码 (MCVE)

#include <stdio.h>
#include <complex.h>
void foo(const double *A, int *size){
   for(int i=0;i < size[0]; ++i){
      for(int j=0;j < size[1]; ++j){
         printf("(%.2e,%.2e) ", creal(A[i + j * size[0]]), cimag(A[i + j * size[0]]));
      }
   }
}

苹果电脑

系统信息

$ uname -a
Darwin redacted 15.4.0 Darwin Kernel Version 15.4.0: Fri Feb 26 22:08:05 PST 2016; root:xnu-3248.40.184~3/RELEASE_X86_64 x86_64

英特尔编译器

$ icpc -v
icpc version 16.0.2 (gcc version 4.9.0 compatibility)
$ icpc -c ttc-creal.cpp 
ttc-creal.cpp(7): error: identifier "creal" is undefined
           printf("(%.2e,%.2e) ", creal(A[i + j * size[0]]), cimag(A[i + j * size[0]]));
                                  ^
ttc-creal.cpp(7): error: identifier "cimag" is undefined
           printf("(%.2e,%.2e) ", creal(A[i + j * size[0]]), cimag(A[i + j * size[0]]));
                                                             ^
compilation aborted for ttc-creal.cpp (code 2)

LLVM 编译器

$ clang++ -v
Apple LLVM version 7.3.0 (clang-703.0.29)
Target: x86_64-apple-darwin15.4.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ clang++ -c ttc-creal.cpp 
ttc-creal.cpp:7:33: error: use of undeclared identifier 'creal'
         printf("(%.2e,%.2e) ", creal(A[i + j * size[0]]), cimag(A[i + j * size[0]]));
                                ^
ttc-creal.cpp:7:60: error: use of undeclared identifier 'cimag'
         printf("(%.2e,%.2e) ", creal(A[i + j * size[0]]), cimag(A[i + j * size[0]]));
                                                           ^
2 errors generated.

Linux目录

系统信息

$ uname -a
Linux redacted 2.6.32-573.18.1.el6.centos.plus.x86_64 #1 SMP Wed Feb 10 18:09:24 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux

GNU 编译器

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/opt/gcc/5.3.0/libexec/gcc/x86_64-unknown-linux-gnu/5.3.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with: /home/redacted/Work/GCC/gcc-5.3.0/configure --prefix=/opt/gcc/5.3.0 --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --enable-languages=c,c++,fortran --with-tune=native --enable-bootstrap --enable-lto --with-mpfr --with-isl --with-gmp --with-mpc --with-cloog --enable-gold --enable-ld --disable-multilib
Thread model: posix
gcc version 5.3.0 (GCC) 
$ g++ -c ttc-creal.cpp
<no error>

英特尔编译器

$ icpc -v
icpc version 17.0.0 Beta (gcc version 5.3.0 compatibility)
$ icpc -c ttc-creal.cpp
<no error>

LLVM 编译器

$ clang++ -v
clang version 3.4.2 (tags/RELEASE_34/dot2-final)
Target: x86_64-redhat-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.4
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.4.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-redhat-linux/4.4.7
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-redhat-linux/4.4.7
$ clang++ -c ttc-creal.cpp 
<no error>

调查

我已经通读了Mac上所有相关的complex.hccomplexcomplex标题,但看不到此问题的明显原因。

这只是

Clang对C++标准的解读 - 看到那个答案 - 在我看来是正确的。基本上,您不应该在C++中使用 C99 复杂类型和关联的函数,而应该改用std::complex<T>。这两种复杂类型都应该与布局兼容,并且您应该能够在 C 和 C++ 代码之间传递复杂数据。

g++complex.h包括<ccomplex>(在 C++11 模式下)和 C <complex.h>,因此代码使用 GCC 编译(在我的情况下来自 Homebrew)。我没有适用于 OS X 的英特尔编译器,但您可以检查它对 icpc -E ttc-creal.cpp | grep complex 的作用,并观察包含文件扩展的顺序。

铛:

$ clang++ -E ttc-creal.cpp | grep "^# 1 " | grep complex
# 1 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/complex.h" 1 3
# 1 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ccomplex" 1 3
# 1 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/complex" 1 3

海湾合作委员会:

$ g++-5 -E ttc-creal.cpp | grep "^# 1 " | grep complex
# 1 "/usr/local/Cellar/gcc/5.3.0/include/c++/5.3.0/complex.h" 1 3
# 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/complex.h" 1 3 4
$ g++-5 -std=c++11 -E ttc-creal.cpp | grep "^# 1 " | grep complex
# 1 "/usr/local/Cellar/gcc/5.3.0/include/c++/5.3.0/complex.h" 1 3
# 1 "/usr/local/Cellar/gcc/5.3.0/include/c++/5.3.0/ccomplex" 1 3
# 1 "/usr/local/Cellar/gcc/5.3.0/include/c++/5.3.0/complex" 1 3
# 1 "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/complex.h" 1 3 4

您可以通过以下方式强制 Clang 包含<path to SDK>/usr/include/complex.h

#include "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk/usr/include/complex.h"

并且您的MCVE将编译,但这样做可能是一个非常糟糕的主意(tm)。

顺便说一下, 在 FreeBSD 10.2-RELEASE 上观察到了相同的行为, Clang 3.4.1 (错误) 和 GCC 4.8.5 (成功)。

所有的功劳都归功于Potatoswatter - 顶部引用的答案的作者。