title: Creating R package containing C++ on Windows
body: <p>My goal is to create a package in R with C++ code: So my questions is how?</p>
  
  <p>I am following the tutorial <a href="http://www.stat.columbia.edu/~gelman/stuff_for_blog/AlanRPackageTutorial.pdf" rel="nofollow noreferrer">http://www.stat.columbia.edu/~gelman/stuff_for_blog/AlanRPackageTutorial.pdf</a> on creating an R package containing C++ code. The specific code Im trying to compile and package is exactly as described in the tutorial. </p>
  
  <p>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)</p>
  
  <p>R CMD INSTALL is where the problem begins. I have done two things encountering two different errors supposedly related:</p>
  
  <p>1) The tutorial says the NAMESPACE file is supposed to contain the code:</p>
  
  <pre><code>useDynLib(XDemo)
  export(XDemoAutoC)
  </code></pre>
  
  <p>When it does R CMD INSTALL fail resulting in error:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Im running version R2.15.2 on windows 64-bit and using R64 bit. </p>
  
  <p>I read the following post with a similar problem: 
  <a href="http://r.789695.n4.nabble.com/Include-C-DLL-error-in-C-symbol-name-not-in-load-table-td3464021.html" rel="nofollow noreferrer">http://r.789695.n4.nabble.com/Include-C-DLL-error-in-C-symbol-name-not-in-load-table-td3464021.html</a></p>
  
  <p>Except they mention nothing about the NAMESPACE-matter. </p>
  
  <p>Also I read this post:
   <a href="https://stackoverflow.com/questions/4916853/problem-with-loading-compiled-c-code-in-r-x64-using-dyn-load">Problem with loading compiled c code in R x64 using dyn.load</a> </p>
  
  <p>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&lt;&lt; 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 <img src="https://i.stack.imgur.com/iSRGT.jpg" alt="enter image description here"> </p>
  
  <p>I really don't have any ideas on how to fix this problem so any suggestion on what to do would be welcome? </p>