安装 R 库:未定义的符号
Installing R library: undefined symbol
我有一个工作的 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
}
来自 RI 运行
compileAttributes(pkgdir = ".", verbose = getOption("verbose"))
并获取文件 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 "library(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 函数(我没有任何其他 predictiveLogLikelihood 函数)
我可能遗漏了一些非常明显的东西,但我已经在这个问题上停留了几天,查看了所有 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
链接的目标文件列表中缺少一个文件:rcpp_functions.o
。您可以将此添加到 Makevars
文件中的 OBJECTS
:
OBJECTS = RcppExports.o rcpp_functions.o $(SOURCES:.cpp=.o)
或者您可以使用
SOURCES = $(wildcard *.cpp) $(wildcard **/*.cpp)
OBJECTS = $(SOURCES:.cpp=.o)
我有一个工作的 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
}
来自 RI 运行 compileAttributes(pkgdir = ".", verbose = getOption("verbose")) 并获取文件 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 "library(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 函数(我没有任何其他 predictiveLogLikelihood 函数)
我可能遗漏了一些非常明显的东西,但我已经在这个问题上停留了几天,查看了所有 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
链接的目标文件列表中缺少一个文件:rcpp_functions.o
。您可以将此添加到 Makevars
文件中的 OBJECTS
:
OBJECTS = RcppExports.o rcpp_functions.o $(SOURCES:.cpp=.o)
或者您可以使用
SOURCES = $(wildcard *.cpp) $(wildcard **/*.cpp)
OBJECTS = $(SOURCES:.cpp=.o)