![Rcpp 在 macOS 下不可用,未找到 math.h](https://linux22.com/image/1593329/Rcpp%20%E5%9C%A8%20macOS%20%E4%B8%8B%E4%B8%8D%E5%8F%AF%E7%94%A8%EF%BC%8C%E6%9C%AA%E6%89%BE%E5%88%B0%20math.h.png)
Rcpp 包无法运行 R 3.6 MacOS Mojave。
我运行时出现以下错误
Library(Rcpp)
evalCpp("2 + 2")
输出为:
clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include" -I"/private/var/folders/h5/r7lh__p16wx7cg_5pzknfg5w0000gn/T/Rtmpz3QxlQ/sourceCpp-x86_64-apple-darwin15.6.0-1.0.1" -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -I/usr/local/include -fPIC -Wall -g -O2 -c file7d712a4a950.cpp -o file7d712a4a950.o
Error in sourceCpp(code = code, env = env, rebuild = rebuild, cacheDir = cacheDir, :
Error 1 occurred building shared library.
clang: warning: no such sysroot directory: '/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk' [-Wmissing-sysroot]
In file included from file7d712a4a950.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp.h:27:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/RcppCommon.h:29:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/r/headers.h:59:
In file included from /Library/Frameworks/R.framework/Versions/3.6/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:100:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/cmath:305:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/math.h:301:15: fatal error: 'math.h' file not found
#include_next <math.h>
^~~~~~~~
1 error generated.
make: *** [file7d712a4a950.o] Error 1
答案1
macOS 10.14 不再将其头文件安装在旧位置。许多第三方安装都会中断,除非您运行:
sudo installer -pkg \
/Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg \
-target /
也可以看看这个 GitHub 问题。
更新 R 或 macOS(Xcode)时必须重复此命令。
答案2
另一种不需要强制将标题安装在旧位置的解决方案是通过设置以下变量来告诉 R 它们的新位置~/.R/Makevars
:
CFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CCFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
CXXFLAGS=-isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk
看https://thecoatlessprofessor.com/programming/cpp/r-compiler-tools-for-rcpp-on-macos/。