R 尝试编译时抛出错误

发布于 2025-01-02 18:56:29 字数 682 浏览 1 评论 0原文

我正在尝试使用 R 生成 2 个文件,第一个文件实际上生成得很好,但第二个文件告诉我以下内容:

conteo_correlaciones.c:3:15:错误:Rh:没有这样的文件或目录 conteo_correlaciones.c:4:24:错误:Rinternals.h:没有这样的文件或 目录 conteo_correlaciones.c:31: 错误: 预期为 '=', ',', ';', “countcorrelations”之前的“asm”或“属性

Rh 和 Rinternals.h 都是从 R 目录复制的,它们与其余文件位于同一目录中,但我无法获取过去这个错误,这些文件被这样的包含调用:

#include <R.h>
#include <Rinternals.h>

我在终端中所做的是:

R CMD SHLIB conteo_correlaciones.c

哪个工作很好并生成正确的文件并且:

gcc -dynamiclib conteo_correlaciones.c -o conteo_correlaciones.so

哪个向我抛出了上述错误。知道会是什么吗?

I am trying to use R to generate 2 files the first one actually generates pretty good but the 2nd one tells me the following:

conteo_correlaciones.c:3:15: error: R.h: No such file or directory
conteo_correlaciones.c:4:24: error: Rinternals.h: No such file or
directory conteo_correlaciones.c:31: error: expected ‘=’, ‘,’, ‘;’,
‘asm’ or ‘attribute’ before ‘countcorrelations’

both R.h and Rinternals.h were copied from the R directory and they're in the same directory that the rest of the files but I can't get past this error, these files are being called by an include like this:

#include <R.h>
#include <Rinternals.h>

and what I'm doing in the terminal is:

R CMD SHLIB conteo_correlaciones.c

Which works great and generates me the correct file and:

gcc -dynamiclib conteo_correlaciones.c -o conteo_correlaciones.so

Which throws me the aforementioned error. Any idea what could be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

奢华的一滴泪 2025-01-09 18:56:29

R CMD SHLIB 添加所有适当的标志来编译您的代码。您无法(可靠地)“手动”编译代码。 R CMD SHLIB 已经创建了所有必需的文件,因此您不需要自己运行 gcc

例如,这就是 R CMD SHLIB 运行的内容:

$ R CMD SHLIB foo.c
gcc -arch x86_64 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include 
  -I/Library/Frameworks/R.framework/Resources/include/x86_64 
  -I/usr/local/include    -fPIC  -g -O2 -c foo.c -o foo.o
gcc -arch x86_64 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names
  -undefined dynamic_lookup -single_module -multiply_defined suppress
  -L/usr/local/lib -o foo.so foo.o -F/Library/Frameworks/R.framework/..
  -framework R -Wl,-framework -Wl,CoreFoundation

如您所见,创建正确的二进制文件需要一长串标志。第一行是编译,第二行是链接。正如您所看到的,它甚至与您尝试“手动”执行的操作相差甚远。

至于确切的错误 - 这只是您会得到的众多错误之一 - 在上述情况下,您需要使用 -I 包含 R 的包含路径。还有其他的,所以你不想去那里,只需坚持使用 R CMD SHLIB 即可。

R CMD SHLIB adds all appropriate flags to compile your code. You cannot (reliably) compile your code "by hand". R CMD SHLIB already creates all necessary files, so you don't need to run gcc yourself.

For example, this is what R CMD SHLIB runs:

$ R CMD SHLIB foo.c
gcc -arch x86_64 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include 
  -I/Library/Frameworks/R.framework/Resources/include/x86_64 
  -I/usr/local/include    -fPIC  -g -O2 -c foo.c -o foo.o
gcc -arch x86_64 -std=gnu99 -dynamiclib -Wl,-headerpad_max_install_names
  -undefined dynamic_lookup -single_module -multiply_defined suppress
  -L/usr/local/lib -o foo.so foo.o -F/Library/Frameworks/R.framework/..
  -framework R -Wl,-framework -Wl,CoreFoundation

As you can see there is a long list of flags that are needed to create the proper binary. The first line is the compilation, the second one is linking. As you can see it's not even close to what you were trying to do "by hand".

As for the exact error - it's just one of many that you would get - in the above case you need to include R's include path with -I. There are others as well, so you don't want to go there, just stick with R CMD SHLIB.

纸短情长 2025-01-09 18:56:29

必须进行更改

#include <R.h>
#include <Rinternals.h>

#include "R.h"
#include "Rinternals.h"

如果头文件与代码文件位于同一目录中,则
或者您可以将正确的包含路径添加到 gcc 命令行(例如 -I/usr/share/R/include)。

You have to change

#include <R.h>
#include <Rinternals.h>

to

#include "R.h"
#include "Rinternals.h"

if the header files are in the same directory like your code files.
Or you could add the correct include-path to gcc commandline (e.g. -I/usr/share/R/include).

陌路黄昏 2025-01-09 18:56:29

首先找到 Rh 的存储位置(它可以位于 /usr/include 或 /usr/local/include 中,或者如果 R 是从源安装的,则可以位于任何位置)。就我而言,它位于 /home/user/R/include/ 中:

然后导出路径:

export CPATH=/home/user/R/include/
export C_INCLUDE_PATH=/home/user/R/include/
export CPLUS_INCLUDE_PATH=/home/user/R/include/
export GCC_INCLUDE_DIR=/home/user/R/include/

然后编译应该可以正常运行。

First locate where R.h is stored (it could be in /usr/include or /usr/local/include or anywhere if R has been installed from sources). In my case it is in /home/user/R/include/:

Then export the paths:

export CPATH=/home/user/R/include/
export C_INCLUDE_PATH=/home/user/R/include/
export CPLUS_INCLUDE_PATH=/home/user/R/include/
export GCC_INCLUDE_DIR=/home/user/R/include/

The compilation should then run fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文