Emacs fastest C++ compilation process?

前端 未结 2 1837
你的背包
你的背包 2020-12-19 15:27

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

相关标签:
2条回答
  • 2020-12-19 16:02

    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)))))
    

    With (setq compilation-ask-about-save nil) there's no more warnings about saving (cpp-run saves automatically).

    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.

    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)...

    UPD:

    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)))))
    

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

    0 讨论(0)
  • 2020-12-19 16:14

    To sum up the answers in the comments.

    1. Change the value of compilation-ask-about-save. If you don't want to change this globally (with setq) you can change it just inside your function by wrapping it in a (let ((compilation-ask-about-save nil)) ...function contents...) statement.
    2. Bind next-error to something quick: (define-key c++-mode-map "\M-j" 'next-error). I personally use global-set-key for this, just because it's useful in a whole bunch of modes.
    3. Replace the compile command in your function with (compile "make && ./a.out") (using the name of your executable of course).

    Hope this helps.

    0 讨论(0)
提交回复
热议问题