c - 从 org babel 运行 C 完成的程序

标签 c emacs org-mode 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 ‘\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”中。

如何让第一个示例正常工作?

最佳答案

您可以尝试将 :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了解更多详情。

关于c - 从 org babel 运行 C 完成的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59727012/

相关文章:

c - 防止 gcc 内联所有函数

c - 未找到 fatal error 'stdio.h'

objective-c - ATOI() 结果为 "Exited Unexpectedly/Lost Connection"

windows - Windows 中的 Emacs

emacs - 如何让 htmlize.el 在 Emacs 组织模式下为项目发布工作?

emacs - 组织模式下的内联图像

c++ - C/C++ 代码突出显示在网页中看起来很奇怪

python - 将行号传递给嵌入式 Python 解释器

emacs - 向 Emacs-Lisp 添加注释结束字符

emacs - 防止 Emacs 在按回车键时删除尾随空格