Skip to content

Instantly share code, notes, and snippets.

@ryukinix
Last active July 30, 2017 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryukinix/f155e39c6a9221ff28c3641d21adb2e7 to your computer and use it in GitHub Desktop.
Save ryukinix/f155e39c6a9221ff28c3641d21adb2e7 to your computer and use it in GitHub Desktop.
Setup C/C++ compile command and gdb
(defvar gud-gud-gdb-history nil)
(defun setup-c-and-cpp-compiler-with-gdb ()
(interactive)
"Generate strings for 'compile and 'gud-gdb commands on C/C++ mode"
(define-key (current-local-map) "\C-c\C-c" 'compile)
(define-key (current-local-map) [M-f9] 'gud-gdb)
(when buffer-file-name
(let* ((file (file-name-nondirectory buffer-file-name))
(file-basename (file-name-sans-extension file))
(extension (if (eq system-type 'windows-nt) "exe" "out")))
(unless (or (file-exists-p "Makefile") (file-exists-p "makefile"))
(set (make-local-variable 'compile-command)
;; emulate make's .c.o implicit pattern rule, but with
;; different defaults for the CC, CPPFLAGS, and CFLAGS
;; variables:
;; $(CC) -c -o $@ $(CPPFLAGS) $(CFLAGS) $<
(format "%s -o %s.%s %s %s %s"
(or (getenv "CC") (if c-buffer-is-cc-mode
"g++ -std=c++14"
"gcc"))
file-basename extension
(or (getenv "CPPFLAGS") "-DDEBUG=9")
(or (getenv "CFLAGS") "-pedantic -Wall -g")
file))
(push (format "gdb --fullname %s.%s" file-basename extension)
gud-gud-gdb-history)
(set (make-local-variable 'gud-gdb-command-name)
(format "gdb -i=mi %s.%s" file-basename extension))))))
;; add commands for build and debug to C++ and C
(add-hook 'c-mode-common-hook 'setup-c-and-cpp-compiler-with-gdb)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment