2015-12-02 14:23:39 +00:00
#+TITLE : C/C++ layer
2015-06-10 16:44:30 +00:00
[[file:img/ccpp.jpg ]]
[[file:img/cmake.png ]]
2016-03-31 02:59:55 +00:00
* Table of Contents :TOC_4_gh:noexport:
2017-05-22 14:16:12 +00:00
- [[#description ][Description ]]
2017-08-06 02:14:10 +00:00
- [[#features ][Features: ]]
2017-05-22 14:16:12 +00:00
- [[#install ][Install ]]
- [[#layer ][Layer ]]
- [[#default-mode-for-header-files ][Default mode for header files ]]
- [[#enable-clang-support ][Enable Clang support ]]
- [[#clang-format ][clang-format ]]
- [[#company-clang-and-flycheck ][Company-clang and flycheck ]]
2017-08-23 09:09:38 +00:00
- [[#cmake-configuration ][CMake configuration ]]
2017-05-22 14:16:12 +00:00
- [[#key-bindings ][Key Bindings ]]
- [[#debugger-realgud ][Debugger (realgud) ]]
2015-06-10 16:44:30 +00:00
* Description
2017-04-26 13:50:19 +00:00
This layer adds configuration for C/C++ language as well support for [[https:/ /cmake.org/ ][CMake]]
2015-06-10 16:44:30 +00:00
scripts.
2017-08-06 02:14:10 +00:00
** Features:
2015-06-10 16:44:30 +00:00
- Support syntax checking via flycheck with Clang.
2015-07-27 22:04:39 +00:00
- Support for disassembly of code with [[https://github.com/jart/disaster ][disaster ]].
2015-06-10 16:44:30 +00:00
- Support code reformatting with [[http://clang.llvm.org/docs/ClangFormat.html ][clang-format ]].
2015-12-22 10:04:26 +00:00
- Display function or variable definition at the bottom. (when =semantic= layer
is included)
2015-06-10 21:16:01 +00:00
- Display current function cursor is in at the top. See [[https://github.com/tuhdo/semantic-stickyfunc-enhance ][stickyfunc-demos ]] for
demos in some programming languages. (when =semantic= layer is included)
- Support common refactoring with [[https://github.com/tuhdo/semantic-refactor ][semantic-refactor ]] . See [[https://github.com/tuhdo/semantic-refactor/blob/master/srefactor-demos/demos.org ][srefactor-demos ]] for
demonstration of refactoring features. (when =semantic= layer is included)
2015-12-22 10:04:26 +00:00
- 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).
2017-05-14 17:20:10 +00:00
- Support for [[https://github.com/realgud/realgud ][realgud ]] debugger.
2017-08-14 07:54:15 +00:00
- 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 [[https://github.com/atilaneves/cmake-ide ][cmake-ide ]] .
2015-06-10 16:44:30 +00:00
* Install
** Layer
2016-01-06 05:21:55 +00:00
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.
2015-06-10 16:44:30 +00:00
2015-06-10 21:16:01 +00:00
*Note:* [[https://github.com/tuhdo/semantic-refactor ][semantic-refactor ]] is only available for Emacs 24.4+
2015-06-10 16:44:30 +00:00
** Default mode for header files
2015-06-10 21:16:01 +00:00
By default header files are opened in =c-mode= , you can open them in =c++-mode=
2015-06-10 16:44:30 +00:00
by setting the variable =c-c++-default-mode-for-headers= to =c++-mode= .
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
2015-06-26 05:15:00 +00:00
'((c-c++ :variables
2015-06-10 16:44:30 +00:00
c-c++-default-mode-for-headers 'c+ +-mode)))
#+END_SRC
2015-06-10 21:16:01 +00:00
*Note:* To set the variable for a given project, create a directory local
2015-06-10 16:44:30 +00:00
variable at the root of your project. More info on directory local variables
can be found in the [[http://www.gnu.org/software/emacs/manual/html_node/elisp/Directory-Local-Variables.html ][dir-locals ]].
** Enable Clang support
To enable Clang support set the layer variable =c-c++-enable-clang-support=
to =t= in the dotfile:
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers
'((c-c++ :variables c-c+ +-enable-clang-support t)))
#+END_SRC
*** clang-format
[[http://clang.llvm.org/docs/ClangFormat.html ][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
2015-09-28 06:05:18 +00:00
particular mode or all modes in your =dotspacemacs/user-config= (within your
2015-06-10 16:44:30 +00:00
=~/.spacemacs= ):
#+BEGIN_SRC emacs-lisp
;; 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:
2016-10-24 17:56:59 +00:00
(add-hook 'c++-mode-hook 'clang-format-bindings
(defun clang-format-bindings ()
(define-key c++-mode-map [tab] 'clang-format-buffer)))
2015-06-10 16:44:30 +00:00
#+END_SRC
2017-04-07 09:21:05 +00:00
To enable automatic buffer formatting on save, set the variable
=c-c++-enable-clang-format-on-save= to =t= :
#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(
(c-c++ :variables c-c+ +-enable-clang-format-on-save t)))
#+END_SRC
2015-06-10 16:44:30 +00:00
*** Company-clang and flycheck
2017-08-28 02:18:53 +00:00
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.
2015-06-10 16:44:30 +00:00
2017-08-28 02:18:53 +00:00
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.
2015-06-10 16:44:30 +00:00
2017-08-23 09:09:38 +00:00
** CMake configuration
=cmake-ide= plugin works out of the box, but there are few useful configuration
2017-08-28 02:18:53 +00:00
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.
2017-08-23 09:09:38 +00:00
#+BEGIN_SRC emacs-lisp
((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"))))
#+END_SRC
2015-06-10 16:44:30 +00:00
* Key Bindings
2017-08-23 09:09:38 +00:00
| Key Binding | Description |
|-------------+-------------------------------------------------------------------------|
| ~SPC m g a~ | open matching file (e.g. switch between .cpp and .h) |
| ~SPC m g A~ | open matching file in another window (e.g. switch between .cpp and .h) |
| ~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-completionand 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 |
2015-06-10 16:44:30 +00:00
2015-06-10 21:16:01 +00:00
*Note:* [[https://github.com/tuhdo/semantic-refactor ][semantic-refactor ]] is only available for Emacs 24.4+
2017-05-14 17:20:10 +00:00
** Debugger (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 |