我尝试运行来自 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/