spacemacs/layers/+lang/c-c++
Fredrik Bergstrand 5ec172d7bd c-c++: Improve the header include auto completion
This change makes the c-c++ layer configure company-c-headers to search for and
list completion alternatives according to the priority list found in 'man gcc'.
If supported, the default system include paths are also fetched from gcc's
configuration instead of being hard coded to "/usr/include" and
"/usr/local/include". This also remedies the problem where the C++ standard
library headers does not show up as completion alternatives without manual
addition of include paths (see #4493, #8655).
2017-12-19 00:31:43 -05:00
..
img Use + instead of ! for layer categories 2015-09-11 00:13:51 -04:00
config.el enable c++11 mode in company-clang-arguments if c-c++-enable-c++11 is not nil 2017-12-19 00:23:43 -05:00
funcs.el c-c++: Improve the header include auto completion 2017-12-19 00:31:43 -05:00
packages.el c-c++: Add clang-format functions and key bindings 2017-12-19 00:25:20 -05:00
README.org c-c++: Add clang-format functions and key bindings 2017-12-19 00:25:20 -05:00

C/C++ layer

/TakeV/spacemacs/media/commit/5ec172d7bdbb71d84d0189a9751665f3cb9977af/layers/+lang/c-c++/img/ccpp.jpg /TakeV/spacemacs/media/commit/5ec172d7bdbb71d84d0189a9751665f3cb9977af/layers/+lang/c-c++/img/cmake.png

Description

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

Features:

  • 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).
  • Support for realgud debugger.
  • Support for CMake configure/build (with limited support for other build systems), automatic generation of compile_commands.json (compile flags), on-the-fly configuration of flycheck, company-clang and RTags (if installed) with cmake-ide .

Install

Layer

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.

Clang Configuration

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

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

To enable automatic buffer formatting on save, set the variable c-c++-enable-clang-format-on-save to t:

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

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.

CMake configuration

To enable CMake projects support set the layer variable c-c++-enable-cmake-ide-support to t in the dotfile:

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

cmake-ide plugin has several useful configuration options.

To configure project you need to create .dir-locals.el file. In case of using make as CMake backend you can use helm-make to select required build target.

Here is a sample configuration. This configuration forces cmake-ide to use the local directory and pass that directory to helm-make. Such config allows to build your project with SPC c c key binding.

((nil .
      ((cmake-ide-project-dir . "~/Project")
       (cmake-ide-build-dir . "~/Project/build")
       (cmake-ide-cmake-opts . "-DCMAKE_BUILD_TYPE=Debug")
       (helm-make-build-dir . "build")
       (helm-make-arguments . "-j7"))))

Enable google-set-c-style

If you have clang enabled with clang-format as described earlier in this page you may not have a lot of neeed for google-set-c-style if you are already using a mode based on Google mode for most of your projects.

However, if you don't have (or want) clang-format, or if you have to do a lot Tramp remote editing on systems that don't have clang-format installed, you may want google-c-style enabled and added to your common hooks.

To get google-c-style actually install itself into your C/C++ common hooks, you need to have c-c++-enable-google-style defined to true when you load the C-C++ lang in Spacemacs. In your ~/.spacemacs file, a possible way that this would look is that in your list of dostpacemacs-configuration-layers you have an entry like

     (c-c++ :variables
            c-c++-enable-google-style t)

Additionally, if you have c-c++-enable-google-newline variable set then `google-make-newline-indent will be set as a c-mode-common-hook. You would set that up like this:

     (c-c++ :variables
            c-c++-enable-google-style t
            c-c++-enable-google-newline t)

Key Bindings

Key Binding Description
SPC m g a open matching file
(e.g. switch between .cpp and .h, requires a project to work)
SPC m g A open matching file in another window
(e.g. switch between .cpp and .h, requires a project to work)
SPC m D disaster: disassemble c/c++ code
SPC m r srefactor: refactor thing at point.
SPC m p c Run CMake and set compiler flags for auto-completion and flycheck
SPC m p C Run CMake if compilation database JSON file is not found
SPC m p d Remove file connected to current buffer and kill buffer, then run CMake
SPC m c c Compile project

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

Debugging (realgud)

Key Binding Description
SPC m d d open cmd buffer
SPC m d e eval variable
s step over
i step into
b set break
B unset break
o step out
c continue
e eval variable
r restart
q quit debug
S goto cmd buffer

Formatting (clang-format)

Key Binding Description
SPC m = = format current region or buffer
SPC m = f format current function