【问题标题】:Cmake compile error when include cmath header包含 cmath 标头时 Cmake 编译错误
【发布时间】:2019-10-22 06:48:54
【问题描述】:

这是我的代码:

#include <iostream>
#include <cmath>

int main() {
    std::cout << std::sqrt(4) << std::endl;
    return 0;
}

MAC OS 10.15、CMake 3.15.4

只要包含头文件cmath.h,CMake编译就会出错。

执行命令:

cmake --build /Users/xxx/Documents/Playground/test1/cmake-build-debug --target test1 -- -j 4

出现以下错误:

/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/xxx/Documents/Playground/test/cmake-build-debug --target test -- -j 4
Scanning dependencies of target test
[ 50%] Building CXX object CMakeFiles/test.dir/main.cpp.o
In file included from /Users/xxx/Documents/Playground/test/main.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:314:9: error: no member named 'signbit' in the global namespace
using ::signbit;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:315:9: error: no member named 'fpclassify' in the global namespace
using ::fpclassify;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:316:9: error: no member named 'isfinite' in the global namespace; did you mean 'finite'?
using ::isfinite;
~~^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h:749:12: note: 'finite' declared here
extern int finite(double)
^
In file included from /Users/xxx/Documents/Playground/test/main.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:317:9: error: no member named 'isinf' in the global namespace
using ::isinf;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:318:9: error: no member named 'isnan' in the global namespace
using ::isnan;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:319:9: error: no member named 'isnormal' in the global namespace
using ::isnormal;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:320:7: error: no member named 'isgreater' in the global namespace; did you mean '::std::greater'?
using ::isgreater;
^~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:728:29: note: '::std::greater' declared here
struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool>
^
In file included from /Users/xxx/Documents/Playground/test/main.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:321:7: error: no member named 'isgreaterequal' in the global namespace; did you mean '::std::greater_equal'?
using ::isgreaterequal;
^~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/functional:757:29: note: '::std::greater_equal' declared here
struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool>
^
In file included from /Users/xxx/Documents/Playground/test/main.cpp:2:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:322:9: error: no member named 'isless' in the global namespace
using ::isless;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:323:9: error: no member named 'islessequal' in the global namespace
using ::islessequal;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:324:9: error: no member named 'islessgreater' in the global namespace
using ::islessgreater;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:325:9: error: no member named 'isunordered' in the global namespace
using ::isunordered;
~~^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:326:9: error: no member named 'isunordered' in the global namespace
using ::isunordered;
~~^
13 errors generated.
gmake[3]: *** [CMakeFiles/test.dir/build.make:63: CMakeFiles/test.dir/main.cpp.o] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/test.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/test.dir/rule] Error 2
gmake: *** [Makefile:118: test] Error 2

使用 XCode 成功编译相同的代码。 如果删除头文件 cmath 的包含,CMake 工作正常。但是我需要在 cmath 中使用函数,这不能解决。

【问题讨论】:

  • 这些不是 CMake 错误,它们是 编译器 错误。
  • 有一个解决方案here,尽管我怀疑删除 XCode 是否适合您。此问题可能是由于在同一台机器上尝试使用 CLion 工具时安装了 XCode。
  • thetechawesomeness.ideasmatter.info/…有一些关于配置的更新。

标签: c++ xcode cmake clion


【解决方案1】:

我在 ma​​cOS Catalina 10.15 上也遇到了同样的问题,同时尝试为 Internet 系统安装 离散事件网络模拟器 (ns-3)

显然,我的/usr/local/include 文件夹中有一堆奇怪的符号链接。

  • 我删除了/usr/local/include/
  • 中的所有内容
  • 重新安装 Homebrew $ brew upgrade

一切又恢复了正常。

【讨论】:

  • 这个答案对我有帮助。但是,我建议不要删除整个 /usr/local/include 目录。对于我的问题,原来是我通过brew 安装的一个新包,其中包含与现有头文件冲突的头文件,并且 macOS 的 clang 不知何故选择使用新的头文件。
【解决方案2】:

这个问题是由于在我的 .profile 中添加以下行引起的

#export CPLUS_INCLUDE_PATH="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include"

注释掉这一行,然后 c++ 编译没有错误。

似乎我添加了这一行来修复 MAC OS 10.14 中的 wchar.h 文件未找到错误。但是 wchar.h not found 的原因目前尚不清楚,可能与某些配置问题有关,而不是操作系统。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    相关资源
    最近更新 更多