简体   繁体   English

Rcpp:从另一个c ++函数调用c ++函数时出错

[英]Rcpp: error in calling a c++ function from another c++ function

I'm writing my functions in C++ to use them in R. Since I don't want to include all the functions inside the same file, I want to call them. 我正在用C ++编写我的函数以在R中使用它们。因为我不想在同一个文件中包含所有函数,所以我想调用它们。 I'll give you a simple example of the three files I'm using: 我将给你一个我正在使用的三个文件的简单示例:

function.h : function.h

#ifndef FUNCTION_H    
#define FUNCTION_H

#include <RcppArmadillo.h>

arma::vec quadraticsum(arma::vec x);

#endif

function.cpp : function.cpp

 #include <RcppArmadillo.h>
 #include <function.h>
 // [[Rcpp::depends(RcppArmadillo)]]

 using namespace Rcpp;
 using namespace arma;
 using namespace std;

 // [[Rcpp::export]]

 arma::vec quadraticsum(arma::vec x){
   arma::vec results = sum(pow(x,2));
   return results;
}

main.cpp : main.cpp

#include <RcppArmadillo.h>
#include <function.h>
// [[Rcpp::depends(RcppArmadillo)]]
using namespace Rcpp;
using namespace arma;
using namespace std;

// [[Rcpp::export]]

arma::vec sum2(arma::vec x){
  arma::vec results = quadraticsum(x)+2;
return results;
}

I'm working with Rstudio and when I write the code in the main.cpp file it recognizes the function quadraticsum , and so everything seems to be fine. 我正在使用Rstudio,当我在main.cpp文件中编写代码时,它识别出函数quadraticsum ,所以一切似乎都很好。 However, when I compile using the command sourceCpp("~/main.cpp") , I got this error: 但是,当我使用命令sourceCpp("~/main.cpp")编译时,我收到此错误:

Error in dyn.load("/private/var/folders/46/1tz_54_n3glfmgftvqsspwrr0000gn/T/Rtmpdnk9hf/sourceCpp-x86_64-apple-darwin13.4.0-0.12.12/sourcecpp_237a88636e6/sourceCpp_2.so") : unable to load shared object '/private/var/folders/46/1tz_54_n3glfmgftvqsspwrr0000gn/T/Rtmpdnk9hf/sourceCpp-x86_64-apple-darwin13.4.0-0.12.12/sourcecpp_237a88636e6/sourceCpp_2.so': dlopen(/private/var/folders/46/1tz_54_n3glfmgftvqsspwrr0000gn/T/Rtmpdnk9hf/sourceCpp-x86_64-apple-darwin13.4.0-0.12.12/sourcecpp_237a88636e6/sourceCpp_2.so, 6): Symbol not found: __Z12quadraticsumN4arma3ColIdEE Referenced from: /private/var/folders/46/1tz_54_n3glfmgftvqsspwrr0000gn/T/Rtmpdnk9hf/sourceCpp-x86_64-apple-darwin13.4.0-0.12.12/sourcecpp_237a88636e6/sourceCpp_2.so Expected in: flat namespace in /private/var/folders/46/1tz_54_n3glfmgftvqsspwrr0000gn/T/Rtmpdnk9hf/sourceCpp-x86_64-apple-darwin13.4.0-0.12.12/sourcecpp_237a88636e6/sourceCpp_2.so dyn.load中的错误(“/ private / var / folders / 46 / 1tz_54_n3glfmgftvqsspwrr0000gn / T / Rtmpdnk9hf / sourceCpp-x86_64-apple-darwin13.4.0-0.12.12 / sourcecpp_237a88636e6 / sourceCpp_2.so”):无法加载共享对象' /private/var/folders/46/1tz_54_n3glfmgftvqsspwrr0000gn/T/Rtmpdnk9hf/sourceCpp-x86_64-apple-darwin13.4.0-0.12.12/sourcecpp_237a88636e6/sourceCpp_2.so':dlopen(/ private / var / folders / 46 / 1tz_54_n3glfmgftvqsspwrr0000gn / T /Rtmpdnk9hf/sourceCpp-x86_64-apple-darwin13.4.0-0.12.12/sourcecpp_237a88636e6/sourceCpp_2.so,6):找不到符号:__ Z12quadraticsumN4arma3ColIdEE引自:/ private / var / folders / 46 / 1tz_54_n3glfmgftvqsspwrr0000gn / T / Rtmpdnk9hf / sourceCpp -x86_64-apple-darwin13.4.0-0.12.12 / sourcecpp_237a88636e6 / sourceCpp_2.so预期在:/private/var/folders/46/1tz_54_n3glfmgftvqsspwrr0000gn/T/Rtmpdnk9hf/sourceCpp-x86_64-apple-darwin13.4.0-0.12中的平面命名空间0.12 / sourcecpp_237a88636e6 / sourceCpp_2.so

Have you seen this problem before? 你以前见过这个问题吗? I'm using macOS 10.12.5. 我正在使用macOS 10.12.5。 Thank you all. 谢谢你们。

sourceCpp only allows a single source file. sourceCpp只允许单个源文件。

If you want to use multiple source files, you will need to build a full package. 如果要使用多个源文件,则需要构建完整的包。

The error appears because the second source file has not been compiled or linked into the shared library. 出现错误是因为第二个源文件尚未编译或链接到共享库中。 As a result, no function implementation exists. 因此,不存在任何功能实现。

You could also make the implementations static or inline and then place them in a header, of you want to avoid a full package. 您也可以将实现设置为staticinline ,然后将它们放在标题中,您要避免使用完整的包。 if 如果

I discovered that writing #include "function.h" instead of #include <function.h> it compiles correctly. 我发现编写#include "function.h"而不是#include <function.h>它正确编译。 I just changed it. 我刚改变了 Thank you all. 谢谢你们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM