【问题标题】:"undefined reference to " error when compiling C project with GNU GCC Compiler使用 GNU GCC 编译器编译 C 项目时出现“未定义的引用”错误
【发布时间】:2016-01-28 19:00:29
【问题描述】:

我希望有人可以提供帮助。我是 C 新手,我一直在尝试 R 项目中的一些代码。我创建的项目由一个 main.c 文件和几个头文件组成。 main.c 文件如下所示:

#include "Rinternals.h"
int main(int argc, char** argv) {     
}
SEXP Cdqrls(SEXP x, SEXP y, SEXP tol, SEXP chk)
{   
SEXP ans;
SEXP qr, coefficients, residuals, effects, pivot, qraux;
int n, ny = 0, p, rank, nprotect = 4, pivoted = 0;
double rtol = asReal(tol), *work;   
}

Rinternals.h 文件是来自 R 项目源的此文件:https://svn.r-project.org/R/trunk/src/include/Rinternals.h

我已将此头文件包含在与 main.c 文件相同的文件夹中,并且我还将 Rinternals.h #includes 语句中的所有头文件包含在此文件夹中。当我尝试编译它时,我收到以下错误:

||=== Build: Debug in MyNewProject (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `Cdqrls':|
main.c|30|undefined reference to `Rf_asReal'|

我尝试了很多方法,但似乎无法找到编译此代码的方法。我目前正在使用 Code::Blocks 并使用 GNU GCC Compiler 进行编译。对此的任何帮助将不胜感激。

【问题讨论】:

  • 你记得链接到 R 吗?仅仅包含头文件不足以让编译器与库链接。
  • 您确定要使用 C,而不是 C++,并且您确定要使用 SEXP,而不是 Rcpp(容易得多)吗?以及如何从控制台编译它?因为最好的方法是创建一个 R 包,在 RStudio 中加载它并单击 Build & Reload。

标签: c r gcc


【解决方案1】:

你需要将它与R库链接(也使用-I来指定包含库):

cc -O2 code.c -o code -I/usr/share/R/include -L/usr/lib/R/lib -lR

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-15
    • 2021-03-14
    • 2015-08-21
    • 2015-08-25
    • 2023-03-23
    • 1970-01-01
    • 2014-09-18
    相关资源
    最近更新 更多