[英]Catalina C++: Using <cmath> headers yield error: no member named 'signbit' in the global namespace
從 Mojave 升級到 Catalina 后,在環境中設置:/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk。
我無法編譯使用<cmath>
header 的程序。
我嘗試更改 CFLAGS、CCFLAGS、CXXFLAGS 以指向沒有任何改變的 MacOSSDK 位置
Scanning dependencies of target OgreMain
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f OgreMain/CMakeFiles/OgreMain.dir/build.make OgreMain/CMakeFiles/OgreMain.dir/build
[ 0%] Building CXX object OgreMain/CMakeFiles/OgreMain.dir/src/OgreASTCCodec.cpp.o
cd /Users/roman/Downloads/ogre-1.12.2/build/OgreMain && /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -DOgreMain_EXPORTS -D__ASSERT_MACROS_DEFINE_VERSIONS_WITHOUT_UNDERSCORES=0 -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/src/OSX -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/include/Threading -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/src -I/Users/roman/Downloads/ogre-1.12.2/build/Dependencies/include -I/Users/roman/Downloads/ogre-1.12.2/OgreMain/include -I/Users/roman/Downloads/ogre-1.12.2/build/include -I/Users/roman/Downloads/ogre-1.12.2/OgreMain -isystem /usr/local/include -Wall -Winit-self -Wcast-qual -Wwrite-strings -Wextra -Wundef -Wmissing-declarations -Wno-unused-parameter -Wshadow -Wno-missing-field-initializers -Wno-long-long -Wno-inconsistent-missing-override -msse -O3 -DNDEBUG -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -std=c++11 -o CMakeFiles/OgreMain.dir/src/OgreASTCCodec.cpp.o -c /Users/roman/Downloads/ogre-1.12.2/OgreMain/src/OgreASTCCodec.cpp
In file included from /Users/roman/Downloads/ogre-1.12.2/OgreMain/src/OgreASTCCodec.cpp:29:
In file included from /Users/roman/Downloads/ogre-1.12.2/OgreMain/src/OgreStableHeaders.h:40:
In file included from /Users/roman/Downloads/ogre-1.12.2/OgreMain/include/OgrePrerequisites.h:309:
In file included from /Users/roman/Downloads/ogre-1.12.2/OgreMain/include/OgreStdHeaders.h:10:
/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;
例如宏: isless
存在於全局命名空間和我的計算機上:
➜ cat math.h | grep "isless"
#define isless(x, y) __builtin_isless((x),(y))
#define islessequal(x, y) __builtin_islessequal((x),(y))
#define islessgreater(x, y) __builtin_islessgreater((x),(y))
➜ pwd
/usr/local/include
➜
甚至 cmath header 也包含它:
➜ cat /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath | grep "math.h"
#include <math.h>
我的命令行有選項-isystem /usr/local/include
這應該工作...
我很好奇:你用的是什么編譯器? CMAKE_OSX_SYSROOT
的值是多少?
我相當確信這是錯誤CMAKE_OSX_SYSROOT
的結果。 I had the problem you're describing when using python bindings for clang (where CMake doesn't manage the compiler call), but I managed to recreate the error in CMake by doing:
set(CMAKE_OSX_SYSROOT "") # Reset.
我按照這個問題的答案解決了我的問題: Cannot compile R packages with c++ code after update to macOS Catalina 。
總結一下:在 Catalina 上, /usr/include
被 SIP 清除和保護。 因此,任何期望在那里找到 C 頭文件的項目都將無法編譯。 如果我沒記錯的話,Apple 建議向/usr/include
中期望 C 標頭的項目提交錯誤報告。
您必須將要編譯的代碼的構建系統指向正確的標頭:
(1) 確保 Xcode 是最新的。 不知道 Catalina 上過時的 Xcode 會對您的構建環境造成什么影響。
(2) 使用-isysroot /sdk/path
編譯器標志,其中/sdk/path
是xcrun --show-sdk-path
的結果。 我不確定 CMake 的最佳做法是什么,但請嘗試做
set(CMAKE_OSX_SYSROOT /sdk/path)
或者
set(CMAKE_CXX_FLAGS "[...] -isysroot /sdk/path")
如果這樣可以解決問題,您可能需要在 CMake 中尋找更好的方法來執行此操作。
當然,如果您喜歡冒險,您也可以禁用 SIP,正如我的問題的回答中所建議的那樣: /usr/include 在 macOS Catalina 上缺失(使用 Xcode 11)
我在嘗試針對 iOS (在我的 MacBook Air 和 GitHub Actions runner 上)時遇到了同樣的問題,盡管我對 Apple 的生態系統不夠熟悉,無法提出適當的解決方案,但這里有一些關於這個問題的想法。 最初的命令行來自 cpprestsdk 中的 CMake,但是一旦我將其歸結為要點,這里有一個簡短的復制。
cmath-bug.cpp
,其中只有一行: #include <cmath>
clang -v -x c++ -target arm64-apple-ios13.2 -fcolor-diagnostics -std=c++11 -stdlib=libc++
-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk
-isystem /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/include
-c cmath-bug.cpp
當我運行它時,我對許多面臨相同問題的人都很熟悉:
Apple clang version 11.0.0 (clang-1100.0.33.16)
Target: arm64-apple-ios13.2
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang" -cc1 -triple arm64-apple-ios13.2.0 -Wdeprecated-objc-isa-usage -Werror=deprecated-objc-isa-usage -Werror=implicit-function-declaration -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name cmath-bug.cpp -mrelocation-model pic -pic-level 2 -mthread-model posix -mdisable-fp-elim -fno-strict-return -masm-verbose -munwind-tables -target-sdk-version=13.2 -target-cpu cyclone -target-feature +fp-armv8 -target-feature +neon -target-feature +crypto -target-feature +zcm -target-feature +zcz -target-feature +sha2 -target-feature +aes -target-abi darwinpcs -fallow-half-arguments-and-returns -dwarf-column-info -debugger-tuning=lldb -ggnu-pubnames -target-linker-version 530 -v -coverage-notes-file /Users/myuser/Projects/C++/cmath-bug.gcno -resource-dir /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk -isystem /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/include -stdlib=libc++ -internal-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1 -Wno-framework-include-private-from-public -Wno-atimport-in-framework-header -Wno-extra-semi-stmt -Wno-quoted-include-in-framework-header -std=c++11 -fdeprecated-macro -fdebug-compilation-dir /Users/myuser/Projects/C++ -ferror-limit 19 -fmessage-length 204 -stack-protector 1 -fstack-check -mdarwin-stkchk-strong-link -fblocks -fencode-extended-block-signature -fregister-global-dtors-with-atexit -fobjc-runtime=ios-13.2.0 -fcxx-exceptions -fexceptions -fmax-type-align=16 -fdiagnostics-show-option -fcolor-diagnostics -o cmath-bug.o -x c++ cmath-bug.cpp
clang -cc1 version 11.0.0 (clang-1100.0.33.16) default target x86_64-apple-darwin19.0.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/Library/Frameworks"
ignoring duplicate directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/include"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/System/Library/Frameworks (framework directory)
End of search list.
In file included from cmath-bug.cpp:1:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/cmath:318: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:319: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:320:9: error: no member named 'isfinite' in the global namespace; did you mean 'finite'?
using ::isfinite;
~~^
我在原始命令行中傳遞的唯一 2 個包含目錄存在並且是:
$ ls -alF /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk
lrwxr-xr-x 1 root wheel 12B Dec 17 11:54 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk@ -> iPhoneOS.sdk
$ ls -alF /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/include
total 2160
drwxr-xr-x 169 root wheel 5.3K Dec 17 12:07 ./
drwxr-xr-x 5 root wheel 160B Nov 4 19:22 ../
...
-rw-r--r-- 9 root wheel 32K Nov 4 19:52 math.h
...
但這里有趣的是它報告的不存在的包含目錄以及最終搜索的包含目錄及其順序。 我的猜測是,命令行中未提及的其他目錄是由 Apple Clang 的驅動程序基於某些 Apple 特定的邏輯插入的。
您可以從報告的錯誤中看到<cmath>
header 位於: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cmath
++/v1/cmath 和它的第 304 行可以看到:
#include <__config> // Line 304
#include <math.h> // This one ends up causing troubles
#include <__cxx_version>
從同一文件夾/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/
中的事實來看,有一個文件math.h
提供了必要的定義,例如:
#include <__config>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif
#include_next <math.h>
#ifdef __cplusplus
// We support including .h headers inside 'extern "C"' contexts, so switch
// back to C++ linkage before including these C++ headers.
extern "C++" {
#include <type_traits>
#include <limits>
// signbit
#ifdef signbit
template <class _A1>
_LIBCPP_INLINE_VISIBILITY
bool
__libcpp_signbit(_A1 __lcpp_x) _NOEXCEPT
{
return signbit(__lcpp_x);
}
#undef signbit
template <class _A1>
inline _LIBCPP_INLINE_VISIBILITY
typename std::enable_if<std::is_floating_point<_A1>::value, bool>::type
signbit(_A1 __lcpp_x) _NOEXCEPT
{
return __libcpp_signbit((typename std::__promote<_A1>::type)__lcpp_x);
}
...
#elif defined(_LIBCPP_MSVCRT)
...
#endif // signbit
<cmath>
的作者期望首先包含同一文件夾中的math.h
,然后#include_next <math.h>
指令找到系統特定的math.h
。 然而,事實並非如此。
如果您查看搜索目錄中的前 2 個條目:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
您會看到特定於系統的包含目錄最終位於注入 Clang 的標准庫目錄之上,這就是為什么找到特定於系統的math.h
,而不是與標准庫頭文件的 rest 位於同一文件夾中的原因。 可能是這種情況,因為如果我在其他兩個目錄之前將標准庫包含目錄顯式添加到我的命令行-isystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
問題消失了,我可以編譯該文件。 這不是 Clang 的驅動程序或此處涉及的任何其他內容自動執行的操作:它通過-internal-system
添加該標准庫目錄(不確定該內部標志的語義是什么)並將其添加到系統目錄之后。
現在,如果您查看被忽略目錄的列表,該列表中的第一個條目是:
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk/usr/include/c++/v1"
其中后面的c++/v1
部分在我的機器上不存在,這讓我想知道 iPhone SDK 安裝是否應該在路徑的現有部分內創建一個符號鏈接c++
以指向/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++
目錄使整個工作正常。
無論如何,這就是我認為正在發生的事情,我想知道是否有人知道如何正確解決這個問題?
謝謝!
PS對於上下文:
$ xcode-select -p
/Applications/Xcode.app/Contents/Developer
$ xcrun --show-sdk-path -sdk iphoneos13.2
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.2.sdk
使用命令:
gcc -Wp,-v -E -
我的#include <...> 搜索序列:
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/10.0.1/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include
/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/System/Library/Frameworks (framework directory)
#include 錯誤的原因描述如下:
#include<cmath>
位於/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
<math.h>
。/usr/local/include
目錄,因為這是要搜索的第一個目錄。 /usr/local/include/c++/9.3.0/
目錄中有一個math.h
math.h
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1
的math.h
包含/usr/local/include
include 的math.h
使用#include_next<math.h>
math.h
包含/鏈接到/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cmath
,會發生編譯錯誤修復:
如果我們可以先將#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
中使用#include</Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/math.h>
而不是<math.h>
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cmath
我遵循了#2 選項,現在構建成功!
並感謝solodon的詳細回答。 我按照答案解決了這個問題。
您可以嘗試使用 CommandLineTools SDK 而不是 XCode.app SDK。
我在編譯 PointCloudLibrary (PCL) 時解決了這個問題
#Check the current sdk
xcrun --show-sdk-path
#Change sdk
sudo xcode-select -s /Library/Developer/CommandLineTools #Using CommandLineTools SDK
sudo xcode-select -s /Applications/Xcode.app/Contents/Developer #Using XCode.app SDK
此外,重新安裝 XCode.app 和 CommandLineTools 可能會有所幫助。
您的 Xcode 副本可能已損壞。 檢查代碼設計:
codesign --verify /Applications/Xcode.app
這發生在我身上,問題是 Xcode 損壞。 重新安裝修復它。
修改了以下內容:
file added: /Applications/Xcode-11.3.1-.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/json/__pycache__/scanner.cpython-37.pyc
file added: /Applications/Xcode-11.3.1-.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/json/__pycache__/decoder.cpython-37.pyc
file added: /Applications/Xcode-11.3.1-.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/json/__pycache__/encoder.cpython-37.pyc
file added: /Applications/Xcode-11.3.1-.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/json/__pycache__/__init__.cpython-37.pyc
file modified: /Applications/Xcode-11.3.1-.app/Contents/Developer/Platforms/AppleTVOS.platform/Developer/SDKs/AppleTVOS.sdk/usr/include/math.h
file modified: /Applications/Xcode-11.3.1-.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/math.h
file modified: /Applications/Xcode-11.3.1-.app/Contents/Developer/Platforms/WatchOS.platform/Developer/SDKs/WatchOS.sdk/usr/include/math.h
file modified: /Applications/Xcode-11.3.1-.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/math.h
file modified: /Applications/Xcode-11.3.1-.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers/math.h
file modified: /Applications/Xcode-11.3.1-.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/DriverKit19.0.sdk/System/DriverKit/usr/include/math.h
file modified: /Applications/Xcode-11.3.1-.app/Contents/Developer/Platforms/WatchSimulator.platform/Developer/SDKs/WatchSimulator.sdk/usr/include/math.h
file modified: /Applications/Xcode-11.3.1-.app/Contents/Developer/Platforms/AppleTVSimulator.platform/Developer/SDKs/AppleTVSimulator.sdk/usr/include/math.h
file modified: /Applications/Xcode-11.3.1-.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/usr/include/math.h
math.h
在上述所有地方都是空的。
我發現在我的項目中我有文件math.h
。 重命名后問題就消失了。 Seams cmath
包含我的文件而不是系統。
重新安裝 Xcode、命令工具和自制軟件,以及
sudo rm -rf /usr/local/include
為我解決了這個問題。
在最近升級到 10.15.4 和 Xcode 11.4 后,我在嘗試編譯 gRPC 時遇到了這個錯誤,我開始查看提供的所有解決方案(在這里, 升級到 Catalina 后無法在 Mac 上編譯 C 程序)。 ,並嘗試了其中的一些(盡管沒有嘗試重新創建/usr/include
,因為這會違反 Apple 試圖創建的分離)- 似乎沒有任何效果。
然后我仔細查看了make
過程產生的實際編譯器調用,並注意到有一個顯式的
-I/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include
這最終導致包含以錯誤的順序發生 - 刪除此顯式包含路徑允許編譯成功,默認安裝 catalina、Xcode 和 Xcode 命令行工具,如您所料,沒有其他技巧/編譯器標志需要。
@solodon 的分析很到位。 根據 header 文件的搜索順序,問題可能是cmath
文件包含錯誤版本的math.h
至少,當我遇到同樣的錯誤時,這就是發生在我身上的事情。
掃描您的編譯器 output 以查找#include <...> search starts here:
. 您還可以使用(source)從命令行強制此 output :
gcc -Wp,-v -E -
它應該看起來像這樣:
/usr/local/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.0/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks (framework directory)
請注意,帶有Toolchains
的路徑位於帶有Platforms
的路徑之前。 如果在您的情況下順序顛倒,您需要弄清楚您的配置中是什么導致了這種情況。 對我來說,這是我登錄腳本中CPLUS_INCLUDE_PATH
的顯式設置。
違規代碼:
XCBASE=`xcrun --show-sdk-path`
export CPLUS_INCLUDE_PATH=$XCBASE/usr/include
This was part of my attempt to work around Xcode 11 no longer providing the installation package for the SDK header files. 刪除此代碼后,我能夠成功地將cmath
包含在我的 C++ 代碼中。
如果您來這里尋找此問題的解決方案,您可能需要不同的解決方案,但希望這有助於闡明此問題的根本原因,header 文件搜索路徑順序。
@mkl 的解決方案有助於解決我這邊的類似問題。 謝謝。
我在PCL的基礎上編譯項目時遇到了同樣的問題。 解決辦法是設置CommandLineTools SDK CMAKE_OSX_SYSROOT。
我在 CMakeLists.txt 中嘗試了 @mkl 的解決方案,但它不起作用。 所以我只能通過cmake.. -DCMAKE_OSX_SYSROOT="/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk"
設置它
另一件事要提一下:我的 MacOS 中有兩個版本的命令行 SDK:
~ » /Library/Developer/CommandLineTools/SDKs/MacOSX
/Library/Developer/CommandLineTools/SDKs/MacOSX
MacOSX.sdk/ MacOSX10.14.sdk@
xcrun --show-sdk-path
給出了默認值,但實際上只有 MacOSX10.14.sdk 對我有用。
在 PCL 的 repo 中討論類似問題: https://github.com/PointCloudLibrary/pcl/issues/2601#issuecomment-621889211
我對c++中的cmake和SDK了解不多。 我在來自不同 SDK 的 cmake 構建日志中注意到的差異如下:
/Library/Developer/CommandLineTools/SDKs/MacOSX
:
-- Found OpenGL: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/OpenGL.framework
/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk
-- Found OpenGL: /Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/OpenGL.framework
默認情況下:
-- Found OpenGL: /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/OpenGL.framework
似乎問題發生在 Opengl 的 SDK 選擇上。
如果有人可以提供建議,將不勝感激。
我終於明白了為什么這只影響了一些人。 當 Catalina 出現時,他們停止發布帶有 /usr 標頭的命令行工具 package。 一個常見的解決方法是使用 CPATH 指向 bashrc 中的系統頭文件,如下所示:
export CPATH=`xcrun --show-sdk-path`/usr/include
然而,這似乎弄亂了 C++ 包括 cmath 和 math.h 的路徑排序(如在其他答案中找到)。 好消息是,當前 clang 和 gcc 的自制版本似乎不再需要設置 CPATH 來查找系統頭文件。 所以你可以刪除 CPATH 覆蓋並完成它。
摘要:在我的情況下,構建腳本使用的是舊版本的ios-cmake
工具鏈(2.1.2),並將其更新到 3.1.2 修復了 cmath/math 包含問題。
適應@Ryan H. gcc -Wp,-v -E -
對於我的情況(clang,c++,iOs 目標)
clang -x c++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.4.sdk -Wp, -v -E -
兩個 Catalina 的產量,包括一個處女,其中唯一安裝的工具是 XCode 11.14.1:
clang -cc1 version 11.0.3 (clang-1103.0.32.59) default target x86_64-apple-darwin19.4.0
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.4.sdk/usr/include/c++/v1"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.4.sdk/usr/local/include"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.4.sdk/Library/Frameworks"
#include "..." search starts here:
#include <...> search starts here:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/11.0.3/include
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.4.sdk/usr/include
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include
/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.4.sdk/System/Library/Frameworks (framework directory)
End of search list.
所以正確的包含路徑是第一個未被忽略的路徑,一切都應該正常,但它沒有。 看來問題來自 ios-cmake 工具鏈附加到編譯調用的附加包含命令:
CompileC /Users/<...>/build.Release.ios/<...>.o <...>.cpp normal arm64 c++ com.apple.compilers.llvm.clang.1_0.compiler
-Isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.4.sdk <...>
-I/Users/<...>/Build_iOS/build.Release.ios/build.arm/Binaries/Release/include
-Isystem /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS13.4.sdk/usr/include
-I/Users/<...>/Build_iOS/build.Release.ios/build.arm/src/<...>.build/Release-iphoneos/<...>/DerivedSources/arm64
...
罪魁禍首是-Isystem...
行,這將導致 cmath 文件中的#include <math>
行最終加載錯誤的文件。 在嘗試修復 cmake 腳本后,我注意到舊版本的 ios-cmake,並且更新它具有刪除不需要的-Isystem
行的“唯一”效果 - 其他一切幾乎相同(除了一些編譯器選項)
刪除MacOSX10.15.sdk
有效。
就我而言,它發生在我 cmake llvm 項目並在 Xcode 中構建 cmaked 項目時。 編譯 LibEdit 時檢測到此錯誤。
我已經在我的 MBP 上成功編譯了這個項目,但在我的 iMac 上失敗了。 它們都是相同的 macOS 11.3.1、Xcode 12.5、命令行工具 12.5,其他配置也是如此。
所以我比較了 cmake 日志,發現不同:在 MBP 上,LibEdit 的日志為:
-- Found LibEdit: /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include (found version ".")
在 iMac 上,LibEdit 的日志為:
-- Found LibEdit: /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include (found version ".")
這就是導致構建錯誤的原因。
CommandLineTools 目錄文件完全相同:
MacOSX11.1.sdk,
MacOSX11.3.sdk,
MacOSX10.15.sdk,
MacOSX.sdk link to MacOSX11.3.sdk
MacOSX11.sdk link to MacOSX11.3.sdk
和 Xcode 中的兩個命令行工具 - 首選項 - 位置設置為 Xcode12.5(12E262)
我在 iMac 上重新安裝了 Xcode 和命令行工具。 但是沒有用。
cmake 命令指定 - DCMAKE_OSX_SYSROOT="/Library/Developer/CommandLineTools/SDKs/MacOSX11.3.sdk"
。 還是沒用。
我不知道在哪里設置這個。 最后,我刪除了MacOSX10.15.sdk
,它起作用了。
將 Ryan H 和 Solodon 的解決方案放在一起解決了我的問題。 這是我修復它的方法(Mac OS 10.15.7):
#############################
#Give C and C++ compilers directions to the right headers
#which are no longer in /usr/include, as expected.
#We should be using the Xcode.app libraries
XCOD="/Applications/Xcode.app/Contents/Developer"
#Append to the front of the path (reverse order, second line ends up searched first)
export PATH="$XCOD/Toolchains/XcodeDefault.xctoolchain/usr/include:$PATH"
export PATH="$XCOD/Toolchains/XcodeDefault.xctoolchain/usr/bin/:$PATH"
#Append to the end:
export PATH="$PATH:$XCOD/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include"
export PATH="$PATH:$XCOD/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"
#Tell compiler to use this SDK's compilers to compile C and C++ code
MYSDK="/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk"
export CFLAGS="-isysroot $MYSDK"
export CCFLAGS="-isysroot $MYSDK"
export CXXFLAGS="-isysroot $MYSDK"
export CPPFLAGS="-isysroot $MYSDK"
###########################
然后通過運行使這些更新生效
source ~/.zshrc
這將使 c++ 編譯器以正確的順序在 Xcode.app 中查找標頭(例如 math.h)。 要驗證這是否有效,請鍵入
gcc -Wp,-v -E -
結果應顯示以“/XcodeDefault.xctoolchain/usr/include”結尾的路徑之前以“/XcodeDefault.xctoolchain/usr/bin”結尾的路徑。
#include <math.h>
和
#include </Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/math.h
(這是我系統上的第 304 行)。 這可以防止 cmath 嘗試使用系統上其他地方存在的不同math.h header 文件,並且沒有此 cmath 文件期望的某些定義。
編譯器試圖找到math.h
,通常它會先在/usr/local/include
下找到它,然后得到一個錯誤的。
修復它的一個簡單方法是使用以下命令重命名它: mv /usr/local/include/math.h /usr/local/include/math_old.h
。 然后編譯器會嘗試以下包含目錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.