【问题标题】:How can I make emacs compile my c++ code and run it in a new window?如何让 emacs 编译我的 c++ 代码并在新窗口中运行它?
【发布时间】:2021-05-29 17:45:34
【问题描述】:

有没有一种方法可以让 emacs 在外部控制台窗口中使用命令M-x compile 编译和运行我的代码(我所说的外部控制台窗口的意思是我希望它在新的控制台窗口中运行我的代码,例如当我在 Visual Studio 中运行我的 C++ 代码时它确实如此)。

我希望 emacs 自动打开一个新的控制台窗口并在 M-x compile 之后运行可执行文件,如下所示:

【问题讨论】:

  • Emacs 不像 Visual Studio 那样是一个 IDE,它的核心是一个编辑器。您可能能够加载包,如 CEDET,这将允许 Emacs 调用编译器并可能像调试器一样工作(我还没有测试过调试功能)。应该有允许您运行可执行文件的现有命令。
  • 在一天结束的时候M-x compile 只是告诉emacs 运行之前指定的命令(例如在你的.emacs 配置文件中)。命令可以是任何你喜欢的。
  • 例如。编译并运行C-u M-x compile'gcc -o a.out my-c-file.c && ./a.out'。对compile 的连续调用将默认为先前输入的命令

标签: c++ emacs compilation


【解决方案1】:

通常使用 Emacs,当您想要一些模糊的(即一些简单的,或对您的用例特殊的)自动化时,您只需编写一个命令,它会执行您想要的操作。

这是一个例子。下面的 Elisp 代码调用函数 and-run,当编译完成时(通过使用钩子 compilation-finish-functions)。

(defun and-run (&rest _)
  "run after comilation, an not elaborated example function"
  (interactive)
  (let ((exe "./test.out")
        (prevent-closing (concat "echo \"Press Enter to continue\"" ";"
                                 "read"))
        (terminal "xterm -e"))
  (when (file-exists-p exe)
    (let ((my-command (concat exe ";" prevent-closing)))
      (shell-command (format "%s %s" terminal (shell-quote-argument my-command)))))))

(add-hook 'compilation-finish-functions #'and-run)

由于我不是 Windows 用户,你必须对这个功能做一些小的修改,才能在 Windows 上运行它。

注意:还有一个compilation-start-hook,如果你例如想要比较你的 exe 的文件修改时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-18
    • 1970-01-01
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多