FW: [R] problem building an R package under Windows XP with calls to NAG C routines

Duncan Murdoch murdoch at stats.uwo.ca
Thu Nov 11 20:58:53 CET 2004


On Thu, 11 Nov 2004 11:19:25 -0800, "Romain Neugebauer"
<romain at berkeley.edu> wrote :

>Dear Duncan Murdoch,
> 
>I just posted the following message to the R-help mailing list in
>response to your email of October 29th (see below).  Unfortunately it
>looks like my new message was not correctly posted, i.e. it does not
>appear as a reply to yours but as a new thread. 
> 
>I would like to thank you again for your important help. Also, I would
>be pleased to help out by sending "patches" as you suggested but I am
>not sure what you mean by those "patches". If you can get back to me to
>clarify how I could be useful, I would be happy to consider helping if I
>have the appropriate skills.

What I meant by "patches" was corrections to the readme.packages file.
The instructions there are not actively maintained, so they may apply
to an obsolete version of MSVC, or may be incomplete, or may just
contain errors.  Now that things are (almost) working for you, go back
and read the instructions and see if you can improve on them; when
you've done that, send me a copy of the changes (using the "patch"
program if you know how, or just sending me a full copy of the updated
file).

More comments intermixed with your letter below...

> 
>Thank you again,
> 
>Romain
> 
>-----Original Message-----
>From: Romain Neugebauer [mailto:romain at berkeley.edu] 
>Sent: Thursday, November 11, 2004 10:48 AM
>To: 'r-help at stat.math.ethz.ch'
>Subject: [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());
> 
>}

Read the Writing R Extensions manual to find out how to do output
using Rprintf.

>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. 

Actually, the .def file is instructions to the linker, telling it
which functions in the DLL that you're producing should be made
visible to callers.  R will only see functions if they are made
visible, and the MS linker chooses what to make visible based on the
.def file.

Duncan Murdoch


>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
> 
> 
> 
> 
>You wrote:
> 
>The problem is that MSVC libraries are not compatible with gcc.  The
>gcc linker doesn't know what to do with them, they're in a different
>format than it expects.
> 
>To do static linking, you'll need to use MSVC to compile and link your
>DLL in the package.  This means setting up a Makefile.win so that MSVC
>gets called instead of gcc.  There's some info on this in the
>README.packages file, but it's incomplete, because none of the core
>developers use MSVC.   If you want to add to it, send me the patches.
> 
>Alternatively, just install the package using your "Hello, world" DLL,
>then compile the real DLL using MSVC and put it into the libs
>subdirectory where your package was installed.
> 
>Note that if you want to put your package on CRAN, you'll have to be
>able to build it without proprietary tools.
> 
>Duncan Murdoch
> 
>On Thu, 28 Oct 2004 18:15:46 -0700, "Romain Neugebauer"
>< <https://stat.ethz.ch/mailman/listinfo/r-help> romainn at hotmail.com>
>wrote:
> 
>>Hello all,
>>
>>I was able to create R packages under windows XP in the past using the 
>>dynamic NAG C library for windows XP (Mark 6).
>>
>>Recently, I changed computers and I am now using the static NAG C
>library 
>>for windows XP (Mark 7) to create a simple R package (called "test")
>which 
>>simply returns random numbers simulated using repetitive calls to a NAG
>C 
>>routine (uniform random generator). I use R Version 2.0.0. and
>installed all 
>>the tools needed to build R packages as described in 
>> <http://www.murdochsutherland.com/Rtools/>
>http://www.murdochsutherland.com/Rtools/
>and readme.packages.
> <http://www.murdochsutherland.com/Rtools/> >
> <http://www.murdochsutherland.com/Rtools/> >I was able to successfully
>execute the C program that is part of this 
> <http://www.murdochsutherland.com/Rtools/> >package when compiling it
>using Microsoft visual studio. However, when I now 
> <http://www.murdochsutherland.com/Rtools/> >try to create the R package
>with the command "Rcmd INSTALL test" I obtain 
> <http://www.murdochsutherland.com/Rtools/> >the following:
> <http://www.murdochsutherland.com/Rtools/> >
> <http://www.murdochsutherland.com/Rtools/> >C:\Rdevelop>Rcmd INSTALL
>test
> <http://www.murdochsutherland.com/Rtools/> >
> <http://www.murdochsutherland.com/Rtools/> >
> <http://www.murdochsutherland.com/Rtools/> >---------- Making package
>test ------------
> <http://www.murdochsutherland.com/Rtools/> >  adding build stamp to
>DESCRIPTION
> <http://www.murdochsutherland.com/Rtools/> >  making DLL ...
> <http://www.murdochsutherland.com/Rtools/> >making test.d from test.c
> <http://www.murdochsutherland.com/Rtools/> >gcc   -Ic:/R/rw2000/include
>-Wall -O2 -I"C:\Program Files\Numerical 
> <http://www.murdochsutherland.com/Rtools/> >Algorithms
>Group\CLW3207DA\include"  -c test.c -o test.o
> <http://www.murdochsutherland.com/Rtools/> >ar cr test.a test.o
> <http://www.murdochsutherland.com/Rtools/> >ranlib test.a
> <http://www.murdochsutherland.com/Rtools/> >windres --include-dir
>c:/R/rw2000/include  -i test_res.rc -o test_res.o
> <http://www.murdochsutherland.com/Rtools/> >gcc  --shared -s  -o
>test.dll test.def test.a test_res.o  
> <http://www.murdochsutherland.com/Rtools/> >-Lc:/R/rw2000/src/gnuwin32
>-L"C:\Program Files\Microsoft Visual 
> <http://www.murdochsutherland.com/Rtools/> >Studio\VC98\Lib"
>-L"C:\Program Files\
> <http://www.murdochsutherland.com/Rtools/> >Numerical Algorithms
>Group\CLW3207DA" -L"C:\Program Files\Numerical 
> <http://www.murdochsutherland.com/Rtools/> >Algorithms
>Group\CLW3207DA\mkl\lib" -lLIBCMT -lnagcsmt-mkl -lmkl_s -lmkl_def 
> <http://www.murdochsutherland.com/Rtools/> >-lmkl_lapack
> <http://www.murdochsutherland.com/Rtools/> >-lADVAPI32 -lNETAPI32
>-lg2c -lR
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/> >Warning: .drectve `%.*s'
>unrecognized
> <http://www.murdochsutherland.com/Rtools/>
>>/mingw/lib/libmingw32.a(main.o)(.text+0x106):main.c: undefined
>reference to 
> <http://www.murdochsutherland.com/Rtools/> >`WinMain at 16'
> <https://stat.ethz.ch/mailman/listinfo/r-help> >make[3]: *** [test.dll]
>Error 1
> <https://stat.ethz.ch/mailman/listinfo/r-help> >make[2]: ***
>[srcDynlib] Error 2
> <https://stat.ethz.ch/mailman/listinfo/r-help> >make[1]: *** [all]
>Error 2
> <https://stat.ethz.ch/mailman/listinfo/r-help> >make: *** [pkg-test]
>Error 2
> <https://stat.ethz.ch/mailman/listinfo/r-help> >*** Installation of
>test failed ***
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Removing
>'C:/R/rw2000/library/test'
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Restoring previous
>'C:/R/rw2000/library/test'
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Interestingly enough,
>if I remove the calls to the NAG C routine in the C 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >program AND if I remove
>the gcc flag "-lLIBCMT", the R package can be 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >created successfully
>and I can use the package in R (it will simply print a 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >"hello the world"
>line). I obtain:
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >C:\Rdevelop>Rcmd
>INSTALL test
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >---------- Making
>package test ------------
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  adding build stamp to
>DESCRIPTION
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  making DLL ...
> <https://stat.ethz.ch/mailman/listinfo/r-help> >gcc  --shared -s  -o
>test.dll test.def test.a test_res.o  
> <https://stat.ethz.ch/mailman/listinfo/r-help>
>>-Lc:/R/rw2000/src/gnuwin32 -L"C:\Program Files\Microsoft Visual 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Studio\VC98\Lib"
>-L"C:\Program Files\
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Numerical Algorithms
>Group\CLW3207DA" -L"C:\Program Files\Numerical 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Algorithms
>Group\CLW3207DA\mkl\lib"  -lnagcsmt-mkl -lmkl_s -lmkl_def 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >-lmkl_lapack  -lADVAPI
> <https://stat.ethz.ch/mailman/listinfo/r-help> >32 -lNETAPI32  -lg2c
>-lR
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  ... DLL made
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  installing DLL
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  installing R files
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  installing data files
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  installing man source
>files
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  installing indices
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  not zipping data
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  installing help
> <https://stat.ethz.ch/mailman/listinfo/r-help> > >>> Building/Updating
>help pages for package 'test'
> <https://stat.ethz.ch/mailman/listinfo/r-help> >     Formats: text html
>latex example chm
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  f
>text    html    latex   example
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  adding MD5 sums
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >* DONE (test)
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >If I add the flag
>"-lLIBCMT" (and the C program still does not call any NAG 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >routine) I get the
>following:
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >C:\Rdevelop>Rcmd
>INSTALL test
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >---------- Making
>package test ------------
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  adding build stamp to
>DESCRIPTION
> <https://stat.ethz.ch/mailman/listinfo/r-help> >  making DLL ...
> <https://stat.ethz.ch/mailman/listinfo/r-help> >making test.d from
>test.c
> <https://stat.ethz.ch/mailman/listinfo/r-help> >gcc
>-Ic:/R/rw2000/include -Wall -O2 -I"C:\Program Files\Numerical 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Algorithms
>Group\CLW3207DA\include"  -c test.c -o test.o
> <https://stat.ethz.ch/mailman/listinfo/r-help> >test.c: In function
>`test':
> <https://stat.ethz.ch/mailman/listinfo/r-help> >test.c:7: warning:
>unused variable `i'
> <https://stat.ethz.ch/mailman/listinfo/r-help> >ar cr test.a test.o
> <https://stat.ethz.ch/mailman/listinfo/r-help> >ranlib test.a
> <https://stat.ethz.ch/mailman/listinfo/r-help> >gcc  --shared -s  -o
>test.dll test.def test.a test_res.o  
> <https://stat.ethz.ch/mailman/listinfo/r-help>
>>-Lc:/R/rw2000/src/gnuwin32 -L"C:\Program Files\Microsoft Visual 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Studio\VC98\Lib"
>-L"C:\Program Files\
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Numerical Algorithms
>Group\CLW3207DA" -L"C:\Program Files\Numerical 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Algorithms
>Group\CLW3207DA\mkl\lib" -lLIBCMT -lnagcsmt-mkl -lmkl_s -lmkl_def 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >-lmkl_lapack
> <https://stat.ethz.ch/mailman/listinfo/r-help> >-lADVAPI32 -lNETAPI32
>-lg2c -lR
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Warning: .drectve
>`%.*s' unrecognized
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Warning: .drectve
>`%.*s' unrecognized
> <https://stat.ethz.ch/mailman/listinfo/r-help>
>>/mingw/lib/libmingw32.a(main.o)(.text+0x106):main.c: undefined
>reference to 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >`WinMain at 16'
> <https://stat.ethz.ch/mailman/listinfo/r-help> >make[3]: *** [test.dll]
>Error 1
> <https://stat.ethz.ch/mailman/listinfo/r-help> >make[2]: ***
>[srcDynlib] Error 2
> <https://stat.ethz.ch/mailman/listinfo/r-help> >make[1]: *** [all]
>Error 2
> <https://stat.ethz.ch/mailman/listinfo/r-help> >make: *** [pkg-test]
>Error 2
> <https://stat.ethz.ch/mailman/listinfo/r-help> >*** Installation of
>test failed ***
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Removing
>'C:/R/rw2000/library/test'
> <https://stat.ethz.ch/mailman/listinfo/r-help> >Restoring previous
>'C:/R/rw2000/library/test'
> <https://stat.ethz.ch/mailman/listinfo/r-help> >
> <https://stat.ethz.ch/mailman/listinfo/r-help> >So it appears that the
>problem is not related to the calls to the NAG C 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >routines but to the
>library "LIBCMT" which is required when using the NAG 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >routines.
> <https://stat.ethz.ch/mailman/listinfo/r-help> >I contacted the NAG
>people who recommended to check the version of the 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >binutils and gcc from
>Mingw. I use gcc version 3.3.1 and I update the 
> <https://stat.ethz.ch/mailman/listinfo/r-help> >binutils from
> <https://stat.ethz.ch/mailman/listinfo/r-help>
>>http://www.mingw.org/download.shtml using 
> <http://www.mingw.org/download.shtml>
>>binutils-2.13.90-20021006-2.tar.gz.
> <http://www.mingw.org/download.shtml> >
> <http://www.mingw.org/download.shtml> >Any help would be greatly
>appreciated.
> <http://www.mingw.org/download.shtml> >Note that I am able to build the
>same R package with calls to NAG routines 
> <http://www.mingw.org/download.shtml> >under linux with the static NAG
>C library for linux.
> <http://www.mingw.org/download.shtml> >
> <http://www.mingw.org/download.shtml> >Thank you,
> <http://www.mingw.org/download.shtml> >
> <http://www.mingw.org/download.shtml> >Romain
> <http://www.mingw.org/download.shtml> >
> <http://www.mingw.org/download.shtml>
>>______________________________________________
> <http://www.mingw.org/download.shtml> >R-help at stat.math.ethz.ch
>mailing list
> <https://stat.ethz.ch/mailman/listinfo/r-help>
>>https://stat.ethz.ch/mailman/listinfo/r-help
> <https://stat.ethz.ch/mailman/listinfo/r-help> >PLEASE do read the
>posting guide! http://www.R-project.org/posting-guide.html
> <http://www.R-project.org/posting-guide.html>  
> <http://www.R-project.org/posting-guide.html>  
> <http://www.R-project.org/posting-guide.html>  
> <http://www.R-project.org/posting-guide.html>




More information about the R-help mailing list