繁体   English   中英

在Rcpp中编译多个源文件

[英]Compiling multiple source files in Rcpp

我有以下目录结构

my_func
    - my_func_r.cpp
    - my_func.c
    - my_func.h
    - my_func_test.c
    - matrix/
      - matrix.h
      - matrix.c

matrix目录包含一些矩阵结构matrix.h和一些初始化,免费,打印等功能中matrix.c my_func.h文件类似于

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "matrix/matrix.h"

... some structures and templates ...

然后是my_func.c文件

#include "my_func.h"

... helper functions ...

int my_func(...) {
    ... my_func stuff ...     
    return 0;
}

my_func_test.c类似于

#include "my_func.h"

int main() {
    ... some test ...
    return 0;
}

使用gcc/g++我可以用

gcc my_func_test.c my_func.c matrix/matrix.c -o test -lm

最终文件my_func_r.cpp是之间的界面Rcpp结构和使用的结构my_func.c 目前是这样的

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

// [[Rcpp::export]]
int my_func_r(Rcpp::List x, ...) {
    ... convert inputs to structure recognised by my_func.h ...       
    ... run my_func.c ...
    ... put returned objects back into one of the R structure ...

    return 0;
}

我的问题是如果我现在跑步

sourceCpp('my_func_r.cpp', verbose=TRUE, rebuild=TRUE)

它抱怨位于matrix/matrix.c函数缺少符号。 一种解决方法是简单地从两个粘贴我所有的头文件和源代码my_funcmatrix在顶部文件my_func_r.cpp

但是,这对于解决代码维护尤其令人感到不满意。 完成我想做的最简单的方法是什么?

快速的:

  1. 这是不是真的特别RCPP 本身
  2. 您只是在R构建中挣扎着使用更高级/更复杂的src/目录。
  3. 在编写R扩展中有关于此的官方文档,以前在SO上已经提过这些问题。
  4. 可以先在子目录中编译libmatrix.a并链接到该目录。 这可以通过一个简单的src/Makevars仍然不建议这样做 继续阅读。
  5. 但这是一个自我伤害的伤口。 只需将matrix.hmatrix.c复制到src/ ,调整包含路径,即可完成。
  6. 与往常一样: 创建一个包 不要在较大的设置上使用sourceCpp() 不是为了这个

暂无
暂无

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

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