Creating R package containing C++ on Windows

My goal is to create a package in R with C++ code: So my questions is how?

I am following the tutorial http://www.stat.columbia.edu/~gelman/stuff_for_blog/AlanRPackageTutorial.pdf on creating an R package containing C++ code. The specific code Im trying to compile and package is exactly as described in the tutorial.

R CMD SHLIB seems to be working creating .dll file. I can load in R using dyn.load() and test it on simulated data (as described in tutorial)

R CMD INSTALL is where the problem begins. I have done two things encountering two different errors supposedly related:

1) The tutorial says the NAMESPACE file is supposed to contain the code:

useDynLib(XDemo)
export(XDemoAutoC)

When it does R CMD INSTALL fail resulting in error:

Error in inDL(x,as.logical(local), as.logical(now),...): unable to load shared object 'C:/.../libs/i386/XDemo.dll': Loadlibrary failure: 1% is not a valid Win32-program

2) Removing the above mentioned lines in NAMESPACE file will result in installation of package. I can succesfully load it in R but when I try to use the R function that makes a .C() call to the C++ written function I another error:

library(newpackage)
ls(package:newpackage)
[[1]] "XDemoAutoC"
  Warning message:
 In ls(package:newpackage) :
 ‘package:newpackage’ converted to character string
 XDemoAutoC(c(1,2,3,4))
Error in .C("DemoAutoCor", OutVec = as.double(vector("numeric", OutLength)),  : 
 C symbol name "DemoAutoCor" not in load table

Im running version R2.15.2 on windows 64-bit and using R64 bit.

I read the following post with a similar problem: http://r.789695.n4.nabble.com/Include-C-DLL-error-in-C-symbol-name-not-in-load-table-td3464021.html

Except they mention nothing about the NAMESPACE-matter.

Also I read this post: Problem with loading compiled c code in R x64 using dyn.load

So I am thinking: that based on the fact that I am able to use dyn.load() in Rx64 means that I have succesfully created x64 .dll. Assuming that the NAMESPACE file is supposed to be left as in the tutorial - hopefully fixing the >>not in load table<< error - this would mean I should focus on fixing problem one. This problem seems to be caused by something related to 32-bit. I have used Dependency Walker on the .dll file but I am not sure how to interpret the results 在这里输入图像描述

I really don't have any ideas on how to fix this problem so any suggestion on what to do would be welcome?


I think you are doing it wrong. Two quick suggestions:

  • Read the Writing R Extensions manual written to explain just this: writing R extensions including those with compiled code

  • Have a look at Rcpp which makes R and C++ extensions, including package building so much easier. Or so we think. Writing a package is as easy as calling Rcpp.package.skeleton() . The documentation in 1) still help.

  • That said, if R CMD INSTALL fails you may have some mixup in your $PATH . Never ever mix MinGW and Cygwin. Make sure no Cygwin DLLs are found when you build or call R. Path order matters greatly. See the manual for details.

    链接地址: http://www.djcxy.com/p/26820.html

    上一篇: 赢7,64位,DLL的问题

    下一篇: 在Windows上创建包含C ++的R包