Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass flags to R when it is compiling C++ code to be used in a package?

Tags:

c++

r

opencv

rcpp

I am trying to use some code from OpenCV 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.

like image 259
Solomon Avatar asked Mar 15 '11 05:03

Solomon


People also ask

How do you add compiler flags in code blocks?

Open your project and then go Project > Build Options > Compiler Flags . You can tick boxes in the "Compiler Flags" tab, and you can write other options in the "Other Options" tab. Do one or the other, e.g. don't tick the "-std=c++98" box and also put "-std=c++11" in the Other Options.

What are compilation flags in C?

Compiler flags are options you give to gcc when it compiles a file or set of files. You may provide these directly on the command line, or your development tools may generate them when they invoke gcc. This section describes just the flags that are specific to Objective-C.

What does the flag do in compilation?

Compile-time flags are boolean values provided through the compiler via a macro method. They allow to conditionally include or exclude code based on compile time conditions. There are several default flags provided by the compiler with information about compiler options and the target platform.

Can R code be compiled?

In fact, you can take any R script and compile it into a report that includes commentary, source code, and script output. Reports can be compiled to any output format including HTML, PDF, MS Word, and Markdown. The first call to render creates an HTML document, whereas the second creates a PDF document.


1 Answers

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".

like image 63
fabians Avatar answered Oct 09 '22 04:10

fabians