Configure Emacs Flymake to call g ++ directly
When writing simple, one file, C ++ code, I usually call g ++ directly. By default, Flymake seems to assume a Makefile with target validation syntax. How to set up Flymake to call g ++ directly, for example:
g++ -c a.cpp
If the answer can be modified to include compiler flags it would be even better
Many thanks
+3
source to share
1 answer
You can call g ++ directly with the following flymake config.
(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 the following screenshot when I call flymake-allowed-file-name-masks
Next version of the comment.
;; 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)
+6
source to share