[Rcpp-devel] How to link a .lib file in a rcpp-package in a Windows environment?

Dirk Eddelbuettel edd at debian.org
Sun Apr 24 22:37:47 CEST 2011

Hi Bernhard,

On 24 April 2011 at 22:26, Bernhard Pollner wrote:
| Hello Dirk,
| thanks for your quick answer.
| Due to my, unfortunately, profound lack of knowledge in this area I have to ask some additional (and probably quite stupid) questions regarding what you replied to me - see in the text below.
| Am 24.04.2011 um 20:12 schrieb Dirk Eddelbuettel:
| > 
| > Hi Bernhard,
| > 
| > Thanks for posting here!
| > 
| >> On 24 April 2011 at 18:29, Bernhard Pollner wrote:
| >> | Hello,
| >> | 
| >> | I am in the process, i.e. I just began to write a Rcpp-based package for a
| >> | Windows environment. The purpose of that package will be to read out signals
| >> | from a USB-connected hardware. The company who is selling this hardware is
| >> | distributing a SDK for developing custom C-programms to read out those signals,
| >> | so I have all the required files (.h, .lib, .dll) to compile a working .exe
| >> | file -- and now, due to some reasons, I want to implement this code in an
| >> | R-package, and read out the signals from the devices straight from within R.
| > 
| > I have done something similar in the part: use the .lib (or .dll) of a C
| > library, presumably produced by a variant of Visual C++, in some MinGW
| > compiled code for R.  With C you can do this; with C++ you are most often out
| > of luck due to name mangling of the functions.
| > 
| > The actual act of mixing MinGW and a VC library is a topic in and by itself;
| > I have found the MinGW documentation to be of help. I believe the FAQ has an
| > entry for this.  As I recall, you may need to do something about symbols
| > being exported etc pp.
| OK, so I will check the FAQs of the MinGW documentation.
| > 
| > I would also strongly recommend to just write a simple 'hello world' wrapper
| > that just tries to access one function from the SDK via R first, ie outside
| > of Rcpp.  Once you have that working, mix it with Rcpp.
| How do I write a simple wrapper via R  (without using Rcpp) for a function from the SDK? Or where can I start reading how to do that? 
| Could you give me or point me to a code-example?

A really simple (untested) one would be something like

#include "whicheverHeaderYouNeed.h"   // adapt to what Plastic needs

SEXP simpleText(void) {		      // no arguments

   Rprintf("hello world--before call\n");

   // set up arguments as needed

   Rprintf("hello world--after call\n");

   return R_NilValue;		      // because of the SEXP return value

See how you far you get with this...

| > The simplest use is to just drop the static library, with its full path, in
| > the linker instructions.  So instead of  -lfoo  just do  C:/foo/sdk/lib/libfoo.a
| > (and a relative path should work too).
| The "linker instructions", are they written in the file Makevars.win? If not, where are they?
| So, then in the right place, what EXACTLY should I then write in there for the .lib called e.g. "plastic.lib"?
| (And how do I write relative paths?)

In the very simplest case you can header headers and the library to the src/
directory of your sources.  The Makevars.win may just be



| >> | I am planning to expose the needed C-functions via modules to R, but,
| >> | unfortunately, I did not even come that far:
| >> | 
| >> | Here´s my problem:
| >> | I do not know, i.e. it is not clear to me how to link / use the .lib file of
| >> | the SDK in my package.
| >> | (In the instructions coming with the SDK they write:  "... copy the contents of
| >> | "API \ lib" to your compiler´s lib directory)
| >> | Is it possible that I have to use the LDFlags function in src/Makevars.win here
| >> | somehow? If yes, then I do not know how to write the appropriate code....
| >> | I do have two .dll files (which I place in in the src-folder) and I suppose
| >> | that I just write them into the Namespace-file using "useDynLib(NameOfTheFile)
| >> | ".
| >> | But, as I was not able to compile the package yet, I do not know if this is
| >> | right either...
| >> | (And the .h file I just put into the src-folder....)
| >> | 
| >> | Could someone, please, show me a way and  the code of how to correctly link the
| >> | .lib file in my package?
| > 
| > All that R does (in the simple cases) is to use it own rules, augmented by
| > the instructions in the file  src/Makevars.win.
| > 
| > So once you know how to build against the SDK (see above),
| --- that means, once the missing .lib - file is correctly linked, or? -- 
| > just follow the
| > usual 'how to build your package with Rcpp' guidelines in the Rcpp-package
| > vignette and expand PKG_LIBS and PKG_CXXFLAGS in src/Makewin.
| Sorry, and again my knowledge in this field is rather bad:
| how do I expand those flags, how do I add PKG_CXXFLAGS to the Makevars.win file?
| Could you give me, please, an exact example of what I would have to write for a library called e.g. "plastic.lib"?

See above.

Cheers, Dirk

| > 
| > Hope this helps,  Dirk
| Thank you very much for your efforts,
| with kind regards,
| Bernhard

Gauss once played himself in a zero-sum game and won $50.
                      -- #11 at http://www.gaussfacts.com

More information about the Rcpp-devel mailing list