Ary*_*man 2 c gcc rstudio r-markdown
我正在尝试使用 gcc 在 Rmarkdown 中运行 C 代码。当我尝试运行以下块时:
{R engine='c' engine.path='/usr/bin/gcc'}
#include <stdio.h>
int main()
{
printf("hello, world\n"); // say hello world
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:Error: unexpected symbol in "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)
```
Run Code Online (Sandbox Code Playgroud)
并创建了我在下面包含的内容。