2013-02-13 49 views
3

在編寫簡單的一個文件C++代碼時,我通常直接調用g ++。默認情況下,Flymake似乎假定存在一個帶有檢查語法目標的Makefile。如何配置Flymake只需撥打G ++直接,如:配置Emacs Flymake直接調用g ++

g++ -c a.cpp 

如果答案可能被修改,以包括編譯器標誌,這將是更好的

非常感謝

回答

6

您可以致電G ++直接使用以下flymake配置。

(require 'flymake) 

(defun flymake-cc-init() 
    (let* ((temp-file (flymake-init-create-temp-buffer-copy 
         'flymake-create-temp-inplace)) 
     (local-file (file-relative-name 
         temp-file 
         (file-name-directory buffer-file-name)))) 
    (list "g++" (list "-Wall" "-Wextra" "-fsyntax-only" local-file)))) 

(push '("\\.cpp$" flymake-cc-init) flymake-allowed-file-name-masks) 
(add-hook 'c++-mode-hook 'flymake-mode) 

我得到了下面的截圖當我打電話flymake-allowed-file-name-masks

flymake-c++

以下是評論的版本。

;; Import flymake 
(require 'flymake) 

;; Define function 
(defun flymake-cc-init() 
    (let* (;; Create temp file which is copy of current file 
     (temp-file (flymake-init-create-temp-buffer-copy 
         'flymake-create-temp-inplace)) 
     ;; Get relative path of temp file from current directory 
     (local-file (file-relative-name 
         temp-file 
         (file-name-directory buffer-file-name)))) 

    ;; Construct compile command which is defined list. 
    ;; First element is program name, "g++" in this case. 
    ;; Second element is list of options. 
    ;; So this means "g++ -Wall -Wextra -fsyntax-only tempfile-path" 
    (list "g++" (list "-Wall" "-Wextra" "-fsyntax-only" local-file)))) 

;; Enable above flymake setting for C++ files(suffix is '.cpp') 
(push '("\\.cpp$" flymake-cc-init) flymake-allowed-file-name-masks) 

;; Enable flymake-mode for C++ files. 
(add-hook 'c++-mode-hook 'flymake-mode) 
+0

非常感謝您的幫助syohex。正如我以前從未使用過lisp,是否可以簡要解釋代碼的每行代碼在做什麼?非常感謝 – Arrakis 2013-02-14 13:21:00

+0

請參閱評論版本代碼(對不起,我的英文很差)。如果您使用flymake模式進行其他編程語言,請參閱flycheck(https://github.com/lunaryorn/flycheck)。 flycheck支持多種語言,因此您可以使用flymake模式進行一些配置。你不需要編寫這樣的Lisp函數。 – syohex 2013-02-14 15:12:03

+0

@syohex是否有可能使flymake在延遲一段時間後顯示錯誤? – Tengis 2014-04-27 13:15:35