4

When writing simple, one file, C++ code, I usually call g++ directly. By default, Flymake seems to assume the presence of a Makefile with a check-syntax target. How do I configure Flymake to simply call g++ directly, e.g:

g++ -c a.cpp

If the answer could be modified to include compiler flags, that would be even better

Many thanks

Arrakis
  • 576
  • 1
  • 6
  • 12

1 Answers1

7

You can call g++ directly with following flymake configuration.

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

I got following screenshot when I call flymake-allowed-file-name-masks

flymake-c++

Following is comment version.

;; 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)
syohex
  • 2,293
  • 17
  • 16
  • Thank you very much for the help syohex. As I have never used lisp before, would it be possible for you to briefly explain what each line of the code is doing? Many thanks again – Arrakis Feb 14 '13 at 13:21
  • Please see comment version code(sorry my english is poor). Please see flycheck(https://github.com/lunaryorn/flycheck) if you use flymake-mode for other programming langages. flycheck supports many languages, so you can use flymake-mode with a little configuration. You need not to write such Lisp function. – syohex Feb 14 '13 at 15:12
  • @syohex is it possible to make flymake display the error after a certain delay? – Tengis Apr 27 '14 at 13:15
  • No it is not. flymake check syntax per one second and popup error message if there are errors. – syohex Apr 28 '14 at 01:23