[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 20:12:55 CEST 2011


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.

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.

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

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

Hope this helps,  Dirk

| Thank you very much,
| with best greetings from Austria,
| Bernhard Pollner
| 
| 
| ----------------------------------------------------------------------
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

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