从org babel运行一个C完成的程序

bjg7j2ky  于 2023-04-29  发布在  Babel
关注(0)|答案(1)|浏览(130)

我试着从 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 ‘\302’ in program
    9 | int main(void)                /* a simple program             */
      |               ^
/tmp/babel-xEtnj6/C-src-mefAEj.c:9:16: error: stray ‘\240’ in program
    9 | int main(void)                /* a simple program             */
      |                ^
/tmp/babel-xEtnj6/C-src-mefAEj.c:9:17: error: stray ‘\302’ in program
    9 | int main(void)                /* a simple program             */

如果删除了main(),它可以工作:

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

#+RESULTS:
: Literature Programming

不幸的是,大多数C代码都封装在'main'中。
我怎样才能让第一个例子工作呢?

j8ag8udp

j8ag8udp1#

您可以尝试将: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。..有关更多细节,请参见C、C++、D源代码块的头参数。

相关问题