[英]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_func
和matrix
在顶部文件my_func_r.cpp
。
但是,这对于解决代码维护尤其令人感到不满意。 完成我想做的最简单的方法是什么?
快速的:
src/
目录。 libmatrix.a
并链接到该目录。 这可以通过一个简单的src/Makevars
但仍然不建议这样做 。 继续阅读。 matrix.h
和matrix.c
复制到src/
,调整包含路径,即可完成。 sourceCpp()
。 不是为了这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.