Majobber
Majobber

Reputation: 45

"undefined reference to " error when compiling C project with GNU GCC Compiler

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.

Upvotes: 0

Views: 1175

Answers (1)

fishtank
fishtank

Reputation: 3728

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

Upvotes: 7

Related Questions