C++ 保存编译执行C++;Emacs中的程序?

C++ 保存编译执行C++;Emacs中的程序?,c++,windows,emacs,g++,user-input,C++,Windows,Emacs,G++,User Input,我一直在试图找到一种有效地保存程序、编译程序并在emacs中运行的方法。我在这方面只取得了部分成功 我使用smart-compile.el使工作更容易()。 在这里,我已经编辑C++相关部分到下面,这样程序编译和运行时,我键入 M x智能编译ReT ,后面跟着一个 ReT (defcustom smart-compile-alist '( ;; g++-3 is used instead of g++ as the latter does not ;; work in Windows.

我一直在试图找到一种有效地保存程序、编译程序并在emacs中运行的方法。我在这方面只取得了部分成功

我使用smart-compile.el使工作更容易()。 在这里,我已经编辑C++相关部分到下面,这样程序编译和运行时,我键入<代码> M x智能编译ReT <代码>,后面跟着一个<代码> ReT
(defcustom smart-compile-alist '(
  ;; g++-3 is used instead of g++ as the latter does not
  ;; work in Windows. Also '&& %n' is added to run the
  ;; compiled object if the compilation is successful
  ("\\.[Cc]+[Pp]*\\'" . "g++-3 -O2 -Wall -pedantic -Werror 
  -Wreturn-type %f -lm -o %n && %n")
  ..
举个例子,对于程序sqrt.cpp,智能编译自动生成以下编译命令:

g++-3 -O2 -Wall -pedantic -Werror -Wreturn-type sqrt.cpp -lm -o sqrt && sqrt
只要my.cpp没有任何
cin
语句,这就可以工作。对于带有
cin
语句的代码,console窗口显示用户应该输入数据的点。但我无法输入任何内容,编译状态仍停留在运行状态

为了让带有用户输入的代码正常工作,我必须删除
&&FILENAME
部分,然后在emacs'
eshell
中手动运行
/FILENAME

我正在Windows中运行emacs 24.3。我已经安装了Cygwin,并将其bin添加到Windows环境变量Path中(这就是g++-3编译工作的原因)

如果有人能指导我如何使用单个命令在emacs中保存编译运行用户输入required.cpp程序,我将不胜感激。或者至少我需要如何修改上面的g++-3命令,才能让compile+run为用户输入程序工作


谢谢

Emacs是可编程的,所以如果需要两个步骤,您可以编写一个命令来组合它们。simples代码如下所示:

(defun save-compile-execute ()
  (interactive)
  (smart-compile 1)                          ; step 1: compile
  (let ((exe (smart-compile-string "%n")))   ; step 2: run in *eshell*
    (with-current-buffer "*eshell*"
      (goto-char (point-max))
      (insert exe)
      (eshell-send-input))
    (switch-to-buffer-other-window "*eshell*")))
上面的代码很简单,但有一个缺陷:它不会等待编译完成。由于
智能编译
不支持同步编译的标志,因此必须通过临时连接到
编译完成函数
来实现同步编译,这使得代码更加复杂:

(require 'cl)  ; for lexical-let

(defun do-execute (exe)
  (with-current-buffer "*eshell*"
    (goto-char (point-max))
    (insert exe)
    (eshell-send-input))
  (switch-to-buffer-other-window "*eshell*"))

(defun save-compile-execute ()
  (interactive)
  (lexical-let ((exe (smart-compile-string "./%n"))
                finish-callback)
    ;; when compilation is done, execute the program
    ;; and remove the callback from
    ;; compilation-finish-functions
    (setq finish-callback
          (lambda (buf msg)
            (do-execute exe)
            (setq compilation-finish-functions
                  (delq finish-callback compilation-finish-functions))))
    (push finish-callback compilation-finish-functions))
  (smart-compile 1))

该命令可以通过M-x
save compile execute
RET运行,也可以通过将其绑定到键来运行。

感谢您的快速回复。我使用了你发布的建议功能,但它对我不起作用。我得到一个错误
错误的类型参数:number-or-marker-p,nil
。我不熟悉Emacs Lisp语法,因此我将自己尝试理解该问题,如果您对该错误很熟悉,请更新可能出现的错误。谢谢!我已经试过了。它几乎起作用了。现在唯一的问题是
(insert exe)
部分在编译结束之前立即运行。函数是否有办法等待编译结束?我还必须用
“/%n”
替换
%n”
,然后它工作得很好。到目前为止,它第一次出现错误,但第二次运行“save compile execute”命令时,它的工作原理与上次生成的.exe完全相同。更新:我不知道如何等待编译完成。但是对于我正在练习的小代码,3秒钟就足够了。所以在
(智能编译1)
之后添加
(坐3)
行就可以了:)最后一个请求。。这将是一个完美的解决方案。我尝试了各种方法,但无法使eshell成为活动缓冲区。一旦我能够做到这一点,我想使用
(缓冲区的末尾)
就可以了,对吗?在运行save compile execute之后,如果我可以直接开始输入数据,而不切换到eshell并滚动到输入数据的底部,那就太酷了。使用
(切换到buffer other window“*eshell*”)
更新它。