hap*_*app 2 c++ r static-libraries .a rcpp
我想在 R 包中使用libDAI C++库并想要该包:
我当前的设置是:
修改Makevar文件:
# include libraries
PKG_CPPFLAGS =-I../inst/include/
PKG_LIBS = -Llib -l../lib/libdai.a
Run Code Online (Sandbox Code Playgroud)
我用于访问 libDAI 库的脚本是(src/ 中的 test.cpp):
#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;
}
Run Code Online (Sandbox Code Playgroud)
运行Rscript -e "Rcpp::compileAttributes('libdai')",然后R CMD INSTALL libdai返回错误:
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
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:
您可以使用-L<directory> -l<name>或<path>,即根据您的情况
PKG_LIBS = -L../lib -ldai
Run Code Online (Sandbox Code Playgroud)
或者
PKG_LIBS = ../lib/libdai.a
Run Code Online (Sandbox Code Playgroud)
的标头libDAI仅在内部使用。无法链接到这些标头中声明的函数。因此我不会使用inst/include这些标头。
gmp 库似乎在 CRAN 构建器上可用,参见https://github.com/cran/gmp和https://cran.r-project.org/package=gmp。看来libDAI需要链接到boost(程序选项),参见https://bitbucket.org/jorism/libdai/src/83bd24a4c5bf17b0592a7b5b21e26bf052881833/Makefile.LINUX?at=master&fileviewer=file-view-default#Makefile.LINUX-49。然而,从实际情况来看,Makefile这似乎仅用于测试和实用程序。因此,您可能会摆脱BH 包提供的boost标头。
这是 Windows 上的常见方法(参见https://github.com/rwinlib),但我发现它对于 Linux 来说不寻常。更常见的方法是以下之一:
对于这三种方法,CRAN 和 GitHub 上都有大量示例。不过,很难提出建议。我可能会选择“在包中包含源代码”并使用上游提供的 Makefile 作为构建库的起点。