MacOS下编译C++ gRPC项目报错:全局命名空间无signbit等cmath成员
问题描述
长话短说: 编译时引入#include <cmath>头文件触发报错no member named 'signbit' in the global space
目标: 编译纯C++实现的gRPC客户端-服务端应用
报错信息
g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client `pkg-config --libs --cflags protobuf grpc++` In file included from client.cpp:3: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpc++/grpc++.h:26: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/grpcpp.h:52: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/channel.h:28: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/impl/codegen/completion_queue.h:45: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/impl/codegen/sync.h:32: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/synchronization/mutex.h:72: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/synchronization/internal/kernel_timeout.h:34: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/time/clock.h:26: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/time/time.h:78: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:321:9: error: no member named 'signbit' in the global namespace using ::signbit; ~~^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:322:9: error: no member named 'fpclassify' in the global namespace using ::fpclassify; ~~^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:323:9: error: no member named 'isfinite' in the global namespace using ::isfinite; ~~^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:324:9: error: no member named 'isinf' in the global namespace using ::isinf; ~~^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:325:9: error: no member named 'isnan' in the global namespace using ::isnan; ~~^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:326:9: error: no member named 'isnormal' in the global namespace using ::isnormal; ~~^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:327:7: error: no member named 'isgreater' in the global namespace; did you mean '::std::greater'? using ::isgreater; ^~ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/functional:742:29: note: '::std::greater' declared here struct _LIBCPP_TEMPLATE_VIS greater : binary_function<_Tp, _Tp, bool> ^ In file included from client.cpp:3: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpc++/grpc++.h:26: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/grpcpp.h:52: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/channel.h:28: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/impl/codegen/completion_queue.h:45: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/impl/codegen/sync.h:32: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/synchronization/mutex.h:72: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/synchronization/internal/kernel_timeout.h:34: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/time/clock.h:26: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/time/time.h:78: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:328:7: error: no member named 'isgreaterequal' in the global namespace; did you mean '::std::greater_equal'? using ::isgreaterequal; ^~ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/functional:771:29: note: '::std::greater_equal' declared here struct _LIBCPP_TEMPLATE_VIS greater_equal : binary_function<_Tp, _Tp, bool> ^ In file included from client.cpp:3: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpc++/grpc++.h:26: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/grpcpp.h:52: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/channel.h:28: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/impl/codegen/completion_queue.h:45: In file included from /opt/homebrew/Cellar/grpc/1.41.1/include/grpcpp/impl/codegen/sync.h:32: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/synchronization/mutex.h:72: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/synchronization/internal/kernel_timeout.h:34: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/time/clock.h:26: In file included from /opt/homebrew/Cellar/abseil/20210324.2_1/include/absl/time/time.h:78: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:329:9: error: no member named 'isless' in the global namespace using ::isless; ~~^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:330:9: error: no member named 'islessequal' in the global namespace using ::islessequal; ~~^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:331:9: error: no member named 'islessgreater' in the global namespace using ::islessgreater; ~~^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:332:9: error: no member named 'isunordered' in the global namespace using ::isunordered; ~~^ /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath:333:9: error: no member named 'isunordered' in the global namespace using ::isunordered; ~~^
以上仅为截取的部分报错内容,还有更多cmath相关错误,推测找到正确解决方案后可全部消除。
已尝试的解决方法
- 切换不同SDK路径
# 查看当前SDK路径 xcrun --show-sdk-path # 切换SDK sudo xcode-select -s /Library/Developer/CommandLineTools # 使用CommandLineTools SDK sudo xcode-select -s /Applications/Xcode.app/Contents/Developer # 使用XCode.app SDK
- 修改cmath文件中的
#include <math.h>引用
修复方案:
- 调整
#include<...>的搜索顺序,优先搜索/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1路径即可解决。- 修改
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cmath文件中的<math.h>引用,替换为#include</Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/math.h>
- 在编译命令中添加
-isysroot /sdk/path参数,完整命令如下:g++ -std=c++17 client.cpp mathtest.pb.cc mathtest.grpc.pb.cc -o client -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk 'pkg-config --libs --cflags protobuf grpc++' - 卸载、重装并更新Xcode命令行工具
- 重装gcc
设备配置
,xNMM. --------------------------------- .OMMMMo OS: macOS 12.0.1 21A559 arm64 OMMM0, Host: MacBookPro18,3 .;loddo:' loolloddol;. Kernel: 21.1.0 cKMMMMMMMMMMNWMMMMMMMMMM0: Uptime: 12 days, 22 hours, 41 mins .KMMMMMMMMMMMMMMMMMMMMMMMWd. Packages: 53 (brew) XMMMMMMMMMMMMMMMMMMMMMMMX. Shell: zsh 5.8 ;MMMMMMMMMMMMMMMMMMMMMMMM: Resolution: 1512x982 :MMMMMMMMMMMMMMMMMMMMMMMM: DE: Aqua .MMMMMMMMMMMMMMMMMMMMMMMMX. WM: Quartz Compositor kMMMMMMMMMMMMMMMMMMMMMMMMWd. WM Theme: Blue (Dark) .XMMMMMMMMMMMMMMMMMMMMMMMMMMk Terminal: iTerm2 .XMMMMMMMMMMMMMMMMMMMMMMMMK. Terminal Font: Monaco 12 kMMMMMMMMMMMMMMMMMMMMMMd CPU: Apple M1 Pro ;KMMMMMMMWXXWMMMMMMMk. GPU: Apple M1 Pro .cooc,. .,coo:. Memory: 3551MiB / 32768MiB
目前暂未找到其他可行排查方向,经过数小时调研,推测该问题为MacOS系统层面问题而非项目配置问题,恳请各位提供协助,非常感谢。
内容的提问来源于stack exchange,提问作者zebra14
相关产品推荐
相关产品推荐

