Jawad
Jawad

Reputation: 171

Calling R functions from VC++

I need to run statistical functions from R within my C++ code. Is there a way to call them from my C++ code? I'm using VS 2005, do I need to include any header files or link any libraries? I installed R using the standard Windows installer provided from their website.

Regards,

Upvotes: 5

Views: 1640

Answers (2)

Borodin Valeria
Borodin Valeria

Reputation: 1

The binary version of Rserve as provided on https://rforge.net/Rserve/files/ does not build under Microsoft Visual C++. In the file ReadMe.txt, the author indicates that there is no configure for Windows, but there is a special Makefile.win. We did not success to build it, by using "make -f Makefile.win", since no file config.h is provided (only config.h.in).

  1. So, comment the following line in the header file Rsrv.h:


    // # include " config .h"


At this stage, the following error is generated :


rsrv .h (384): fatal error C1189 : # error : " Cannot determine endianness. Make sure config .h is included or _ _{BIG| LITTLE } _ENDIAN__ is defined ."


  1. Solve the problems related to endianness. In the header file Rsvr.h, replace :

/* Windows is little-endian is most cases, anywhere else we're stuck*/
 # error "Cannot determine endianness. Make sure config.h is included or  __{BIG|LITTLE}_ENDIAN__ is defined ."
# endif

by:


/********* MODIFICATION *************/

/* # error "Cannot determine endianness. Make sure config.h is included or __{BIG|LITTLE}_ENDIAN__ is defined ." */
/************************************/
#define _BIG_ENDIAN (*(uint16_t *)?\xff? < 0x100)

  1. Ensure that the build environment links to the Winsock Library file Ws2_32.lib. To do this, add the following code lines before the main function :

/* indicate to the linker that the Ws2_32.lib file is needed */
#pragma comment(lib, "Ws2_32.lib") 

Applications that use Winsock must be linked with the Ws2_32.lib library file.

For more details about R and Microsoft Visual C++ integration via Rserve, visit https://www.researchgate.net/publication/305789407_Rserve_for_Windows_case_of_R_and_Microsoft_Visual_C_integration

Upvotes: -1

Dirk is no longer here
Dirk is no longer here

Reputation: 368201

Briefly:

  • As has been stated fairly frequently, R itself does not build under VS* making the linking of C code hard-but-possible and C++ code impossible (as function header information is not standardized).

  • Both Rcpp and RInside work perfectly well on Windows, given the standard Windows toolchain. (RInside did have a bug but this is now fixed.)

    The Rcpp FAQ has more to say about VS* (ie that you cannot expect this to work if R itself can't work with the compiler)

  • If you must use VS* then your best bet may be looser coupling via networking and using Rserve.

    If you can switch compilers then Rcpp / RInside can be of interest; and the rest of the R API is also at your disposal.

  • Rcpp et al have copious documentation, including an Rcpp-introduction pdf (which is also peer-reviewed article) and the aforementioned Rcpp-FAQ --- as well as a dedicated mailing list.

And please do not cross-post. I also just answered this on r-help.

Upvotes: 4

Related Questions