C, C++, D Source Code Blocks in Org Mode

Table of Contents

Org Mode support for
C, C++
D

Introduction

Note: a new version of both the software and the documentation is under evaluation. It is available here: ob-doc-C-new

Babel can evaluate C, C++, and D code.

As opposed to interpreted languages, which can be evaluated directly, C, C++, and D 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.

Note: there used to be two separate library files, ob-C.el and ob-D.el. They have been merged in a single ob-C.el file which handle all three languages.

About C

C dates back in the 1970. It was devised by Kernighan & Ritchie. It was used to create the Unix kernel, and many of its utilities. Today it is still the base of the Linux & Unix kernel.

About C++

C++ was devised by Stroustrup in the 1980. The purpose was to enhance C with object programming. Among the features introduced by C++, there are:

  • templates and the Standard Template Library,
  • object programming, with class definition and inheritance,
  • functions and operators overloading
  • exceptions

About D

D is a C++-like language made by Digital Mars. It features:

  • C++ syntax
  • Built-in garbage collector
  • Strong type system
  • Meta-programming
  • Seamless assembler support
  • Usable as a scripting language
  • C binary compatibility
  • and much more.

Requirements and Setup

1- You must have the compilers available on your computer. You may use only one of the three languages: there is no requirement to have all three installed.

  • C and C++ often come pre-installed. Popular compilers are the GNU ones, called gcc and g++. But others are usable as well.
  • For D, look at http://dlang.org/ for downloading and instructions. The compilers are called dmd and rdmd.

Eventually, the compilers must be in the PATH.

2- Make any or all languages available to Babel. Type:

M-x customize-variable org-babel-load-languages

and add the language of your choice.

Org Mode Features for D Source Code Blocks

Header Arguments

:var VARIABLE=VALUE
A global C, C++, or D variable named VARIABLE will be declared and initialized with VALUE

Possible types for VARIABLE may be:

int,
double,
string or const char*,
type[]    // type = int, double, string, const char*
type[][]  // type = int, double, string, const char*

The later type, type[][], is used for variables storing Org tables

The type[] is used for lists or vectors declared in the header

:cmdline
command line arguments to pass to the executable compiled from the code block
:flags
flags to pass to the compiler
:main
can be set to "no" to inhibit wrapping of the code block in a main function call
(no term)
:includes (C & CC+ only):: accepts either a single string name, or a list of names of files to #include in the execution of the code block
:import package (D only)
An import statement will be declared in the D source before the source code
(no term)
:defines (C & C++ only):: just like :includes but for #defines lines at the top of the code

Sessions

There is no support for sessions

Examples of Use

Hello World in C & C++

Here is Hello World!

This source code block:

#+begin_src C
  #include "stdio.h"
  printf ("Hello World!");
#+end_src

Note that:

  • no main() is declared, a trivial one is automatically provided,

Hello World in D

Here is Hello World!

This source code block:

#+begin_src D
  import std.stdio;
  writefln ("Hello World!");
#+end_src

yields this result:

Hello World!

Scalar variables

Variables may be declared outside the script. They are automatically inserted at the top of the script. Three types are supported, based on the look of the value:

  • string or const char*
  • int
  • double

Example in C or C++:

#include "stdio.h"
printf ("mystring %s\n", mystring);
printf ("myint    %d\n", myint);
printf ("mydouble %g\n", mydouble);

This source code block:

#+header: :var mystring="Sunday" :var myint=145 :var mydouble=3.14
#+BEGIN_SRC C
  #include "stdio.h"
  printf ("mystring %s\n", mystring);
  printf ("myint    %d\n", myint);
  printf ("mydouble %g\n", mydouble);
#+END_SRC

yields this result:

mystring Sunday
myint 145
mydouble 3.14

Example in D:

import std.stdio;
writefln ("mystring %s", mystring);
writefln ("myint    %d", myint);
writefln ("mydouble %g", mydouble);
#+header: :var mystring="Sunday" :var myint=145 :var mydouble=3.14
#+BEGIN_SRC D
  import std.stdio;
  writefln ("mystring %s", mystring);
  writefln ("myint    %d", myint);
  writefln ("mydouble %g", mydouble);
#+END_SRC
mystring Sunday
myint 145
mydouble 3.14

Process an Org Mode Table

How to handle a table

Example of input table:

nb day
zero Sunday
one Monday
two Tuesday
three Wednesday
four Thursday
five Friday
six Saturday

The table is converted to a variable in the script:

  • const char* somedata[7][2] = {...}; // in C & C++
  • string somedata[7][2] = [...]; // in D

Beware that in the current version, input tables must be homogeneous: they must contain only integers, or only doubles, or only strings. This constraint will be removed in a future release.

Example in C & C++

This code:

#+header: :exports results
#+begin_src C++ :var somedata=somedata
  #include "stdio.h"
  int main()
  {
    for (int i=0; i<7; i++) {
      for (int j=0; j<2; j++)
        printf ("%s ", somedata[i][j]);
      printf("%d\n", i);
    }
    return 0;
  }
#+end_src

yields this result:

zero Sunday 0
one Monday 1
two Tuesday 2
three Wednesday 3
four Thursday 4
five Friday 5
six Saturday 6

Example in D

This code:

#+header: :exports results
#+begin_src D :var somedata=somedata
  import std.stdio;
  void main()
  {
    foreach (i, row; somedata) {
      foreach (j, cell; row)
        writef ("%s ", cell);
      writefln ("%s", i);
    }
  }
#+end_src

yields this result:

zero Sunday 0
one Monday 1
two Tuesday 2
three Wednesday 3
four Thursday 4
five Friday 5
six Saturday 6

Documentation from the http://orgmode.org/worg/ website (either in its HTML format or in its Org format) is licensed under the GNU Free Documentation License version 1.3 or later. The code examples and css stylesheets are licensed under the GNU General Public License v3 or later.