Find file History
syl20bnr Bump year in copyright headers
Happy New Year 2017!
Latest commit b252d25 Jan 5, 2017
Type Name Latest commit message Commit time
Failed to load latest commit information.
img Use + instead of ! for layer categories Sep 11, 2015 Convert org doc files with doc-fmt Mar 31, 2016
config.el Bump year in copyright headers Apr 2, 2017
funcs.el Bump year in copyright headers Apr 2, 2017
packages.el Bump year in copyright headers Apr 2, 2017

C/C++ layer

img/ccpp.jpg img/cmake.png

Table of Contents


This layer adds configuration for C/C++ language as well support for CMake scripts.


  • Support syntax checking via flycheck with Clang.
  • Support for disassembly of code with disaster.
  • Support code reformatting with clang-format.
  • Display function or variable definition at the bottom. (when semantic layer is included)
  • Display current function cursor is in at the top. See stickyfunc-demos for demos in some programming languages. (when semantic layer is included)
  • Support common refactoring with semantic-refactor . See srefactor-demos for demonstration of refactoring features. (when semantic layer is included)
  • Support code navigation via cscope (when cscope layer is included) and gtags.
  • Support auto-completion (when auto-completion layer is included) via company-clang (when c-c++-enable-clang-support is turned on), or company-ycmd (when ycmd layer is included).



To use this configuration layer, add it to your ~/.spacemacs. You will need to add c-c++ to the existing dotspacemacs-configuration-layers list in this file.

Note: semantic-refactor is only available for Emacs 24.4+

Default mode for header files

By default header files are opened in c-mode, you can open them in c++-mode by setting the variable c-c++-default-mode-for-headers to c++-mode.

(setq-default dotspacemacs-configuration-layers
  '((c-c++ :variables
           c-c++-default-mode-for-headers 'c++-mode)))

Note: To set the variable for a given project, create a directory local variable at the root of your project. More info on directory local variables can be found in the dir-locals.

Enable Clang support

To enable Clang support set the layer variable c-c++-enable-clang-support to t in the dotfile:

(setq-default dotspacemacs-configuration-layers
  '((c-c++ :variables c-c++-enable-clang-support t)))


clang-format allows reformatting either a selected region of code (clang-format-region) or a whole buffer (clang-format-buffer) to make it conform to a style defined in a .clang-format file. This file is either located in the same directory as the file being edited, or in any of its parent directories (otherwise a default style will be used).

You can add snippets similar to the following to bind clang-format to either a particular mode or all modes in your dotspacemacs/user-config (within your ~/.spacemacs):

;; Bind clang-format-region to C-M-tab in all modes:
(global-set-key [C-M-tab] 'clang-format-region)
;; Bind clang-format-buffer to tab on the c++-mode only:
(add-hook 'c++-mode-hook 'clang-format-bindings)
  (defun clang-format-bindings ()
    (define-key c++-mode-map [tab] 'clang-format-buffer))

Company-clang and flycheck

This layer adds some fancy improvements to company-clang. It includes a hook to load a projects .clang_complete file, which is just a text file with one clang flag per line, a format also used by other text editor clang plugins.

Not only does this allow proper autocomplete on projects with extra includes and flags, but there is also support for flycheck so that it doesn’t complain about missing header files.

Key Bindings

Key BindingDescription
SPC m g aopen matching file (e.g. switch between .cpp and .h)
SPC m g Aopen matching file in another window (e.g. switch between .cpp and .h)
SPC m Ddisaster: disassemble c/c++ code
SPC m rsrefactor: refactor thing at point.

Note: semantic-refactor is only available for Emacs 24.4+