我正在尝试使用 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)
```
并创建我在下面包含的内容。
关于c - 在 Rmarkdown 中为 C 使用 gcc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40808946/