Yes, you can do so by using the :post()
header argument.
I will present two solutions. The first one is simpler but has two
disadvantages. I would consider the second solution the way to go.
First solution
Consider the following Org Mode file. We can call the dash
code
block after the c++
code block is executed by executing
org-babel-execute-src-block
, which is executed by pressing C-c C-c
when the element at point is a source code block.
* Compilation errors in C++
#+NAME: compile-source-cpp
#+begin_src dash :epilogue ":" :prologue "exec 2>&1"
g++ main.cpp && echo "Success"
#+end_src
#+begin_src cpp yes :main yes :tangle main.cpp :post compile-source-cpp()
int 12 = "a";
#+end_src
#+RESULTS:
#+begin_example
main.cpp: In function ‘int main()’:
main.cpp:2:5: error: expected unqualified-id before numeric constant
2 | int 12 = "a";
| ^~
#+end_example
There are two disadvantages of this solution:
The C++ source file would be executed twice: The first one because
of C-c C-c
is pressed and the second one because g++
is
executed in the dash
code block but at least the #+RESULTS
of
the corresponding block would show the compilation errors.
Before pressing C-c C-c
, you will need to ensure that
org-babel-tangle
is executed beforehand on the C++ code block so
that the source file is created and g++
can then compile it.
Second solution
This solution gets rid of the two disadvantages of the first solution.
#+PROPERTY: header-args:cpp :tangle main.cpp :eval no
* Utilities
#+begin_src elisp
(defun org-babel-tangle-previous-src ()
"Tangles the backward nearest source code block."
(interactive)
(save-excursion
(goto-char
(search-backward-regexp "^[[:space:]]*#\\+begin_src\\>"))
(let ((current-prefix-arg '(4)))
(call-interactively 'org-babel-tangle))))
#+end_src
#+NAME: org-babel-tangle-previous-src
#+begin_src elisp
(org-babel-tangle-previous-src)
#+end_src
#+NAME: compile-source-cpp
#+begin_src dash :prologue "exec 2>&1" :epilogue ":"
g++ main.cpp && echo "Success"
#+end_src
* Compilation errors in C++
#+begin_src cpp
int main
#+end_src
#+CALL: org-babel-tangle-previous-src() :post compile-source-cpp()
#+RESULTS:
#+begin_example
main.cpp: In function ‘int main()’:
main.cpp:3:1: error: expected initializer before ‘return’
3 | return 0;
| ^~~~~~
#+end_example
#+begin_src cpp
int number = "12 + 13";
#+end_src
#+CALL: org-babel-tangle-previous-src() :post compile-source-cpp()
#+RESULTS:
#+begin_example
main.cpp: In function ‘int main()’:
main.cpp:2:14: error: invalid conversion from ‘const char*’ to ‘int’ [-fpermissive]
2 | int number = "12 + 13";
| ^~~~~~~~~
| |
| const char*
#+end_example
Pressing C-c C-c
in the #+CALL
statements will tangle the nearest
source code block and then will execute g++
on the tangled file.