dfeich / org-babel-examples

Examples using emacs org mode babel inline source code with different backend languages
570 stars 116 forks source link

Problems related to c-babel examples #1

Closed lazysquid closed 7 years ago

lazysquid commented 7 years ago

Hello. I was testing your nice ob-C examples.

When I was testing this https://github.com/dfeich/org-babel-examples/blob/master/C/c-babel.org#42-passing-a-table, my Emacs spitted out errors in below. Only homogeneous lists are supported by C. You can not mix char and int

What am I missing?

I'm using Org mode 8.2.10 and the document used Org mode 8.2.8. So I am guessing there are some deprecated function between Org mode v 8.2.10 and 8.2.8..

dfeich commented 7 years ago

Hi You need to look at what input your C-compiler really gets from babel. For this it is easiest to use the M-x org-babel-expand-src-block command. I show here what boilerplate code is generated by my org version on top of the main function.

So, the following src header definition

   #+begin_src C++ :results output :var somedata=somedata :includes "<string.h> <stdio.h> <stdlib.h>"

generates this C boilerplate

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

const char* somedata[5][3] = {
 {"zero","0","0.23"},
 {"one","1","1.31"},
 {"two","4","4.61"},
 {"three","9","9.05"},
 {"four","16","16.55"}
};
const int somedata_rows = 5;
const int somedata_cols = 3;
int get_column_num (int nbcols, const char** header, const char* column)
{
  int c;
  for (c=0; c<nbcols; c++)
    if (strcmp(header[c],column)==0)
      return c;
  return -1;
}

const char* somedata_header[3] = {"nb","sqr","noise"};
const char* somedata_h (int row, const char* col) { return somedata[row][get_column_num(3,somedata_header,col)]; }

/* here now starts the main function */

My guess is that your generated code looks different. Now, I have had this example for quite some time, but regrettably had forgotten to check it in. I am sure that it has been working for the last 2 years, and the C babel code is older than that. Would be interesting to understand what exactly causes your error. Please try to expand the code as suggested and have a look or post it.

Cheers, Derek

lazysquid commented 7 years ago

Hello again.

I did M-x org-babel-expand-src-block and my Emacs showed same error. However I found that my org babel C implementation which is ob-C.el is quite old. And I found new ob-C.el implementation in here. (very bottom of the page) I evaluated those codes in Scratch buffer and run your again then my Emacs showed proper results. I vaguely guess this is the main problem.

If you don't mind would you compare your ob-C.el and the ob-C.el in here?

Thank you for your time. Wonjun

dfeich commented 7 years ago

Hi I do not quite get what you mean by "M-x org-babel-expand-src-block and my Emacs showed same error". The expand command (with cursor within the src block!) should bring you into a buffer where you see the complete, generated C code for that particular src block. There you can also see how the table gets implemented within this particular babel language. You seem to have an old org-mode version, and I am sure that you will notice that in the expanded buffer. Your generated boilerplate code will be different from the one I posted above.

Now, as to a newer org version with a newer ob-C.el. My ob-C.el mostly matches the newer one posted on worg. But this is part of current org versions and has been for quite some time. Where does your org-mode come from? Is it the default version within your emacs? Then maybe you should use the package manager to get a more recent version (q.v. http://orgmode.org/elpa.html).

lazysquid commented 7 years ago

M-x org-babel-expand-src-block showed Only homogeneous lists are supported by C. You can not mix char and int) and didn't show expansion results.

Currently I'm using Emacs 25.1. And I'm using built in org mode which is version 8.2.10

The problem is resolved after I install bleeding edge org mode(version 9.0.3) and org-plus-contrib package.

I guess built-in org mode is quite different from org-mode comming from org repo.

Thank you for your help.

dfeich commented 7 years ago

Ok, I guess I understand now. The message was generated by the old ob-C.el when trying to expand the source block. So you never got to see the expanded block, but just got the error.

And indeed, when looking up the code in my own 25.1 emacs ob-C.el I can see

           (error "Only homogeneous lists are supported by C.  You can not mix %s and %s"
              type-name
              tmp-type-name)))

Sad that the new code is not yet included in the newest emacs releases. It has been in the org stable releases for a long time.

Cheers and welcome