Romain Neugebauer
2004-Nov-11 18:48 UTC
[R] problem building an R package under Windows XP with calls to
Thank you very much for your help. I was able to figure out a way to make this simple package work and was able to build a more useful and complex package as well. I am however left with one problem when building that more complex package. Before I go on with this issue, here is how I solved my initial problem: After creating an R package directory structure, I included in the \src directory my test.c file containing the following C code: #include <stdio.h> #include <nag.h> #include <nagg05.h> void test() { long i; g05ccc(); for(i=0;i<10000;i++)printf("Hello the world - NAG random number: %lf\n",g05cac()); } and a test.R file in the \R directory with the following R code: .First.lib<-function(lib,pkg){ library.dynam("test",pkg,lib) } .Last.lib <- function(libpath) { library.dynam.unload("test",libpath) } gotest <- function() { .C("test") return(0) } Based on Duncan Murdoch's recommendation, I then included a Makefile.win file containing the following code: test.dll : test.obj test.def link /dll /def:test.def /out:test.dll test.obj nagcsmt-mkl.lib mkl_s.lib mkl_def.lib mkl_lapack.lib advapi32.lib netapi32.lib Rdll.lib test.obj : test.c cl /MT /Ox /D "WIN32" /c -I"C:\Program Files\Numerical Algorithms Group\CLW3207DA\include" test.c I inspired myself from the readme.package file in the \R folder to write these lines. Note that this makefile relies on a .def file which indicates (at least this is my understanding of what it does) to R how to find the test() C function in the test.dll. Before realizing the need for this .def file, I omitted this file in the Makefile.win. As a consequence I was able to build and install the package successfully, i.e. without error message. However, when I tried to use it, R would give me an error message suggesting that it could not find the test() C routine. Here is the code included in the test.def that I placed in the \src directory: LIBRARY test EXPORTS test After doing so I was able to successfully build, install and use the R package that I called "test": C:\Rdevelop>Rcmd INSTALL test ---------- Making package test ------------ adding build stamp to DESCRIPTION running src/Makefile.win ... cl /MT /Ox /D "WIN32" /c -I"C:\Program Files\Numerical Algorithms Group\CLW3207DA\include" test.c Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804 for 80x86 Copyright (C) Microsoft Corp 1984-1998. All rights reserved. test.c link /dll /def:test.def /out:test.dll test.obj nagcsmt-mkl.lib mkl_s.lib mkl_def.lib mkl_lapack.lib advapi32.lib netapi32.lib Rdll.lib Microsoft (R) Incremental Linker Version 6.00.8447 Copyright (C) Microsoft Corp 1992-1998. All rights reserved. Creating library test.lib and object test.exp ... done installing DLL installing R files installing data files installing man source files installing indices not zipping data installing help >>> Building/Updating help pages for package 'test' Formats: text html latex example chm f text html latex example adding MD5 sums * DONE (test) Now, using the same methodology I was able to build a more complex R package successfully. The package works very well and returns the answers I expect except that it does not display in the R interface the things that the C program requests to be printed. I this more complex package, I use several calls to "printf" in the C program of that package. I also use "cat" calls in the R function of the package. It turns out that all calls to "cat" work without problem whereas calls to "printf" always fail (i.e. nothing is displayed in R). What is also interesting is that the calls to "printf" are however all displayed after I quit R. So it looks like it is R that does not allow my C program to display text on the standard output. This is very surprising because the simple package "test" I described above does work just fine even though it also uses "printf" calls. Again, any suggestions on what can cause this problem or any recommendation on how to solve it will be greatly appreciated. Thank you. Romain [[alternative HTML version deleted]]
Prof Brian Ripley
2004-Nov-11 21:04 UTC
[R] problem building an R package under Windows XP with calls to
Please do read the posting guide: that clearly indicates you sent this to the wrong list. On Thu, 11 Nov 2004, Romain Neugebauer wrote: [Comments removed about how you didn't know how to use VC++, a compiler we don't recommend but for which e.g. `S Programming' has extensive examples.]> I this more complex package, I use several calls to "printf" in the C > program of that package. I also use "cat" calls in the R function of the > package. It turns out that all calls to "cat" work without problem > whereas calls to "printf" always fail (i.e. nothing is displayed in R). > What is also interesting is that the calls to "printf" are however all > displayed after I quit R. So it looks like it is R that does not allow > my C program to display text on the standard output. This is very > surprising because the simple package "test" I described above does work > just fine even though it also uses "printf" calls.The answer to your question is `use Rprintf'. Please read `Writing R Extensions', carefully, and you will be less surprised. Reading the rw-FAQ would help, too. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272860 (secr) Oxford OX1 3TG, UK Fax: +44 1865 272595
Apparently Analagous Threads
- FW: problem building an R package under Windows XP with calls to NAG C routines
- problem building an R package under Windows XP with calls to NAG C routines
- Error compiling ROracle on Windows 2000
- About creating an import library
- Building Rdll.lib with Visual C++