Emacs最快的C ++编译过程? [英] Emacs fastest C++ compilation process?

查看:68
本文介绍了Emacs最快的C ++编译过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的用例:我阅读了一篇关于C ++
的技术博客的文章(多重继承失败和多线程等:)。
通常它们带有一些代码。它几乎总是一个文件,我几乎总是
想运行它,并玩弄它。

Here's the use case: I read an articles on tech blogs about C++ (fails of multiple inheritance this and multi-threading that etc.:). Usually they come with some code. It's almost always one file and I almost always want to run it and play around with it.

我想用Emacs做,我想做

I want to do it with Emacs and I want to do it FAST, as in with the least (or reasonably few) keystrokes.

所以假设我已经创建了一个 multiple_inheritance,它是FAST,
。 cc
文件放在自己的文件夹
中并粘贴代码。

So suppose I've already created a multiple_inheritance.cc file in its own folder and pasted the code. How do I get to an executable really fast?

这是我现在正在做的事(希望有人会改进它。)

Here's what I'm doing now (hopefully someone will improve it.)

(defun cpp-generate-makefile ()
  (interactive)
  (let* ((n-buffer (buffer-file-name))
         (n-file (file-name-nondirectory n-buffer))
         (n-target (file-name-sans-extension n-file))
         (n-makefile (concat (file-name-directory n-buffer) "Makefile")))
    (if (file-exists-p n-makefile)
        (when (called-interactively-p 'any)
          (message "Makefile already exists"))
      (with-current-buffer (find-file-noselect n-makefile)
        (insert 
         (concat n-target ": " n-file 
                 "\n\tg++ -g -O2 -std=c++0x -o $@ $^\n\n"
                 "clean: \n\trm -f " n-target "\n"))
        (save-buffer)))))

(defun cpp-run ()
  (interactive)
  (save-buffer)
  (cpp-generate-makefile)
  (compile "make"))

(add-hook 'c++-mode-hook
      (lambda()
            ;; ...
            (define-key c++-mode-map [f5] 'cpp-run)))

这里有一些东西我现在慢了:

Here's a few things that SLOW me down currently:


  1. compile

  2. 我想要在 * compilation * 中指定一个开头的文件$ c>缓冲区,以防万一出现错误。
    我查看了 compile.el ,它定义了 compilation-num-errors-found

  3. 另一方面,如果没有错误(我只需要一个谓词),
    b为什么不启动 term 并运行程序?

  1. compile asks if I want to save any open files that are totally unrelated to C++.
  2. I'd like to have the point in *compilation* buffer in case there was an error. I've looked into compile.el, which has compilation-num-errors-found defined, but that variable isn't used anywhere in that file.
  3. On the other hand, if there were no errors (I just need a predicate for this), why not launch term and run the program?


推荐答案

我更新了代码,我必须添加运行目标到 Makefile
我还添加了一个扩展C:

I've updated the code I had to add a run target to Makefile. I've also added an extension to C:

(defvar cpp-generate-compiler "g++ -g -O2 -std=c++0x")

(defun cpp-generate-makefile ()
  (interactive)
  (let* ((n-buffer (buffer-file-name))
         (n-file (file-name-nondirectory n-buffer))
         (n-target (file-name-sans-extension n-file))
         (n-makefile (concat
                       (file-name-directory n-buffer)
                       "Makefile")))
    (if (file-exists-p n-makefile)
        (when (called-interactively-p 'any)
          (message "Makefile already exists"))
      (with-current-buffer (find-file-noselect n-makefile)
        (insert
         (concat n-target ": " n-file
                 (format "\n\t%s -o $@ $^"
                   cpp-generate-compiler)
                 "\n\nclean: \n\trm -f " n-target
                 "\n\nrun: " n-target "\n\t ./" n-target
                 "\n\n.PHONY: clean run\n"))
        (save-buffer)))))

(defun cpp-run ()
  (interactive)
  (save-buffer)
  (cpp-generate-makefile)
  (compile "make run"))

(defun c-run ()
  (interactive)
  (let ((cpp-generate-compiler "gcc -g -O2 -std=c99"))
    (cpp-run)))

(add-hook 'c++-mode-hook
      (lambda()
            ;; ...
            (define-key c++-mode-map [f5] 'cpp-run)))
(add-hook 'c-mode-hook
      (lambda()
            ;; ...
            (define-key c-mode-map [f5] 'c-run)))

(setq compilation-ask-about-save nil)
(setq compilation-finish-functions
      (list (lambda(buffer str)
              (unless (string= str "finished\n")
                (push-mark)
                (next-error)))))

cpp-run 自动保存)。

我只记得 Mg n Mg p
导航错误。

And I just have to remember that M-g n and M-g p navigate the errors.

现在我的过程是最优的:从源代码中的一个键,以便有没有错误的

Now my process is neary optimal: one key from source to result in case there are no errors.

case有错误,它是额外的 Mg n
现在,如果只有一种方法 compile 调用(push-mark)(下一个错误) ...

In case there are errors, it's an extra M-g n. Now, if only there was a way for compile to call (push-mark)(next-error)...

感谢@juanleon的建议, / p>

Thanks to suggestion of @juanleon, this is solved with:

(setq compilation-finish-functions
      (list (lambda(buffer str)
              (unless (string= str "finished\n")
                (push-mark)
                (next-error)))))

但是由于某种原因, push-mark 在这种情况下无法正常工作。

But for some reason, push-mark doesn't work properly in this case.

这篇关于Emacs最快的C ++编译过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆