我正在尝试在 R 中编译 C++ 代码,以便可以从源代码安装包。我在 Mac OS X Catalina (10.15.6) 上运行 R 4.0.2。我已按照此处Rcpp
的说明在 Mac OS 上安装 R 编译器工具。但是,按照说明操作时,尝试在 R 中编译以下 C++ 代码(另存为 )时收到错误消息/Users/itpetersen/Desktop/helloworld.cpp
:
#include <RcppArmadillo.h>
// [[Rcpp::depends(RcppArmadillo)]]
// [[Rcpp::export]]
void hello_world() {
Rcpp::Rcout << "Hello World!" << std::endl;
}
// After compile, this function will be immediately called using
// the below snippet and results will be sent to the R console.
/*** R
hello_world()
*/
这是我尝试在 R 中编译上述 C++ 代码时收到的错误:
> Rcpp::sourceCpp('/Users/itpetersen/Desktop/helloworld.cpp')
helloworld.cpp:1:10: fatal error: 'RcppArmadillo.h' file not found
#include <RcppArmadillo.h>
^~~~~~~~~~~~~~~~~
1 error generated.
make: *** [helloworld.o] Error 1
clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Packages/Rcpp/include" -I"/Users/itpetersen/Desktop" -I/usr/local/include -fPIC -Wall -g -O2 -c helloworld.cpp -o helloworld.o
Error in Rcpp::sourceCpp("/Users/itpetersen/Desktop/helloworld.cpp") :
Error 1 occurred building shared library.
尝试从源代码编译包时,我也收到错误mirt
消息:
> install.packages("mirt", type = "source")
Installing package into ‘/Library/Frameworks/R.framework/Packages’
(as ‘lib’ is unspecified)
trying URL 'https://cran.r-project.org/src/contrib/mirt_1.32.1.tar.gz'
Content type 'application/x-gzip' length 811759 bytes (792 KB)
==================================================
downloaded 792 KB
* installing *source* package ‘mirt’ ...
** package ‘mirt’ successfully unpacked and MD5 sums checked
** using staged installation
** libs
clang++ -mmacosx-version-min=10.13 -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Packages/Rcpp/include' -I'/Library/Frameworks/R.framework/Packages/RcppArmadillo/include' -I/usr/local/include -fPIC -Wall -g -O2 -c Estep.cpp -o Estep.o
In file included from Estep.cpp:1:
In file included from ./Misc.h:4:
In file included from /Library/Frameworks/R.framework/Packages/RcppArmadillo/include/RcppArmadillo.h:31:
/Library/Frameworks/R.framework/Packages/RcppArmadillo/include/RcppArmadilloForward.h:26:10: fatal error: 'RcppCommon.h' file not found
#include <RcppCommon.h>
^~~~~~~~~~~~~~
1 error generated.
make: *** [Estep.o] Error 1
ERROR: compilation failed for package ‘mirt’
* removing ‘/Library/Frameworks/R.framework/Packages/mirt’
Warning in install.packages :
installation of package ‘mirt’ had non-zero exit status
The downloaded source packages are in
‘/private/var/folders/p5/tkdpnffs2_g5ybr2t1yq1nd0rq_9pt/T/RtmpHLa2q1/downloaded_packages’
这是我所做的:
- 我在以下位置删除了文件:
~/.R/Makevars
- 我在以下位置删除了文件:
~/.Renviron
- 我安装了
Xcode
11.6 版 - 我通过输入终端验证了
Xcode
安装。gcc --version
这是输出:
gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1
Apple clang version 11.0.3 (clang-1103.0.32.62)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
- 我从这里
gfortran
安装了8.2 版 - 我在 R 中安装了
Rcpp
和RcppArmadillo
包 - 我还尝试将文件夹中的内容复制到
include
文件RcppArmadillo
夹中的include
文件Rcpp
夹中(如此处所建议),但在尝试编译 C++ 代码时继续收到错误(尽管是不同的错误) - 我也在这里尝试了 StackOverflow 上类似问题的解决方案,但他们没有解决问题。请注意,我使用的 R 版本比其他问题更新,因此我的问题不应被视为重复问题。
这是我的会话信息:
> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS Catalina 10.15.6
Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/4.0/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
other attached packages:
[1] RcppArmadillo_0.9.900.2.0 Rcpp_1.0.5
loaded via a namespace (and not attached):
[1] compiler_4.0.2 tools_4.0.2 tinytex_0.25 xfun_0.16