Using C and C++ code with Babel
Table of Contents
Introduction
Babel can evaluate C and C++ code. As opposed to interpreted
languages, which can be evaluated directly, C or C++ code is first
compiled to an executable which is then run. If a main method is
not present in a code block then the entire block is wrapped in a
trivial main function call.
So, for example, the following simple code block can be evaluated and the results of evaluation inserted into the buffer.
#+begin_src c++ :includes <stdio.h>
int a=1;
int b=1;
printf("%d\n", a+b);
#+end_src
#+results:
: 2
Language Specific Header Arguments
Babel provides the following C and C++ specific header arguments:
-
:cmdline - command line arguments to pass to the executable compiled from the code block
-
:flags - flags to pass to the C compiler
-
:main - can be set to "no" to inhibit wrapping of the code block
in a
mainfunction call -
:includes - accepts either a single string name, or a list of
names of files to
#includein the execution of the code block -
:defines - just like
:includesbut for#defineslines at the top of the code