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

时间:2011-03-15 05:42:21

标签: c++ r opencv rcpp

我试图在R包中使用OpenCV中的一些代码,使用Rcpp构建包。当我在我的机器上编译c代码时,它工作正常。

例如,我在本地使用以下语法来编译facedetect.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扩展手册找到答案。谢谢!

感谢之前的响应者,他们帮助我弄清楚我遇到的最初问题。

2 个答案:

答案 0 :(得分:5)

您需要将Makevars - 文件放入src目录并指定PKG_CPPFLAGS(预处理器和包括)和PKG_CXXFLAGS(编译器标志)。详细信息请参见“编写R扩展”中的1.2.1和5.5节。

答案 1 :(得分:4)

这是一个实际适用于我的Makevars文件:

PKG_LIBS = `pkg-config --libs opencv`
PKG_CFLAGS = `pkg-config --cflags opencv`
PKG_CXXFLAGS = `pkg-config --cflags opencv` `Rscript -e 'Rcpp:::CxxFlags()'`
PKG_CFLAGS = `pkg-config --cflags opencv`

希望这有帮助。