Click here to Skip to main content
14,423,206 members
Rate this:
Please Sign up or sign in to vote.
See more:
R does not  get compiled into intermediate code. Can any one help on how to integrate R with C++.

What I have tried:

Tried running sample code to call a method from R script. Failed with linker errors.
Updated 20-Jun-17 20:30pm
Mohibur Rashid 19-Jun-17 3:05am
have you tried google?
AshwiniKJ 19-Jun-17 7:39am
I have a separate R script that is reading CSV and performing the task. Now I need to call that script/method from that script directly from my C++ code.

This is how I tried,

#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#include "Rinternals.h"
#include "Rembedded.h"

SEXP hello() {
return mkString("Hello, world!\n");

int main(int argc, char **argv) {
Rf_initEmbeddedR(argc, argv);
x = hello();
return 0; //x == NULL; /* i.e. 0 on success */

Geting following linker error:

error LNK2019: unresolved external symbol _Rf_initEmbeddedR referenced in function _main
Richard MacCutchan 19-Jun-17 7:42am
You need to add the R library that provides this function into your linker options, in the project parameters.
AshwiniKJ 19-Jun-17 7:48am
Hi Richard,

I guess the R library does not get built while installing R in windows. Can you please guide me on how to build this library. I saw R.dll in C:\Program Files\R\R-3.3.2\bin\i386 folder. But could not find the corresponding library. I am using R on windows 2008 OS and Visual Studio for C++ compilaton.
Richard MacCutchan 19-Jun-17 10:20am
R.dll is the library. I guess you need to go and study the documentation to find out what needs to be done to link to it.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

We can call a R script from C++ code as below,

#include "stdafx.h"
#include <iostream>
using namespace std;

int main(int argc, char **argv) {
system("C:\\\"Program Files\"\\R\\R-3.3.2\\bin\\rscript C:\\R-Scripts\\test_script.R");

return 0;

rscript is the command that executes the test_script.R.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100