Class Library calling R Language in C ++

Source: Internet
Author: User

1. Install R and download the source code of R. Click to download the official version of R.
Install R: download and run r-2.10.1-win32.exe from r's website, remember the installation path such as C: \ Program Files \ R \ R-2.15.2.
Download r-2.15.2.tar.gz from r's website, decompress it, copy all the extracted Files to the installation path C: \ Program Files \ R \ R-2.15.2, and select overwrite duplicate Directories
2. Install the corresponding version of Rtools
Run rtools210.exe, select the default installation path, and install all components. Note: select to change the system environment variables.
3. Use MinGW to compile Rmath
3.1 generate Rmath. dll
Use cmd to enter the installation path under C: \ Program Files \ R \ R-2.15.2
Cd C: \ Program Files \ R \ R-2.15-2
Cd src \ include www.2cto.com
Make-f Makefile. win config. h Rconfig. h Rmath.
If an error is reported, ../gnuwin32/MkRules cannot be found. In the corresponding directory, there is a file with the same name. Remove the suffix.
If an rmath. h file cannot be found, copy the file from include to src/include.
Cd ../nmath/standalone
Make-f Makefile. win
Then we can see a large string of compilation output, and see Rmath. dll and Rmath. def in the standalone folder.
 
3.2 generate Rmath. dll
First, to use pexports, install the MinGW plug-in:
(If NO environment variable is set, the command cannot be found :)
Download mingw-utils-0.3.tar.gz (pexports here) and decompress it directly to c: \ Rtools \ MinGW. Run:
 
Pexports Rmath. dll> Rmath. exp
Lib/def: Rmath. exp/out: Rmath. lib
// It seems that the lib command will change the Rmath. exp file, so it can only be executed once. Otherwise, the lib file generated for the second time is incorrect.
// The MACHINE switch is also very important. It must be consistent with the setting of the c ++ project. Otherwise, the load error will be reported during compilation.
 
(Note 1:
 
Here, lib is the command of visual studio C ++. If you cannot find this command, add the bin that installs visual studio C ++ to the environment variable PATH. The method is as follows:
Add C: \ Program Files \ Microsoft Visual Studio 9.0 \ VC \ bin to the Path.
 
NOTE 2: If "mspdb80.dll is not found after running ..... "
 
Copy the file in the visual studio C ++ installation directory, such as msobj80.dll1_mspdb80.dll1_mspdbsrv.exe under C: \ Program Files \ Microsoft Visual Studio 9.0 \ Common7 \ IDE \ in my system to C: \ Program Files \ Microsoft Visual Studio 9.0 \ VC \ bin)
Then we get Rmath. lib.
 
4. Call the Rmath library in the visual C ++ Project
4.1 Build a project
 
Create an empty project on the win32 console, and add Rmath to the project Linker> Additional Dependencies. lib, set Rmath. copy the dll to the project directory and add the following main. cpp to compile and run it:
 
Main. cpp content:
# Define MATHLIB_STANDALONE 1 // macro definition is required, refer to Rmath. h
# Include <Rmath. h>
# Include <stdio. h>
Int main (int argc, char ** argv)
{
Printf ("hello \ n ");
Double x = fmax2 (3, 2 );
Double y = 0;
Printf ("x = % lf", x );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.