在 Rmarkdown 中为 C 使用 gcc

Using gcc for C in Rmarkdown

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

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

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

我收到以下错误:Error: unexpected symbol in "int main"。我的 gcc 可执行文件具有正确的路径,我也试过 /usr/bin/clang。我在 11" MacBook Air 上使用 Rstudio。

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

以下"just works":

---
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)
```

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