該賞金過期5天。此問題的答案有資格獲得 50聲望賞金。 AFH希望引起對這個問題的更多關注。
我正在嘗試Rcpp在我的 M1 Mac 上使用R中需要的軟體包,但在購買這臺計算機后我永遠無法啟動和運行。我將它更新到 Monterey,希望這能解決一些安裝問題,但它沒有。我嘗試Rcpp從此頁面運行檢查,但出現以下錯誤:
> Rcpp::sourceCpp("~/github/helloworld.cpp")
ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0'
ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/lib'
ld: library not found for -lgfortran
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sourceCpp_4.so] Error 1
clang -arch arm64 -std=gnu 14 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include -I"/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/RcppArmadillo/include" -I"/Users/afredston/github" -I/opt/R/arm64/include -fPIC -falign-functions=64 -Wall -g -O2 -c helloworld.cpp -o helloworld.o
clang -arch arm64 -std=gnu 14 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/opt/R/arm64/lib -o sourceCpp_4.so helloworld.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0 -L/opt/R/arm64/gfortran/lib -lgfortran -lemutls_w -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
Error in Rcpp::sourceCpp("~/github/helloworld.cpp") :
Error 1 occurred building shared library.
我知道它無法“找到” gfortran。我安裝了這個版本的gfortran為蒙特雷。當我輸入which gfortran終端時,它回傳/opt/homebrew/bin/gfortran. (也許這個版本gfortran需要的 Xcode 工具太新了——它說的是 13.2,當我運行clang --version它時說的是 13.0——但我沒有看到gfortranMonterey 的另一個版本?)
我還附加/opt/homebrew/bin:到PATHR 中,所以現在看起來像這樣:
> Sys.getenv("PATH")
[1] "/opt/homebrew/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:/Applications/RStudio.app/Contents/MacOS/postback"
我檢查的其他事項:
- Xcode 命令列工具已安裝(
which clang回傳/usr/bin/clang)。 - 檔案
~/.R/Makevars和~/.Renviron不存在的。
這是我的會話資訊:
R version 4.1.1 (2021-08-10)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Monterey 12.1
Matrix products: default
LAPACK: /Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/lib/libRlapack.dylib
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] stats graphics grDevices utils datasets methods base
loaded via a namespace (and not attached):
[1] compiler_4.1.1 tools_4.1.1 RcppArmadillo_0.10.7.5.0
[4] Rcpp_1.0.7
更新:嘗試從@Mikael 進行修復后,它仍然失敗,但出現了一個稍微不同的錯誤:
> Rcpp::sourceCpp("~/github/helloworld.cpp")
ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0'
ld: library not found for -lemutls_w
clang-13: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [sourceCpp_2.so] Error 1
/opt/homebrew/opt/llvm/bin/clang -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -target arm64-apple-macos12 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I../inst/include -I"/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/4.1-arm64/Resources/library/RcppArmadillo/include" -I"/Users/afredston/github" -I/opt/homebrew/opt/llvm/include -I/opt/R/arm64/include -fPIC -g -O2 -Wall -pedantic -c helloworld.cpp -o helloworld.o
/opt/homebrew/opt/llvm/bin/clang -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk -target arm64-apple-macos12 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/opt/homebrew/opt/llvm/lib -L/opt/R/arm64/lib -o sourceCpp_2.so helloworld.o -L/Library/Frameworks/R.framework/Resources/lib -lRlapack -L/Library/Frameworks/R.framework/Resources/lib -lRblas -L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0 -L/opt/R/arm64/gfortran/lib -lgfortran -lemutls_w -lm -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
Error in Rcpp::sourceCpp("~/github/helloworld.cpp") :
Error 1 occurred building shared library.
uj5u.com熱心網友回復:
注意:自 OP 的最新(第三)修訂版以來,此答案已得到糾正。他們報告的編譯錯誤,由于
ld: warning: directory not found for option '-L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0'
已通過為蒙特雷適當地設定make變數FLIBS來解決。見下文。
在基于 ARM 的 Mac(以及一般的 macOS)上從源代碼編譯包含 C/C 代碼的 R 包以及使用Rcpp. 主要問題及其修復記錄在R-admin 手冊中,但它們有點分散。
您的近端問題可能比您沒有提到的問題更容易解決,即clangApple 的命令列工具提供的工具鏈不支持 OpenMP。如果您想編譯一個使用多執行緒的 C/C 程式,那么沒有 OpenMP 支持是一個問題。我將首先討論您的近端問題,然后提供一套完整的說明,這些說明應該立即解決所有問題,以便您可以獲得一個完全啟用的工具鏈。
看make變數,而不是PATH
當 R(和Rcpp等)編譯 C/C /Fortran 程式時,它會根據Makefile 中設定的make變數確定要使用的編譯器、編譯器標志、前處理器標志等。指定鏈接 Fortran 代碼所需的庫的正確方法是在 R 知道的 Makefile 中設定make變數FLIBS。makeR 使用的所有變數的系統級默認值都存盤在$(R_HOME)/etc/Makeconf. 您可以使用 查詢它們R CMD config,如下所示:
$ R CMD config FLIBS --no-user-files
用戶可以Makeconf通過創建和修改來覆寫任何設定$(HOME)/.R/Makevars。由于您沒有 a Makevars,因此 R 正在尋找類似的東西
FLIBS=-L/opt/R/arm64/gfortran/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0 -L/opt/R/arm64/gfortran/lib -lgfortran -lemutls_w -lm
中Makeconf,因為這就是 CRAN 配置您已安裝的 R 版本的方式。此配置適用于 Big Sur,而不是 Monterey。如果您設定,您的編譯錯誤應該會消失
FLIBS=-L$(F_DIR)/lib/gcc/aarch64-apple-darwin21/12.0.0 -L$(F_DIR)/lib -lgfortran -lemutls_w -lm
in Makevars,替換$(F_DIR)為您的gfortran安裝路徑。(我假設您實際上是gfortran從這里安裝的。)
我應該在這里強調,環境變數PATH并不會真正影響 R 編譯 C/C /Fortran 程式的方式,除了告訴 R where to find make。也就是說,由
$ which clang clang gfortran
不一定是 R 使用的 C/C /Fortran 編譯器的路徑。
在基于 ARM 的 Mac 上獲取作業工具鏈的說明
為了其他閱讀本文的人的利益,我將假設您是從無到有。隨意跳過您已經采取的步驟,盡管您可能會發現重新開始很有幫助。
在此處從 CRAN 下載 R 二進制檔案并安裝。請務必選擇為 Apple 芯片構建的二進制檔案。
跑
$ xcode-select --install在終端中下載并安裝最新版本的 Xcode,其中包括 Apple 的命令列工具。您也可以在此處從您的瀏覽器下載 Xcode 。早期版本的 Xcode 可在此處獲得,但我將從最新版本開始。(可能有充分的理由安裝 Xcode 的確切版本,而不是 CRAN 用于構建 R 二進制檔案的版本,并且該版本可能不是最新的。不確定。)
使用 Homebrew安裝LLVM
clang工具鏈。與 Apple 不同clang,它支持 OpenMP。$ brew update $ brew install llvm它應該解壓成
/opt/homebrew/opt.下載
gfortran為您的 macOS 版本和架構構建的二進制檔案,然后安裝。此步驟因 macOS 版本而異:Monterey:下載此處托管的磁盤映像檔案并
open進行安裝。安裝程式會將二進制檔案解壓到/usr/local; 您應該將安裝移動到/opt/R/arm64.$ sudo mkdir -p /opt/R/arm64/bin $ sudo mv /usr/local/gfortran /opt/R/arm64 $ sudo mv /usr/local/bin/gfortran /opt/R/arm64/bin $ sudo ln -sfn /opt/R/arm64/gfortran/bin/gfortran /opt/R/arm64/bin/gfortranBig Sur:下載此處托管的 tarball并直接解壓縮到
/opt/R/arm64.$ sudo mkdir -p /opt/R/arm64 $ sudo tar xvf path/to/tarball -C /opt/R/arm64將以下行添加到
$(HOME)/.R/Makevars,如有必要,創建檔案。LLVM_DIR=/opt/homebrew/opt/llvm LIBS_DIR=/opt/R/arm64 F_DIR=$(LIBS_DIR)/gfortran SDK_DIR=/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk CC=$(LLVM_DIR)/bin/clang -isysroot $(SDK_DIR) -target arm64-apple-macos12 CXX=$(LLVM_DIR)/bin/clang -isysroot $(SDK_DIR) -target arm64-apple-macos12 FC=$(F_DIR)/bin/gfortran -mtune=native CFLAGS=-falign-functions=8 -g -O2 -Wall -pedantic -Wno-implicit-function-declaration CXXFLAGS=-g -O2 -Wall -pedantic FFLAGS=-g -O2 -Wall -pedantic SHLIB_OPENMP_CFLAGS=-fopenmp SHLIB_OPENMP_CXXFLAGS=-fopenmp SHLIB_OPENMP_FFLAGS=-fopenmp CPPFLAGS=-I$(LLVM_DIR)/include -I$(LIBS_DIR)/include LDFLAGS=-L$(LLVM_DIR)/lib -L$(LIBS_DIR)/lib FLIBS=-L$(F_DIR)/lib/gcc/aarch64-apple-darwin21/12.0.0 -L$(F_DIR)/lib -lgfortran -lemutls_w -lmCC,CXX, 并且FLIBS需要在 Big Sur 上進行調整:CC=$(LLVM_DIR)/bin/clang -isysroot $(SDK_DIR) -target arm64-apple-macos11 CXX=$(LLVM_DIR)/bin/clang -isysroot $(SDK_DIR) -target arm64-apple-macos11 FLIBS=-L$(F_DIR)/lib/gcc/aarch64-apple-darwin20.2.0/11.0.0 -L$(F_DIR)/lib -lgfortran -lemutls_w -lmRun R and test that you can compile a program with OpenMP support. For example:
if (!requireNamespace("Rcpp", quietly = TRUE)) { install.packages("Rcpp") } if (!requireNamespace("RcppArmadillo", quietly = TRUE)) { install.packages("RcppArmadillo") } Rcpp::sourceCpp(code = ' #include <RcppArmadillo.h> #ifdef _OPENMP # include <omp.h> #endif // [[Rcpp::depends(RcppArmadillo)]] // [[Rcpp::plugins(openmp)]] // [[Rcpp::export]] void omp_test() { #ifdef _OPENMP Rprintf("OpenMP threads available: %d\\n", omp_get_max_threads()); #else Rprintf("OpenMP not supported\\n"); #endif } ') omp_test()OpenMP threads available: 8If this fails to compile, or if it compiles without error but you get the message saying that OpenMP is not supported, then something is wrong. Let me know if either happens.
uj5u.com熱心網友回復:
我通過~/.R/Makevars按照以下說明添加 gfortran 的自制安裝路徑來解決此問題:https : //pat-s.me/transitioning-from-x86-to-arm64-on-macos-experiences-of-an- r-用戶/#gfortran
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/409424.html
標籤:
上一篇:使用MTLSharedEvent在單個命令緩沖區內同步CPU和GPU之間的作業
下一篇:如何保存用戶可以訪問的動態檔案?
