Skip to content

Instantly share code, notes, and snippets.

@erizhang
Last active November 12, 2019 14:58
Show Gist options
  • Select an option

  • Save erizhang/7787d376fc189429b088 to your computer and use it in GitHub Desktop.

Select an option

Save erizhang/7787d376fc189429b088 to your computer and use it in GitHub Desktop.
emacs configuration for c/c++
; start package.el with emacs
(require 'package)
; add MELPA to repository list
(add-to-list 'package-archives '("melpa" . "http://melpa.milkbox.net/packages/"))
; initialize package.el
(package-initialize)
;start auto-complete with emacs
(require 'auto-complete)
; do default config for auto-complete
(require 'auto-complete-config)
(ac-config-default)
; start yasnippet with emacs
(require 'yasnippet)
(yas-global-mode 1)
; let's define a function which initializes auto-compelte-c-headers and gets called for c/c++ hooks
(defun my:ac-c-header-init ()
(require 'auto-complete-c-headers)
(add-to-list 'ac-sources 'ac-source-c-headers)
; execute command `gcc-xc++ -E -v -` to find the header driectories
(add-to-list 'achead:include-directories '"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/.
./lib/clang/6.1.0/include")
)
; now let's call this function from c/c++ hooks
(add-hook 'c++-mode-hook 'my:ac-c-header-init)
(add-hook 'c-mode-hook 'my:ac-c-header-init)
; Fix iedit bug in Mac
(define-key global-map (kbd "C-c ;") 'iedit-mode)
; start flymake-google-cpplint-load
; let's define a function for flymake initialization
(defun my:flymake-google-init()
(require 'flymake-google-cpplint)
(custom-set-variables
'(flymake-google-cpplint-command "/usr/local/bin/cpplint"))
(flymake-google-cpplint-load)
)
(add-hook 'c-mode-hook 'my:flymake-google-init)
(add-hook 'c++-mode-hook 'my:flymake-google-init)
; and then install the flymake-cursor for the cpplint result prompts in buffer window.
; start google-c-style with emacs
(require 'google-c-style)
(add-hook 'c-mode-common-hook 'google-set-c-style)
(add-hook 'c-mode-common-hook 'google-make-newline-indent)
; turn on Semantic
(semantic-mode 1)
; let's define a funciton which adds semantic as a suggestion backend to auto complete
; and hook this funciton to c-mode-common-hook
(defun my:add-semantic-to-autocomplate()
(add-to-list 'ac-sources 'ac-source-semantic)
)
(add-hook 'c-mode-common-hook 'my:add-semantic-to-autocomplate)
; turn on ede mode
(global-ede-mode)
; create a project for our program.
(ede-cpp-root-project "my project" :file "~/workspace/demos/my_program/src/main.cpp"
:include-path '("/../my_inc"))
; you can use system-include-path for seeting up the system header file locations.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment