I am trying to use some code from OpenCV开发者_如何学Python in an R package, using Rcpp to build the package. When I compile the c code on my machine, it works fine.
For example, I am using the the following syntax locally to compile the facedetect.cpp code:
g++ `pkg-config --cflags opencv` facedetect.cpp -o facedetect `pkg-config --libs opencv`
However, when I try to include it in my package using the following command:
R CMD SHLIB facedetect.cpp -o facedetect
with the following defined in my makevars file:
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 executes the following:
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
which gives me the following error messages:
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
I do not understand these error messages, because I do not have enough experience with C++. Does anyone know how to get R to compile the C++ code as my local g++ compiler does? I'm not sure if the "-c" flag is the problem... Unfortunately I could not find the answer via google or the Writing R Extensions manual. Thanks!
Thanks to previous responders who helped me figure out the initial problems I was having with the flags.
You need to put a Makevars
-File into your src directory and specify PKG_CPPFLAGS
(preprocessor & includes) and PKG_CXXFLAGS
(compiler flags). Details are in sections 1.2.1 and 5.5 in "Writing R Extensions".
This is a Makevars
file that actually worked for me:
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`
Hope this helps.
精彩评论