Skip to content

Latest commit

 

History

History
219 lines (169 loc) · 5.73 KB

c-babel.org

File metadata and controls

219 lines (169 loc) · 5.73 KB

c-babel

1 Links

2 Compiler version

gcc --version

3 test

Both C and C++ get activated by adding C to the org-babel-load-languages.

If there is no main function present, babel will wrap one around the code. It also will add some of the standard include files needed to compile the code.

printf("Hello World");

The equivalent example with explicitely writing the include and the main function.

#include "stdlib.h"
int main(int argc,char **argv) {
  printf("Hello World");
  exit(0);
}

4 Passing variables by header arguments

4.1 passing simple types

printf ("mystring %s\n", mystring);
printf ("myint    %d\n", myint);
printf ("mydouble %g\n", mydouble);

Using CALL to call the source block as a function

An example with C++ and including a header file using the :includes header argument. Note that one can look at the resulting code before compilation by using org-babel-expand-src-block (by default assigned to C-c C-v v)

std::cout << "Hello World!\n";

4.2 passing a table

nbsqrnoise
zero00.23
one11.31
two44.61
three99.05
four1616.55

The following code only works if the string.h header is included via the header. Even though the src block does not require a function from that library by itself, the boiler plate code inserted by babel to parse the table uses strncmp. The boiler plate code gets inserted before the source block, so includes defined within the source block are read in too late. But the includes defined through the header arguments are inserted before the boiler plate.

This I consider a bug which I should report to the developers.

int main()
 {
   for(int j=0; j<somedata_cols; j++) { printf("%s  ", somedata_header[j]); };
   printf("\n");
   for (int i=0; i<somedata_rows; i++) {
     printf ("%2d %7s ", i, somedata_h(i,"nb"));
     for (int j=1; j<somedata_cols; j++) {
       const char* cell = somedata[i][j];
       printf ("%5s %5g ", cell, 1000*atof(cell));
     }
     printf("\n");
   }
   return 0;
 }

5 noweb syntax

First we define named code blocks

void myfunc() {
  printf("print from function\n");
}
int main(int argc,char **argv) {
  printf("Hello World\n");
  myfunc();
  exit(0);
}

Now we define a block which includes the other code blocks (needs the :noweb yes option). We could tangle this block, but we can also execute it directly.

#include "stdlib.h"
#include "stdio.h"

<<srcMyfunc>>
<<srcMain>>

6 C++11 examples

Passing multiple includes requires defining them within a list. ERROR: Why can I not use the :results output syntax?

std::vector<std::complex<double>> v{{0,0}, {1,0}, {0,1}};
for (auto &iter: v) {
  std::cout << iter << ' ';
 }

An example with lambda functions

using namespace std;

float vf0[5] = {1.2, 3.4, 5.1, 8.4, 9.9};
function<void(float&)> out = [](float &f) {cout << f << ' '; };
// function<void(float&)> mult = [&fac](float &f) {f* = fac; };

for_each(vf0, vf0+5, out);
cout << " (vf0)\n";

Local Variables: org-confirm-babel-evaluate: nil End: