0
votes

I'm hoping someone can assist. I'm new to C, and I've been experimenting with some code from the R project. The project I've created consists of a main.c file and several header files. The main.c file looks as follows:

#include "Rinternals.h"
int main(int argc, char** argv) {     
}
SEXP Cdqrls(SEXP x, SEXP y, SEXP tol, SEXP chk)
{   
SEXP ans;
SEXP qr, coefficients, residuals, effects, pivot, qraux;
int n, ny = 0, p, rank, nprotect = 4, pivoted = 0;
double rtol = asReal(tol), *work;   
}

The Rinternals.h file is this file from the R Project source: https://svn.r-project.org/R/trunk/src/include/Rinternals.h

I've included this header file in the same folder as the main.c file, and I've also included in this folder all the header files that are in the Rinternals.h #includes statements. When I attempt to compile this, I get the following error:

||=== Build: Debug in MyNewProject (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `Cdqrls':|
main.c|30|undefined reference to `Rf_asReal'|

I've tried a number of things, and can't seem to find a way to get this code to compile. I'm currently working in Code::Blocks and compiling with GNU GCC Compiler. Any help with this would be greatly appreciated.

1
Did you remember to link with R? Merely including the header file is not enough for the compiler to link with the library. - fuz
Are you sure you want to use C, not C++ and are you sure you want to work on SEXP, not Rcpp (much easier)? And how do you compile it, from console? Because best way is to create an R package, load it in RStudio and click Build & Reload. - bartoszukm

1 Answers

7
votes

You need to link it with R library (also use -I to specify include library):

cc -O2 code.c -o code -I/usr/share/R/include -L/usr/lib/R/lib -lR