当编译要在包中使用的C代码时,如何将标志传递给R?

前端之家收集整理的这篇文章主要介绍了当编译要在包中使用的C代码时,如何将标志传递给R?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图在R包中使用OpenCV的一些代码,使用Rcpp构建包.当我在我的机器上编译c代码时,工作正常.

例如,我在本地使用以下语法来编译facesetect.cpp代码

g++ `pkg-config --cflags opencv` facedetect.cpp -o facedetect `pkg-config --libs opencv`

但是,当我尝试使用以下命令将其包含在我的包中时:

R CMD SHLIB  facedetect.cpp -o facedetect

在我的makevars文件中定义了以下内容

PKG_CPPFLAGS=  `$(R_HOME)/bin/Rscript -e 'Rcpp:::CxxFlags()'` 
PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"`  
PKG_CXXFLAGS= `pkg-config --cflags opencv` `pkg-config --libs opencv`

R执行以下操作:

g++ -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -I/Library/Frameworks/R.framework/Resources/include/x86_64 `pkg-config --cflags opencv` `pkg-config --libs opencv`  `/Library/Frameworks/R.framework/Resources/bin/Rscript -e 'Rcpp:::CxxFlags()'`  -I/usr/local/include    -fPIC  -g -O2 -c facedetect.cpp -o facedetect.o

这给我以下错误信息:

i686-apple-darwin10-g++-4.2.1: -lopencv_core: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_imgproc: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_highgui: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_ml: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_video: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_features2d: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_calib3d: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_objdetect: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_contrib: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_legacy: linker input file unused because linking not done
i686-apple-darwin10-g++-4.2.1: -lopencv_flann: linker input file unused because linking not done
g++ -arch x86_64 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup     -single_module -multiply_defined suppress -L/usr/local/lib -o facedetect facedetect.o -I/opt/local/include/opencv -I/opt/local/include -L/opt/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_flann -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation

我不明白这些错误信息,因为我没有足够的C经验.有没有人知道如何让R编译C代码,就像我本地的g编译器一样?我不知道“-c”标志是否是问题…不幸的是,我无法通过谷歌或“写入R扩展”手册找到答案.谢谢!

感谢以前的回复者,帮助我弄清楚我在旗帜上遇到的初步问题.

解决方法

您需要将一个Makevars-File放入您的src目录,并指定PKG_CPPFLAGS(预处理程序和包含)和PKG_CXXFLAGS(编译器标志).详细信息请参见“编写R扩展”中的第1.2.1和5.5节.

猜你在找的C&C++相关文章