I want to use the libDAI C++ library within an R-package and want the package:
- to be usable on Linux and Windows
- save disc space (the external library has ~60 Mb)
- the end user does not need to install boost and gmp for compilation
My current setup is:
- precompile libDAI
- copy libdai.a to lib/
- copy all libDAI header files to inst/include
- add Makevar to src/
Modify Makevar file:
# include libraries
PKG_CPPFLAGS =-I../inst/include/
PKG_LIBS = -Llib -l../lib/libdai.a
My script for accessing the libDAI library is (test.cpp in src/):
#include <dai/factorgraph.h>
#include <Rcpp.h>
#include <cmath>
using namespace Rcpp;
using namespace std;
using namespace dai;
//'
//' Creates libDAI factor graph object
//'
//' @param factor_graph character definition of the factor graph
//' @export
// [[Rcpp::export]]
void initialize_factor_graph(const char* factor_graph) {
// read the factor graph from the string
std::istringstream fgStream(factor_graph);
FactorGraph net;
net.ReadFromString( fgStream );
// Output some information about the factorgraph
cout << "Factor graph has " << net.nrVars() << " variables" << endl;
cout << "Factor graph has " << net.nrFactors() << " factors" << endl;
}
running Rscript -e "Rcpp::compileAttributes('libdai')"
, followed by R CMD INSTALL libdai
returns the error:
Error: package or namespace load failed for 'libdai' in dyn.load(file, DLLpath = DLLpath, ...):
unable to load shared object
'/home/jk/libs/R/libdai/libs/libdai.so':
/home/jk/libs/R/libdai/libs/libdai.so: undefined symbol: _ZTVN3dai11FactorGraphE
Error: loading failed
So my questions are:
- What is wrong with my setup?
- what is the best procedure to share my final package on CRAN?
- What is the nicest setup for sharing the package?
My question is closely related to this and this question and several other post related to referencing static libraries, however I was not able to solve my problem with these links.