2013-02-13 100 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