I am trying to create a dynamic library (dll) for loading into R with
I am trying to use the library libdai (a machine learning C++ library) and
boost (C++ libraries) in my main C++ program. While I am able to compile the
main C++ file I am using successfully into a dll, there is a problem linking
to libraries (libdai.a) and boost libraries. I only have instructions on how
to compile these libraries with cygwin - NOT with minGW tools, as required
by Rtools and R for correct dll creation and loading.
Currently I have R CMD SHLIB configured as follows:
(To create shared library)
<path to Rtools/minGW/bin>/g++ -shared -s static-libgcc -o example.dll
example.o -L.../libdai/lib -ldai -L"C:/cygwin/lib" -lcygwin
Including the -lcygwin library is necessary for compilation without linker
error. However, it causes dyn.load to "hang" when used in R.
The tutorials on the web regarding creation of C++ dlls do not discuss
linking to libraries.
Thus my two questions are:
(1) Does anyone know how to compile a C++ program which links to a library,
which dyn.load will accept? What flags are required in the R makeconf? How
do you link in a library to the dll? What flags must I compile the libdai
and boost libraries with?
(2) If my interfacing issue cannot be resolved, does anyone know any tools
that interface to R that do machine learning belief propagation?
Any help is appreciated.
School of IT
University of Sydney
Re: R-loadable dll with minGW-compiled linked library
On 30 August 2011 at 02:25, Ilana Lichtenstein wrote:
| The tutorials on the web regarding creation of C++ dlls do not discuss
| linking to libraries.
Well, there are working examples among the 3200+ CRAN packages...
| Thus my two questions are:
| (1) Does anyone know how to compile a C++ program which links to a library,
| which dyn.load will accept? What flags are required in the R makeconf? How
| do you link in a library to the dll? What flags must I compile the libdai
| and boost libraries with?
Sure. I am as self-centered as the next guy so I welcome you to look at my
RQuantLib project (on CRAN and R-Forge) which has been doing that for a
decade -- and the QuantLib project / library itself uses parts of Boost.
Moreover, Rcpp is a package whose goal it is to make C++ interchange from /
to R a little less painful. There are now three dozen packages on CRAN using
it (see the 'reverse depends' via the Rcpp CRAN page) and several of these
link to other external libraries.
[ All that said, I suspect you need to build everything with MinGW. You
certainly cannot mix object code generated via compilers from the visual
whichever suites (as C++ function headers get mangled) and I am pretty
certain that Cygwin's g++ may be doomed for the same reason. I'd be happy to
be convinced otherwise; this would be something worth documenting better. ]
| (2) If my interfacing issue cannot be resolved, does anyone know any tools
| that interface to R that do machine learning belief propagation?