包含 cmath 标头时 Cmake 编译错误

use*_*765 6 c++ xcode cmake clion

这是我的代码:

#include <iostream>
#include <cmath>

int main() {
    std::cout << std::sqrt(4) << std::endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

MAC操作系统10.15,CMake 3.15.4

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

执行命令:

cmake --build /Users/xxx/Documents/Playground/test1/cmake-build-debug --target test1 -- -j 4
Run Code Online (Sandbox Code Playgroud)

出现以下错误:

/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
Run Code Online (Sandbox Code Playgroud)

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

use*_*765 2

此问题是由于将以下行添加到我的 .profile 中引起的

#export CPLUS_INCLUDE_PATH="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include"
Run Code Online (Sandbox Code Playgroud)

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

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