Rcpp-sourceCpp-未定义符号 [英] Rcpp - sourceCpp - undefined symbol

查看:70
本文介绍了Rcpp-sourceCpp-未定义符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Rcpp to get self-written C++ into R.

我有3个以下C ++文件

I have the 3 follwing C++ files

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

After compiling successfully I can call the function callx() as well as x() from R

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

但是当我尝试进行文件调用时。 cpp使用 cppSource 我得到这个错误

But when I try to source the file call.cpp using cppSource I get this error

> 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 进行编译,其中包括以下步骤:

For compiling I use R CMD INSTALL which includes the follwing steps:

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


推荐答案

真的不理解为什么有些人如此反对创建软件包。

I really do not understand why some folks are so opposed to creating packages.

这里很快复制并粘贴与五个命令相对应的充满输出的屏幕使用确切的输入文件为我提供确切的信息

Here is a quick copy and paste of a screen full of output corresponding to five commands giving me exactly what you wanted with your exact input files.

五个简单步骤。

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$



第二步:复制您的确切三个源文件



Step Two: Copy your exact three sources files

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()更新出口



Step Three: Run compileAttributes() to update the exports

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



第四步:构建程序包



Step Four: Build the package

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$                                                          



第五步:安装它



Step Five: Install it

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 / 中的所有源文件。

And it obviously loads as it automagically links against all source files in src/.

这篇关于Rcpp-sourceCpp-未定义符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆