运行 来自 org babel 的 C 完成程序

Run a C's completed program from org babel

我尝试 运行 来自 C-Primer-Plus 的样本

Listing 2.1 The first.c Program
#+BEGIN_SRC C :results output
#include <stdio.h>
int main(void)                /* a simple program             */
{
    int num;                  /* define a variable called num */
    num = 1;                  /* assign a value to num        */

    printf("I am a simple "); /* use the printf() function    */
    printf("computer.n");
    printf("My favorite number is %d because it is first.n",num);

    return 0;
}
#+END_SRC

它报告神秘错误为

/tmp/babel-xEtnj6/C-src-mefAEj.c:9:15: error: stray ‘2’ in program
    9 | int main(void)                /* a simple program             */
      |               ^
/tmp/babel-xEtnj6/C-src-mefAEj.c:9:16: error: stray ‘0’ in program
    9 | int main(void)                /* a simple program             */
      |                ^
/tmp/babel-xEtnj6/C-src-mefAEj.c:9:17: error: stray ‘2’ in program
    9 | int main(void)                /* a simple program             */

如果 main() 被删除,它会起作用

#+BEGIN_SRC C
printf("Literature Programming");
#+END_SRC

#+RESULTS:
: Literature Programming

遗憾的是,大多数 C 代码都封装在“main”中

第一个示例如何运行?

您可以尝试将 :main no 添加到您的代码块

#+BEGIN_SRC C :results output :main no
#include <stdio.h>

int main(void)                /* a simple program             */
{
    int num;                  /* define a variable called num */
    num = 1;                  /* assign a value to num        */

    printf("I am a simple "); /* use the printf() function    */
    printf("computer.n");
    printf("My favorite number is %d because it is first.n",num);

    return 0;
}
#+END_SRC

另请注意,还有其他有用的修饰符,如 :flags:lib:cmdline...请参阅 Header Arguments for C, C++, D Source Code Blocks 了解更多详细信息。