From edd at debian.org  Fri Jun  1 03:12:16 2007
From: edd at debian.org (Dirk Eddelbuettel)
Date: Thu, 31 May 2007 20:12:16 -0500
Subject: [Rd] Problems when linking to R shared library
In-Reply-To: <007501c7a3ae$25c2f840$38540352@fuji>
References: <007501c7a3ae$25c2f840$38540352@fuji>
Message-ID: <18015.29296.520924.232709@basebud.nulle.part>


On 31 May 2007 at 19:04, John Maddock wrote:
| Folks,
| 
| I'm fairly sure that I'm doing something stupid, but I'm getting a few
| really strange results from *some* of the distributions, but by no means 
| all,
| when I link directly to the R shared library.

When I try to build your program, I get very immediate and explicit messages:

edd at basebud:~> g++ -Wall -O3 -I/usr/share/R/include -o /tmp/johnmaddock /tmp/johnmaddock.cc -lRmath
/tmp/ccb5T7l9.o: In function `main':
johnmaddock.cc:(.text+0xae): undefined reference to `Rf_pf'
johnmaddock.cc:(.text+0xed): undefined reference to `Rf_pchisq'
johnmaddock.cc:(.text+0x136): undefined reference to `Rf_pgamma'
johnmaddock.cc:(.text+0x17a): undefined reference to `Rf_ppois'
johnmaddock.cc:(.text+0x1b9): undefined reference to `Rf_qchisq'
johnmaddock.cc:(.text+0x202): undefined reference to `Rf_qgamma'
collect2: ld returned 1 exit status

which shows that the compiler/linker do not yet know about the functions you
use. 

Your mistake was to not do 

#define MATHLIB_STANDALONE 1

before the #include of Rmath.h.  After that, all is well:

edd at basebud:~> g++ -Wall -O3 -I/usr/share/R/include -o /tmp/johnmaddock /tmp/johnmaddock.cc -lRmath
edd at basebud:~> /tmp/johnmaddock
0.646447
0.527633
0.090204
0.124652
1.38629
5.03504
edd at basebud:~>                    

| I've tried this on both Windows with the precompiled Mingw binary of R-2.5.0
| (compiling my code with MinGW-3.4.2), and by building R-2.5.0 on Mandriva
| Linux with gcc-3.4.4 and --enable-R-shlib.  The actual code is at the end of
| the message, but the main cases are:
| 
| std::cout << pf(1.5, 2.0, 3.0, 1, 0) << std::endl;
| 
| Outputs 1, expect 0.646447

Good.

| std::cout << pchisq(1.5, 2.0, 1, 0) << std::endl;
| 
| Outputs 1, expect 0.527633

Good.
 
| std::cout << pgamma(1.5, 2.0, 3.0, 1, 0) << std::endl;
| 
| Outputs 1, expect 0.090204

Good.
 
| std::cout << ppois(2.0, 5.0, 1, 0) << std::endl;
| 
| Outputs 0, expect 0.124652

Good.
 
 
| std::cout << qchisq(0.5, 2.0, 1, 0) << std::endl;
| 
| Outputs -0.61379, expect 1.38629

Good.
 
| std::cout << qgamma(0.5, 2.0, 3.0, 1, 0) << std::endl;
| 
| Outputs 0.0282703, expect 5.03504

Good.
 
| I should stress that within the R environment, I *do* get the values I
| expect: though sometimes the arguments need adjusting as the C functions
| have slightly different argument lists from the R versions.
| 
| Also:
| 
| std::cout << qpois(0.5, 5.0, 1, 0) << std::endl;
| 
| Seems to go into an infinite loop inside qpois, while:
| 
| qbeta(0.1, 1e-5, 1e-5, 1, 0);
| 
| Raises an access violation inside R: although I should stress that all other
| beta quantiles I've tested come out OK.
| 
| Anyway, hopefully you can just tell me what an idiot I am :-)

We are too polite for that, we also insist that folks do the old RTFM -- the
#define you missed is of course mentioned in the R Extensions manual.

Hth, Dirk

 
| Thanks in advance for any help you can give,
| 
| John Maddock.
| 
| The complete source code is:
| 
| #include <iostream>
| #include <iomanip>
| 
| extern "C" {
| #include "Rmath.h"
| }
| 
| int main(int argc, const char** argv)
| {
|   std::cout << pf(1.5, 2.0, 3.0, 1, 0) << std::endl;
|   std::cout << pchisq(1.5, 2.0, 1, 0) << std::endl;
|   std::cout << pgamma(1.5, 2.0, 3.0, 1, 0) << std::endl;
|   std::cout << ppois(2.0, 5.0, 1, 0) << std::endl;
|   std::cout << qchisq(0.5, 2.0, 1, 0) << std::endl;
|   std::cout << qgamma(0.5, 2.0, 3.0, 1, 0) << std::endl;
|   //std::cout << qpois(0.5, 5.0, 1, 0) << std::endl;
|   return 0;
| }
| 
| ______________________________________________
| R-devel at r-project.org mailing list
| https://stat.ethz.ch/mailman/listinfo/r-devel

-- 
Hell, there are no rules here - we're trying to accomplish something. 
                                                  -- Thomas A. Edison



[Rd] Problems when linking to R shared library
Dirk Eddelbuettel