本文介绍了试图用Rcpp编译c ++ 11时的g ++错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

系统规格:


  • 操作系统 - Mac OS X 10.6.8 (Snow Leopard)

  • g ++ - Macports gcc 4.8.1_2 + universal

  • - 2.15.3

  • Rcpp - 0.10.3

  • OS - Mac OS X 10.6.8 (Snow Leopard)
  • g++ - Macports gcc 4.8.1_2+universal
  • R - 2.15.3
  • Rcpp - 0.10.3

我一直收到一个错误,当我试图编译使用C ++ 11在R(通过Rcpp)的函数 - 由于某种原因,g ++不认识 -std = c ++ 11 选项。

I keep on receiving an error when I am trying to compile functions that use C++11 in R (through Rcpp) - for some reason, g++ does not recognise -std=c++11 option.

此示例取自Rcpp帮助文件(它不包含任何特定于C ++ 11的,但可以显示我的问题) 。如果我尝试运行:

This example is taken from Rcpp help files (it does not contain anything specific to C++11, but can show what my problem is). If I try running:

require( Rcpp )
Sys.setenv( "PKG_CXXFLAGS"="-std=c++11" )
cppFunction(plugins=c("cpp11"), '
int useCpp11() {
    int x = 10;
    return x;
}')

我得到:

cc1plus: error: unrecognized command line option "-std=c++11"
make: *** [file61239328ae6.o] Error 1
g++ -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/x86_64 -DNDEBUG  -I/usr/local/include  -I"/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include"   -std=c++11  -fPIC  -g -O2  -c file61239328ae6.cpp -o file61239328ae6.o
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput,  :
  Error 1 occurred building shared library.

同时,我可以直接从bash编译这个函数 - 如果这个代码在 useCpp11.cpp 文件,那么这个运行没有任何投诉:

At the same time, I can compile this function directly from bash - if this code is in useCpp11.cpp file, then this runs without any complaints:

g++ useCpp11.cpp -std=c++11

当然,我做错了,但我不能解决它是什么。 gcc 4.8被设置为bash中的默认编译器,Rcpp过去一直没有故障工作。我怀疑我不告诉R哪个版本的g ++使用 - 可能是这种情况?

Certainly, I am doing something wrong, but I cannot work out what it is. gcc 4.8 is set as a default compiler in bash, Rcpp has been working without fault in the past. I suspect that I am not telling R which version of g++ to use - could that be the case?

推荐答案

Kevin Ushley绝对右键 - 确保使用正确编译器的最简单方法是通过 Makevars 文件。在我的例子中,我添加了:

Kevin Ushley is absolutely right - the easiest way to make sure that the right compiler is being used is through Makevars file. In my case, I added:

CXX = g++-4.8.1
PKG_CXXFLAGS = -std=c++11

这是我一直遗漏的 - 如果您正在编译您的包,这个工作。

This is what I have been missing - and it all worked afterwards. This works if you are compiling your package.

这篇关于试图用Rcpp编译c ++ 11时的g ++错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 12:13