Rcpp - sourceCpp - 未定义的符号

时间:2018-05-15 15:43:26

标签: c++ r rcpp

我正在使用Rcpp将自编的C ++写入R。

我有3个以下的C ++文件

header.h

#include <Rcpp.h>
int x();

def.cpp

#include <Rcpp.h>
#include "header.h"
// [[Rcpp::export]]
int x()
{
  return 0;
}

call.cpp

#include <Rcpp.h>
#include "header.h"

// [[Rcpp::export]]
int callx(){
  return x();
}

成功编译后,我可以调用函数callx()以及来自R的x()

R> callx()
[1] 0
R> x()
[1] 0

但是当我尝试使用cppSource来源call.cpp时,我得到了错误

> Rcpp::sourceCpp('src/call.cpp')
Error in dyn.load("/tmp/RtmpNpOnpJ/sourceCpp-x86_64-pc-linux-gnu-0.12.16/sourcecpp_3a47f828d0d/sourceCpp_2.so") :
unable to load shared object '/tmp/RtmpNpOnpJ/sourceCpp-x86_64-pc-linux-gnu-0.12.16/sourcecpp_3a47f828d0d/sourceCpp_2.so':
/tmp/RtmpNpOnpJ/sourceCpp-x86_64-pc-linux-gnu
0.12.16/sourcecpp_3a47f828d0d/sourceCpp_2.so: undefined symbol: _Z1xv

为了编译,我使用R CMD INSTALL,其中包括以下步骤:

g++  -I/usr/share/R/include -DNDEBUG  -I"/usr/lib/R/library/Rcpp/include"    -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c RcppExports.cpp -o RcppExports.o
g++  -I/usr/share/R/include -DNDEBUG  -I"/usr/lib/R/library/Rcpp/include"    -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c call.cpp -o call.o
g++  -I/usr/share/R/include -DNDEBUG  -I"/usr/lib/R/library/Rcpp/include"    -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c def.cpp -o def.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o linkingtest.so RcppExports.o call.o def.o -L/usr/lib/R/lib -lR

2 个答案:

答案 0 :(得分:4)

真的不明白为什么有些人会反对创建包。

这是一个快速复制并粘贴一个完整输出的屏幕,对应于五个命令,准确地给出了你想要的确切输入文件

五个简单的步骤。

第一步:为Rcpp

创建占位符
edd@rob:/tmp/flappix$ Rscript -e 'Rcpp::Rcpp.package.skeleton("stubborn")' 
Creating directories ...                                                                                                                                                                                                   
Creating DESCRIPTION ...                                                                                     
Creating NAMESPACE ...                                 
Creating Read-and-delete-me ...                          
Saving functions and data ...                          
Making help files ...                                                                                                                                                                                                      
Done.                                                                                                        
Further steps are described in './stubborn/Read-and-delete-me'.

Adding Rcpp settings                                                         
 >> added Imports: Rcpp                                                                                                                                                                                                    
 >> added LinkingTo: Rcpp                                                                                    
 >> added useDynLib directive to NAMESPACE            
 >> added importFrom(Rcpp, evalCpp) directive to NAMESPACE
 >> added example src file using Rcpp attributes      
 >> added Rd file for rcpp_hello_world                                                                                                                                                                                     
 >> compiled Rcpp attributes                                                                                 
 edd@rob:/tmp/flappix$

第二步:复制完全三个源文件

edd@rob:/tmp/flappix$ cp -vax header.h def.cpp call.cpp stubborn/src/                                        
'header.h' -> 'stubborn/src/header.h'                 
'def.cpp' -> 'stubborn/src/def.cpp'                      
'call.cpp' -> 'stubborn/src/call.cpp'  
edd@rob:/tmp/flappix$

第三步:运行compileAttributes()更新导出

edd@rob:/tmp/flappix$ cd stubborn/                         
edd@rob:/tmp/flappix/stubborn$ Rscript -e 'Rcpp::compileAttributes()'
edd@rob:/tmp/flappix$

第四步:构建软件包

edd@rob:/tmp/flappix/stubborn$ R CMD build .             
* checking for file ‘./DESCRIPTION’ ... OK                                                                   
* preparing ‘stubborn’:                                                                                      
* checking DESCRIPTION meta-information ... OK        
* cleaning src                                                                           
* installing the package to process help pages        
* saving partial Rd database                                                                                 
* cleaning src                                        
* checking for LF line-endings in source and make files and shell scripts                                    
* checking for empty or unneeded directories          
* building ‘stubborn_1.0.tar.gz’                                                                             
edd@rob:/tmp/flappix$                                                          

第五步:安装

edd@rob:/tmp/flappix/stubborn$ R CMD INSTALL stubborn_1.0.tar.gz
* installing to library ‘/usr/local/lib/R/site-library’
* installing *source* package ‘stubborn’ ...                                                                 
** libs                                               
ccache g++ -I/usr/share/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include"    -fpic  -g -O3 -Wall -pipe  -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-deprecated-declarations -marc
h=native -c RcppExports.cpp -o RcppExports.o          
ccache g++ -I/usr/share/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include"    -fpic  -g -O3 -Wall -pipe  -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-deprecated-declarations -marc
h=native -c call.cpp -o call.o                                                                 
ccache g++ -I/usr/share/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include"    -fpic  -g -O3 -Wall -pipe  -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-deprecated-declarations -marc
h=native -c def.cpp -o def.o                                                          
ccache g++ -I/usr/share/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include"    -fpic  -g -O3 -Wall -pipe  -Wno-misleading-indentation -Wno-unused -Wno-ignored-attributes -Wno-deprecated-declarations -marc
h=native -c rcpp_hello_world.cpp -o rcpp_hello_world.o   
ccache g++ -Wl,-S -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o stubborn.so RcppExports.o call.o def.o rcpp_hello_world.o -L/usr/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/stubborn/libs                  
** R                                                                                                         
** preparing package for lazy loading                    
** help                                                          
*** installing help indices                           
** building package indices                                                                                  
** testing if installed package can be loaded
* DONE (stubborn)
edd@rob:/tmp/flappix/stubborn$ 

它显然会加载自动链接src/中的所有源文件。

答案 1 :(得分:2)

sourceCpp适用于编译和链接的单个翻译单元,通常意味着单个C ++文件。您可以使用#include def.cpp,但我发现它很奇怪,并且需要使用与包源不同的文件。但是,我真的想知道为什么要在已经从文件集中构建软件包时想要获取文件。