2

I'm trying to execute C and/or C++ snippets in Org Babel. For example, I want to be able to execute the following snippet with C-c C-c:

#+begin_src C
printf ("Hello World!");
#+end_src

The documentation states that there are two installation steps: (1) having a C compiler available from PATH, and (2) having C added to the Org Babel load languages variable.

When I do this and I try to run the above code snippet, I get the following message:

Evaluation of this C code block is disabled.

The message suggests that there is something wrong with this particular code block (the use of the world this), but other code snippets result in the same message.

Was anybody able to run C and/or C++ code blocks in Org Babel? Were more installation steps needed?

I'm using Org mode 9.1.2 and GNU Emacs 25.3.1. Org Babel code block evaluation does work for other languages (e.g., SPARQL, SML, ELisp).

Wouter Beek
  • 289
  • 3
  • 14
  • Are the source code block lines above the only text in the org file? Note that you can explicitly enable and disable a programming language in `org-babel-load-languages`. Double-check that `Activated` is really `on` for `C`! Is the value of `org-babel-C-compiler` meaningful on your system? – Tobias Oct 06 '17 at 20:08
  • @Tobias Yes, the Org file only contains the hello world code block. `(eval org-babel-C-compiler)` gives `gcc`. I'm not sure what `Activated` is? `(eval org-babel-load-languages)` gives `Invalid function: (C . t)`. – Wouter Beek Oct 06 '17 at 20:38
  • You cannot evaluate the value of `org-babel-load-language`. Nevertheless, the `(C . t)` in the error message of your attempt already shows that `C` is activated. Do you have `gcc` installed on your system? – Tobias Oct 06 '17 at 23:50
  • @Tobias Yes, I'm using GCC 7.2.1. I'm able to use GCC from within Emacs, `M-x compile` in C/C++ mode and such. – Wouter Beek Oct 07 '17 at 07:43
  • Strangely, I'm running into something similar, except it does evaluate in emacs 25.3 and does not evaluate in emacs 26 nightly. @WouterBeek which version of emacs are you running? – amitp Oct 08 '17 at 04:07
  • @amitp My `emacs-version` is "GNU Emacs 25.3.1 (x86_64-redhat-linux-gnu, GTK+ Version 3.22.19) of 2017-09-15". – Wouter Beek Oct 08 '17 at 12:07
  • Try `#+BEGIN_SRC C++`, it works for me (Emacs 25.1.1 on Mac). – AhLeung Oct 09 '17 at 08:11
  • @AhLeung Thanks for the suggestion, but I get the same behavior for C and C++ :( Are more people experiencing this? User `amitp` has observed the same behavior in at least one instance. Maybe this should become a bug report? – Wouter Beek Oct 09 '17 at 11:45
  • I've got also emacs 25.3.1 under Cygwin. The emacs version is not so relevant here. More important is `M-x` `org-version` which is 9.0.9 on my box. Please, add the version numbers as info to your question. – Tobias Oct 09 '17 at 11:56
  • 1
    See that orgmode-ticket: https://github.com/syl20bnr/spacemacs/issues/7641. There they say that removing all the compiled files `*.elc` and byte-compiling the sources does the trick. – Tobias Oct 09 '17 at 12:59
  • 1
    Possible duplicate of: https://emacs.stackexchange.com/questions/28441/org-mode-9-unable-to-eval-code-blocks – Tobias Oct 09 '17 at 13:01
  • I've added the Org mode version (9.1.2) and the GNU Emacs version (25.3.1) to the question. – Wouter Beek Oct 09 '17 at 18:00
  • I've also recompiled all `*.elc` files in `~/.emacs/elpa`, and I've uninstalled and then reinstalled org mode through the package manager (starting+closing Emacs in between these steps). The issue is still the same. Evaluation of other languages does work for me. Only not for C/C++. – Wouter Beek Oct 09 '17 at 18:10
  • FWIW, it works fine for me: GNU Emacs 27.0.50, Org mode version 9.1.2. – NickD Oct 09 '17 at 18:42
  • I read the link Tobias had and … it had a comment from … me. How embarrassing. I reinstalled org and the problem disappeared for me. I don't know why it happened but my guess is I had a version mismatch. I'm not sure if the problem I had is the same thing @WouterBeek has. – amitp Oct 09 '17 at 20:00
  • Then there is a remote chance that emacs' own org-mode interferes... Try `M-x` `locate-library` `org` and `M-x` `locate-library` `ob-C`. – Tobias Oct 09 '17 at 20:26
  • 1
    The fix was to do `rm ~/.emacs/elpa/org-20171009/*.elc` and then restart Emacs. The overall issues seems to be an inconsistency between Org mode versions. Very annoying that updating/installing Org does not automatically rebuild the `*.elc` files :( – Wouter Beek Oct 10 '17 at 02:00

2 Answers2

1

The solution was basically given by @Tobias who shared the following links:

It is important to note that removing Org, restarting Emacs, and installing Org did not work for me. And C-0 M-x byte-recompile-directory RET ~/emacs.d/elpa/org-20171009 did not work either.

The only thing that worked for me was rm ~/.emacs/elpa/org-20171009/*.elc and then restart Emacs.

Wouter Beek
  • 289
  • 3
  • 14
0

The Evaluation of this C code block is disabled. message is produced by org-babel-check-evaluate which in turn calls org-babel-check-confirm-evaluate to do the check. See the doc strings for these functions for details. The latter function checks for :noeval headers and :eval headers with values "no" or "never" or "no-export" or "never-export". In all these cases, the evaluation is disabled.

I would instrument org-babel-check-confirm-evaluate for edebug, and step through it to find out why it is returning nil, and go from there.

NickD
  • 27,023
  • 3
  • 23
  • 42