Compilation steps for a c++ file with Rcpp header file - c++

I am trying this code on gedit and compiling by g++ compiler on terminal.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double sumC(NumericVector x) {
int n = x.size();
double total = 0;
for(int i = 0; i < n; ++i) {
total += x[i];
}
return total;
}
// [[Rcpp::export]]
double meanC(NumericVector x) {
return sumC(x) / x.size();
}
Error occurred for the header file.
fatal error: Rcpp.h: No such file or directory
I have compiled like this: g++ -I /usr/ r1.cpp -o c0 -L /usr/ -lRcpp
Also i tried :g++ -I /usr/lib/R/site-library/Rcpp/include/ r1.cpp -o c0 -L /usr/lib/R/site-library/Rcpp/libs/ -lRcpp . THen got error like fatal
error: R.h: No such file or directory #include <R.h>
Locations:
locate Rcpp.h:/usr/lib/R/site-library/Rcpp/include/Rcpp.h
locate R.h:/usr/share/R/include/R.h
I have tried with make file also.
My make file:
all:
g++ rcpp.cpp -o obj
compile:
I have attached all the depending header files in a single folder. Still getting the errors for Rcpp.
Any one knows how to compile this through terminal?

You can compile this file with
g++ -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include -c rcpp.cpp -o rcpp.o
However, I do not understand why you want to do this. In order to make such C++ functions callable from R, several additional steps are necessary:
C++ wrapper functions that translate to an interface based on R's SEXP.
R wrapper functions that call the C++ wrapper functions via .Call().
Linking of all the object files into a dynamic library that R can load.
Loading the library and the R wrapper functions into R.
All this is automated via sourceCpp() or when using Rcpp::compileAttributes() in the context of packages using Rcpp, c.f. the vignettes on attributes and packages.

Related

C++, can't access included header files from so library

I try to include my self built .so library in the test.cpp file.
When I try to make the test.cpp file I get this exception:
root#airdrop:/home/pi/naza-interface/examples# make
g++ -c test.cpp
test.cpp:31:35: fatal error: naza_interface_manual.h: No such file or
directory
#include "naza_interface_manual.h"
^
compilation terminated.
Makefile:5: recipe for target 'test.o' failed
make: *** [test.o] Error 1
The Makefile for test.cpp looks like that:
output: test.o
g++ -L. -lnazainterface -o test test.o
test.o:
g++ -c test.cpp
clean:
rm -f *.o
test.cpp just includes the library.
#include "naza_interface_manual.h"
The library contains two files, naza_interface_manual.h and naza_interface_manual.cpp. The library's makefile looks like that:
libso: naza_interface_manual.o pca9685.o
g++ -fPIC -L/usr/local/lib -shared naza_interface_manual.o
pca9685.o -lbcm2835 -o libnazainterface.so
naza_interface_manual.o: src/naza_interface_manual.cpp src/naza_interface_manual.h
g++ -fPIC -c -Wall src/naza_interface_manual.cpp
pca9685.o: src/PCA9685/pca9685.cpp src/PCA9685/pca9685.h
g++ -c src/PCA9685/pca9685.cpp
install: naza_interface_manual.o pca9685.o
g++ -L/usr/local/lib naza_interface_manual.o pca9685.o -lbcm2835 -
shared -o /usr/local/libnazainterface.so
clean:
rm *.o output
naza_interface_manual.h:
#ifndef NAZA_INTERFACE_MANUAL_H_
#define NAZA_INTERFACE_MANUAL_H_
class naza_interface_manual_c{
public:
naza_interface_manual_c();
// A: For roll control (left/right)
// E: For pitch control (front/back)
// T: For throttle control
// R: For rudder control
// U: For Control Model Switch
void configure_pins(int A, int E, int T, int R, int U);
void fly_forward(int speed);
void fly_backward(int speed);
void fly_up(int speed);
void fly_down(int speed);
void fly_left(int speed);
void fly_right(int speed);
};
#endif
naza_interface_manual.cpp:
#include <iostream>
#include <wiringPi.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include "naza_interface_manual.h"
#include "PCA9685/pca9685.h"
naza_interface_manual_c::naza_interface_manual_c(){
std::cout << "Starting Naza Interface";
}
void naza_interface_manual_c::configure_pins(int A, int E, int T, int R, int U){
PCA9685 pca9685;
pca9685.SetFrequency(100);
pca9685.Write(CHANNEL(0), VALUE(350));
}
void naza_interface_manual_c::fly_forward(int speed){
}
void naza_interface_manual_c::fly_backward(int speed){
}
void naza_interface_manual_c::fly_up(int speed){
}
void naza_interface_manual_c::fly_down(int speed){
}
void naza_interface_manual_c::fly_left(int speed){
}
void naza_interface_manual_c::fly_right(int speed){
}
Your Makefile doesn't install the header file. In fact, it also installs the shared object in a non-standard location: /usr/local. You want the library to go into /usr/local/lib and you need the header file installed in /usr/local/include.
Your Makefile is not consistent with conventional rules: You have no all rule, you are creating the library directly in the installation directory, instead of calling /usr/bin/install... I suggest you look into "proper" Makefile layout, if you want to distribute this. Users expect a lot of things from the Makefiles you give them; there are de-facto standards to follow.
If you want to use the library without having installed it, you need to provide the compiler the relevant include directive in your test.o: target; something like -Ipath/to/your/header.
Your compilation doesn't give the compiler the include path to find the header.
Instead, specify a base location and add the path to the compile. Otherwise if you can change the naza interface library, its install target should install the headers to a system (or $PREFIX/include) location.
test.o:
g++ -I$(NAZA_INTERFACE_LIB)/src/ -c test.cpp

troubles with compiling a c++ file including exprTk

I have some troubles compiling a c++ code including exprtk. I want to compile an given example of the package (I called it parser.cpp):
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void trig_function()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
std::string expression_string = "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";
T x;
symbol_table_t symbol_table;
symbol_table.add_variable("x",x);
symbol_table.add_constants();
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_string,expression);
for (x = T(-5); x <= T(+5); x += T(0.001))
{
T y = expression.value();
printf("%19.15f\t%19.15f\n",x,y);
}
}
int main()
{
trig_function<double>();
return 0;
}
Therefore I use the following commands in cmd:
g++ -c -o parser.o -Wa,-mbig-obj -I include parser.cpp
g++ -o parser.exe -s parser.o
The exprtk.hpp file is in an include folder in the same directiory as the parser.cpp file.
The first problem is, that the parser.o file is very large (~ 32 Mb) and creating the .exe file take such a long time that I abort the compilation. Furthermore without the -Wa,-mbig-obj flag I get an error. Also I think there is no linking needed because all the code is included in the .hpp file and there are no .dll files or something else. By dropping the flag the error is:
C:/Rtools/mingw_64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.3/../../../../x86_64-w64-mingw32/bin/as.exe:
parser.o: too many sections (88691)
C:\..\AppData\Local\Temp\ccE7ythI.s: Assembler messages:
C:\..\AppData\Local\Temp\ccE7ythI.s: Fatal error: can't write parser.o: File too big
C:/Rtools/mingw_64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.3/../../../../x86_64-w64-mingw32/bin/as.exe: parser.o:
too many sections (88691)
C:\..\AppData\Local\Temp\ccE7ythI.s: Fatal error:
can't close parser.o: File too big
The source is given via GitHub here. The same error as above occurs if I run make in cmd in the folder where the Makefile is.
Do I miss something or am I too foolish importing the exprtk.hpp file correctly? Any suggestions?
You have to have the -mbig-obj flag, which allows for big object files.
As explained here, .obj files have 65536 sections by default.
Exprtk is one big template heavy library (hpp is 1.4M) so it will take a long time to compile and need lots of sections in the object file.

Linking Intel MKL with Armadillo C++ Matrix Library

I have been having a number of issues with linking Intel MKL with Armadillo C++.
Im trying to do this on OSX.
I have installed Intel parallel_studio_xe_2016.3.068 as root so that its available across the whole system.
Here is my example code that I'm trying to run:
example.cpp
#include <iostream>
#include <armadillo>
using namespace std;
using namespace arma;
int main(int argc, const char * argv[]) {
// mkl_set_num_threads(4);
mat A = randu<mat>(1,2);
mat B = randu<mat>(2,1);
mat D;
double C = 0;
while (C == 0){
cout << A*B;
}
return 0;
}
I downloaded the armadillo 7.300.1 tar from sourceforge, unpacked it ran the following commands in terminal:
cd armadillo-7.300.1
cmake .
make
And then to compile my cpp file I used the link line advisor on intel site to get all the details I needed.
icpc -DMKL_ILP64 -qopenmp -I/opt/intel/mkl/include -I~/GithubRepos/PMIWork/armadillo-7.300.1/include -DARMA_DONT_USE_WRAPPER -L/opt/intel/mkl/lib -Wl,-rpath,/opt/intel/mkl/lib -lmkl_intel_ilp64 -lmkl_core -lmkl_intel_thread -lpthread -lm -ldl example.cpp -o example
However when I run the resulting executable I get a segmentation fault. If I remove the cout << then the code complies, runs but it only uses 1 single core. I thought that MKL is suppose to automatically scale to the whole CPU.
Would really appreciate any help with this.

Programming Error, New to C++

I'm taking an intro class into C++ and we are using jgrasp. I'm doing a simple exercise right now which looks like this:
#include <iostream>
using namespace std;
int main()
{
cout << "Testing 1, 2, 3\n";
return 0;
}
though when I compile it and link it I get this error:
----jGRASP exec: g++ -c -fsyntax-only H:\COP2334\Excerises\Display 1.10
g++.exe: warning: H:\COP2334\Excerises\Display 1.10: linker input file unused because linking not done
----jGRASP: operation complete.
The option -c means "Compile or assemble the source files, but do not link."
So, of course, linking isn't performed.

sourceCpp from Rcpp giving weird compilation error on canonical example

I am getting the following strange error:
> sourceCpp( "comp.Cpp" )
Warning message:
In sourceCpp("comp.Cpp") :
No Rcpp::export attributes or RCPP_MODULE declarations found in source
when I use sourceCpp. The "comp.Cpp" file looks like this:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp:export]]
RcppExport SEXP comp(int n){
int i;
Rcpp::NumericVector product(n);
for(i=0;i<n;i++){
product[i]=i;
}
return(product);
}
I tried updating my operating system to Maverick (and then had to reinstall Xcode command line tools and some other things) but this error predates that. I can make the test package and install it and run the hello world it provides, so the Rcpp package is mostly working. I also get another error from running in R:
cppFunction( "
int useCpp11() {
auto x = 10;
return x;
}
", plugins=c("cpp11" ) )
which is
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -std=c++11 -fPIC -mtune=core2 -O3 -c file69810a85a0d.cpp -o file69810a85a0d.o
Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput, :
Error 1 occurred building shared library.
cc1plus: error: unrecognized command line option "-std=c++11"
make: *** [file69810a85a0d.o] Error 1
I don't know if these two things are related. I think something is happening with my compiler not playing well with attributes, but hunting around the internet hasn't educated me sufficiently to understand that.
Any help would be greatly appreciated.
Change "[[Rcpp:export]]" by "[[Rcpp::export]]".
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
SEXP comp(int n){
int i;
Rcpp::NumericVector product(n);
for(i=0;i<n;i++){
product[i]=i;
}
return(product);
}
Your compiler is too old for the C++11 flag. And the error message is very clear about it.
Try -std=c++0x as well as upgrading to Xcode 5 (which has its own set of issues -- but those are well documented here).