Skip to content

Instantly share code, notes, and snippets.

@ageekymonk
Created January 23, 2012 01:35
Show Gist options
  • Save ageekymonk/1659869 to your computer and use it in GitHub Desktop.
Save ageekymonk/1659869 to your computer and use it in GitHub Desktop.
Emacs snippet for compiling code in c and cpp
;; Compilation for gcc / g++
(defun ramz/code-compile ()
(interactive)
(unless (file-exists-p "Makefile")
(set (make-local-variable 'compile-command)
(let ((file (file-name-nondirectory buffer-file-name)))
(format "%s -o %s %s"
(if (equal (file-name-extension file) "cpp") "g++" "gcc" )
(file-name-sans-extension file)
file)))
(compile compile-command)))
(global-set-key [f9] 'ramz/code-compile)
;; Run the compiled file
(defun ramz/run-code ()
(interactive)
(set 'run-command
(if (equal (file-name-extension (file-name-nondirectory buffer-file-name)) "rb")
(format "ruby %s" (file-name-nondirectory buffer-file-name))
(format "./%s" (file-name-sans-extension (file-name-nondirectory buffer-file-name)))))
(shell-command run-command))
(global-set-key [f8] 'ramz/run-code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment