安装R库:未定义符号

时间:2018-12-06 09:31:16

标签: c++ r linker-errors rcpp dynamic-linking

我有一个正常工作的C ++库,我正尝试通过Rcpp向R公开函数,我有以下目录设置

├── CMakeLists.txt
├── DESCRIPTION
├── install_log.txt
├── LICENSE
├── mock_data.csv
├── NAMESPACE
├── R
│   └── RcppExports.R
├── README.md
├── scripts
│   ├── generate_synthetic_data.cpp
│   └── run_synthetic.cpp
├── src
│   ├── collectors
│   │   ├── base_collector.hpp
│   │   ├── file_collector.cpp
│   │   └── file_collector.hpp
│   ├── data
│   │   ├── cluster.hpp
│   │   ├── time_series.cpp
│   │   ├── time_series.hpp
│   ├── data.hpp
│   ├── exponential_family
│   │   ├── mv_normal_inv_wishart.hpp
│   │   ├── mv_normal_inv_wishart_imp.hpp
│   │   ├── mv_normal_wishart.cpp
│   │   ├── mv_normal_wishart.hpp
│   │   └── options.hpp
│   ├── linear_model
│   │   ├── mv_fixed_covariance.cpp
│   │   └── mv_fixed_covariance.hpp
│   ├── Makevars
│   ├── models
│   │   ├── ar_model.cpp
│   │   ├── ar_model.hpp
│   │   └── base_model.hpp
│   ├── random
│   │   ├── inv_wishart.hpp
│   │   ├── mvn.hpp
│   │   ├── mvn_precision.hpp
│   │   ├── polya_gamma
│   │   │   ├── PolyaGamma.cpp
│   │   │   ├── PolyaGamma.h
│   │   │   └── RNG
│   │   │       ├── CPURNG.cpp
│   │   │       ├── CPURNG.hpp
│   │   └── wishart.hpp
│   ├── RcppExports.cpp
│   ├── RcppExports.o
│   ├── rcpp_functions.cpp
│   ├── rcpp_functions.h
│   ├── samplers
│   │   ├── beta_sampler.cpp
│   │   ├── beta_sampler.hpp
│   │   ├── gamma_sampler.cpp
│   │   ├── gamma_sampler.hpp
│   │   ├── g_sampler.cpp
│   │   ├── g_sampler.hpp
│   │   ├── phi_sampler.cpp
│   │   ├── phi_sampler.hpp
│   │   ├── sigma_sampler.cpp
│   │   ├── sigma_sampler.hpp
│   │   ├── weights_sampler.cpp
│   │   └── weights_sampler.hpp
│   └── utils
│       ├── argparse.hpp
│       ├── data_utils.cpp
│       ├── data_utils.hpp
│       ├── math_utils.cpp
│       ├── math_utils.hpp
│       ├── string_utils.cpp
│       ├── string_utils.hpp
│       ├── vector_utils.cpp
│       └── vector_utils.hpp
├── test
│   ├── test_ar_model.cpp
│   ├── test_vector_utils.cpp
│   └── test_wishart.cpp
└── MyPackage_1.0.tar.gz 

我要公开的函数在src / rcpp_functions.h中声明,并在src / rcpp_functions.cpp中定义,它看起来像:

#include <RcppEigen.h>
#include <vector>
#include <vector_utils.hpp>
#include <time_series.hpp>
#include <ar_model.hpp>

using Eigen::MatrixXd;
using Eigen::VectorXd;
using std::vector;

Rcpp::NumericVector predictiveLogLikelihood(
    const Rcpp::List &timeSerie, const Eigen::MatrixXd &bChain,
    const Eigen::MatrixXd &gammaChain, const Rcpp::List &alphaChains,
    const Rcpp::List &phiChains, const Eigen::MatrixXd &sigmaChain) {

  convert from R to C++ objects & do some stuff
}

我从R运行     compileAttributes(pkgdir =“。”,详细= getOption(“详细”)) 并获取文件R / RcppExports.R,src / RcppExports.cpp

我的临时Makevars非常丑陋,看起来像:

CXX_STD = CXX14

SOURCES=$(wildcard **/*.cpp)

OBJECTS = RcppExports.o $(SOURCES:.cpp=.o)

PKG_CPPFLAGS = -I"." -I"./collectors" -I"./data" -I"./exponential_family"  \
    -I"./linear_model" -I"./models" -I"./random" -I"./random/polya_gamma" \
    -I"./random/polya_gamma/RNG" -I"./samplers" -I"./utils" \
    -I"./usr/include/eigen3/" \
    -DUSE_R -D"USE_R" -w -z,defs

PKG_LIBS = -lgsl

all: $(SHLIB)

$(SHLIB): $(OBJECTS)

目前,我认为它已经足够好了,因为我真的不需要分发软件包(尽管非常欢迎有关改进它的建议)

当我从终端运行时:

  

R -e“库(devtools); install()”

编译正常,但出现以下错误:

*** installing help indices
** building package indices
** testing if installed package can be loaded
Error: package or namespace load failed for ‘MyPackage’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/home/mario/R/x86_64-pc-linux-gnu-library/3.5/MyPackage/libs/MyPackage.so':
  /home/mario/R/x86_64-pc-linux-gnu-library/3.5/MyPackage/libs/MyPackage.so:
 undefined symbol: _Z23predictiveLogLikelihoodRKN4Rcpp6VectorILi19ENS_15PreserveStorageEEERK

给出符号的名称,我想它指的是Rcpp函数(我没有任何其他预测性的LogLikelihood函数)

我可能会遗漏一些非常明显的东西,但是我在这个问题上停留了几天,研究了所有Rcpp软件包和RcppExamples软件包

编辑1 编译的输出是

>R -e "library(devtools); install()"
....
Running /usr/lib/R/bin/R CMD INSTALL \
ttmp/Rtmp412bar/MyPackage_1.0.tar.gz --install-tests 
* installing to library ‘/home/mario/R/x86_64-pc-linux-gnu-library/3.5’
* installing *source* package ‘MyPackage’ ...
** libs

g++ -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG -I"." -I"./collectors" -I"./data" -I"./exponential_family" -I"./linear_model" -I"./models" -I"./random" -I"./random/polya_gamma" -I"./random/polya_gamma/RNG" -I"./samplers" -I"./utils" -I"./usr/include/eigen3/" -DUSE_R -D"USE_R" -w -z,defs -I"/home/mario/R/x86_64-pc-linux-gnu-library/3.5/Rcpp/include" -I"/home/mario/R/x86_64-pc-linux-gnu-library/3.5/RcppArmadillo/include" -I"/home/mario/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include" -I"/home/mario/R/x86_64-pc-linux-gnu-library/3.5/RcppGSL/include"    -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-oUvOgu/r-base-3.5.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c RcppExports.cpp -o RcppExports.o

(对所有源文件完成此操作)

g++ -std=gnu++14 -I"/usr/share/R/include" -DNDEBUG -I"." -I"./collectors" -I"./data" -I"./exponential_family" -I"./linear_model" -I"./models" -I"./random" -I"./random/polya_gamma" -I"./random/polya_gamma/RNG" -I"./samplers" -I"./utils" -I"./usr/include/eigen3/" -DUSE_R -D"USE_R" -w -z,defs -I"/home/mario/R/x86_64-pc-linux-gnu-library/3.5/Rcpp/include" -I"/home/mario/R/x86_64-pc-linux-gnu-library/3.5/RcppArmadillo/include" -I"/home/mario/R/x86_64-pc-linux-gnu-library/3.5/RcppEigen/include" -I"/home/mario/R/x86_64-pc-linux-gnu-library/3.5/RcppGSL/include"    -fpic  -g -O2 -fdebug-prefix-map=/build/r-base-oUvOgu/r-base-3.5.1=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c collectors/file_collector.cpp -o collectors/file_collector.o

构建共享对象

g++ -std=gnu++14 -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o MyPackage.so RcppExports.o samplers/weights_sampler.o samplers/sigma_sampler.o samplers/g_sampler.o samplers/gamma_sampler.o samplers/phi_sampler.o samplers/beta_sampler.o exponential_family/mv_normal_wishart.o linear_model/mv_fixed_covariance.o models/ar_model.o utils/string_utils.o utils/math_utils.o utils/vector_utils.o utils/data_utils.o data/generate_synthetic_data.o data/time_series.o collectors/file_collector.o -lgsl -L/usr/lib/R/lib -lR


installing to /home/mario/R/x86_64-pc-linux-gnu-library/3.5/MyPackage/libs
** R
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded

错误

Error: package or namespace load failed for ‘MyPackage’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/home/mario/R/x86_64-pc-linux-gnu-library/3.5/MyPackage/libs/MyPackage.so':
  /home/mario/R/x86_64-pc-linux-gnu-library/3.5/MyPackage/libs/MyPackage.so: undefined symbol: _Z23predictiveLogLikelihoodRKN4Rcpp6VectorILi19ENS_15PreserveStorageEEERKN5Eigen6MatrixIdLin1ELin1ELi0ELin1ELin1EEES9_S4_S4_S9_
Error: loading failed
Execution halted

1 个答案:

答案 0 :(得分:2)

在链接的目标文件列表中,缺少一个文件:rcpp_functions.o。您可以将其添加到OBJECTS文件中的Makevars

OBJECTS = RcppExports.o rcpp_functions.o $(SOURCES:.cpp=.o)

或者您可以使用

SOURCES = $(wildcard *.cpp) $(wildcard **/*.cpp)

OBJECTS = $(SOURCES:.cpp=.o)