c - 在 Rmarkdown 中为 C 使用 gcc

标签 c gcc rstudio r-markdown

我正在尝试使用 gcc 在 Rmarkdown 中运行 C 代码。当我尝试运行以下 block 时:

{R engine='c' engine.path='/usr/bin/gcc'}
#include <stdio.h>

int main()
{
    printf("hello, world\n"); // say hello world
}

我收到以下错误:错误:“int main”中出现意外符号。我的 gcc 可执行文件有正确的路径,我也试过 /usr/bin/clang。我在 11"MacBook Air 上使用 Rstudio。

最佳答案

真正想做什么? Rmarkdown 无法为您构建带有 main()可执行文件,但它已经集成了 Rcpp 很长时间了。

以下“有效”:

---
title: "RMarkdown Demo"
author: "Dirk"
date: "November 25, 2016"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## C++ Code

```{r engine='Rcpp'}
#include <Rcpp.h>

// [[Rcpp::export]]
int fibonacci(const int x) {
    if (x == 0 || x == 1) return(x);
    return (fibonacci(x - 1)) + fibonacci(x - 2);
}
```

## Deployed

```{r}
fibonacci(10L)
fibonacci(20L)
```

并创建我在下面包含的内容。

enter image description here

关于c - 在 Rmarkdown 中为 C 使用 gcc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40808946/

相关文章:

c++ - 如何以编程方式查找进程的所有文件句柄?

c - WinApi32 C 滚动条控件( slider )

c - 使用 clang 与 gcc 进行 union 零初始化

c - 错误 : _mm_clmulepi64_si128 was not declared in this scope

r - 如何定义函数以便 R studio 可以预测其变量?

r - 将 RStudio 演示文稿 (.Rpres) 转换为 rmarkdown 演示文稿 (.Rmd)

使用 Rstudio 时 Ubuntu 16.04 卡住(加载字形失败)

检查矩阵

linux - 执行二进制文件

无法从 scanf 获取字符串的正确输入(作为数组的字符)