reddit / emacs

How can I make emacs compile my c++ code and run it in a new window?

HateMintChoco · 2022-02-23 11:08
[https://stackoverflow.com/questions/67754260/how-can-i-make-emacs-compile-my-c-code-and-run-it-in-a-new-window](https://stackoverflow.com/questions/67754260/how-can-i-make-emacs-compile-my-c-code-and-run-it-in-a-new-window) Does anyone know the answer to this question?
pja · 2022-02-23 11:47
Since emacs doesn’t automatically know where your compile command finally drops the binary, you’re going to need a custom bit of lisp to compile & run it. You could just record a macro of the relevant pair of M-x (re)compile, M-! <command> keystrokes.
big_lentil · 2022-02-23 14:32
When I'm doing something like this I split the window* into three, containing shell, compilation, and source buffers, then just change the active buffer depending on what I want to do. If a fresh separate console window is not an absolute necessity I'd suggest doing this. With a keyboard macro you can compile, clear the shell buffer, and run your executable with a single key press so it should be functionally equivalent. Most of the time I don't even bother with a macro though. Alternatively you could attach something to the end of your compile command, e.g. if your compilation command is bash compile.sh you could do something like bash compile.sh; ./executable which would run your command and show its output in the compilation buffer, or bash compile.sh; xterm -hold -e "./executable", which opens a new xterm window and runs your command in it. \* window referring to DE windows and not emacs windows
patrick_thomson · 2022-02-23 14:54
Set your `compile-command` to whatever compiler invocation you use plus `&& ./yourprogram` (or whatever actual program invocation you use). You can set this easily by binding `compile` to a key combo and then using `C-u` before invoking it.
SlowValue · 2022-02-23 15:03
> Since emacs doesn’t automatically know where your compile command finally drops the binary, Aren't there `hooks` for that?
pja · 2022-02-23 15:11
Not that I’m aware of, but I’m not any kind of emacs expert.
xtifr · 2022-02-23 20:35
A hook would have to *know* where your compile command drops the binary (assuming there's just one...). Since a compile command usually invokes a separate build system (make, cmake, etc., etc.), there's no obvious way to tell.
SlowValue · 2022-02-23 21:40
There is a hook, it's called `compilation-finish-functions`. It is an odd name for an hook var, but it calls all its functions via (run-hook-with-args 'compilation-finish-functions cur-buffer msg) at the end of function `compilation-handle-exit`. So I guess it counts as the hook we searched for.
SlowValue · 2022-02-23 21:48
> have to know where your compile command drops the binary ... there's no obvious way to tell That may not be an easy task, but there are ways to determine that (parsing the makefile, conventions, a project file, asking the user, or searching for executable files with recent modification time). But in my understanding, that is not the task of the hook. The hook just pulls (aka calls) a user provided function. The called function then, has to discover the name of the executable.
Extension_Try_6870 · 2022-02-23 23:59
Projectile has some useful keybinding for this. (install, compile, run, package,...). I didn't try 'run' yet but for the other commands, a new window get opened.
HateMintChoco · 2022-02-24 03:00
`fileName=$1` `osascript -e 'tell app "Terminal"` `do script "g++ -o /Users/username/temp/ctemp '$PWD/${fileName}' && /Users/username/temp/ctemp"` `activate` `end tell'` run5.sh `(map! :leader` `:desc "Run in new Term"` `"c c" #'execute-c-program)` \---------------------------------------------------------------- `(defun my-sh-send-command (command)` `"Send COMMAND to current shell process.` `Creates new shell process if none exists.` `See URL \`https://stackoverflow.com/a/7053298/5065796'"` `(let ((proc (get-process "shell"))` `pbuf)` `(unless proc` `(let ((currbuff (current-buffer)))` `(shell)` `(switch-to-buffer currbuff)` `(setq proc (get-process "shell"))` `))` `(setq pbuff (process-buffer proc))` `(setq command-and-go (concat command "\n"))` `(with-current-buffer pbuff` `(goto-char (process-mark proc))` `(insert command-and-go)` `(move-marker (process-mark proc) (point))` `)` `(process-send-string proc command-and-go)))` `(defun execute-c-program ()` `(interactive)` `(my-sh-send-command (concat "run5.sh " (buffer-name)))` `)` I found this way. I use macos, so i look up the command that open terminal and run command. Finally i make shell script. it is awesome! It's what I wanted It is being mapped and used with the shortcut key of Emacs.