2019-01-05 19:46:14 -05:00
#+TITLE : lang/cc
#+DATE : January 16, 2017
#+SINCE : v2.0
#+STARTUP : inlineimages
* Table of Contents :TOC_3:noexport:
2019-04-23 14:03:16 -04:00
- [[#description ][Description ]]
- [[#module-flags ][Module Flags ]]
- [[#plugins ][Plugins ]]
- [[#prerequisites ][Prerequisites ]]
2020-08-21 23:17:48 -04:00
- [[#lsp-servers ][LSP servers ]]
2019-04-23 14:03:16 -04:00
- [[#irony-server ][irony-server ]]
2020-08-21 23:17:48 -04:00
- [[#macos ][macOS ]]
2019-04-23 14:03:16 -04:00
- [[#arch-linux ][Arch Linux ]]
2019-08-27 10:50:37 +02:00
- [[#opensuse ][openSUSE ]]
2019-04-23 14:03:16 -04:00
- [[#rtags ][rtags ]]
- [[#configure ][Configure ]]
- [[#project-compile-settings ][Project compile settings ]]
- [[#known-issues-with-bear-on-macos ][Known issues with bear on macOS ]]
2020-05-28 11:55:32 +02:00
- [[#appendix ][Appendix ]]
- [[#eglot-specific-bindings ][Eglot specific bindings ]]
2019-01-05 19:46:14 -05:00
* Description
2017-05-25 20:08:50 +02:00
This module adds support for the C-family of languages: C, C++, and Objective-C.
2017-05-04 10:40:51 +02:00
2017-05-25 20:08:50 +02:00
+ Code completion (~company-irony~ )
+ eldoc support (~irony-eldoc~ )
+ Syntax-checking (~flycheck-irony~ )
2017-12-27 00:32:11 -05:00
+ Code navigation (~rtags~ )
2019-04-21 19:59:44 -04:00
+ File Templates ([[../../editor/file-templates/templates/c-mode ][c-mode ]], [[../../editor/file-templates/templates/c++-mode ][c++-mode ]])
2019-07-14 14:43:45 +02:00
+ Snippets ([[https://github.com/hlissner/doom-snippets/tree/master/cc-mode ][cc-mode ]], [[https://github.com/hlissner/doom-snippets/tree/master/c-mode ][c-mode ]], [[https://github.com/hlissner/doom-snippets/tree/master/c++-mode ][c++-mode ]])
2017-08-21 20:07:07 +02:00
+ Several improvements to C++11 indentation and syntax highlighting.
2017-05-04 10:40:51 +02:00
2019-01-05 19:46:14 -05:00
** Module Flags
2019-04-23 14:03:16 -04:00
+ ~+lsp~ Disables irony+rtags and replaces them with LSP (ccls by default). This
requires the =:tools lsp= module.
2019-01-05 19:46:14 -05:00
** Plugins
+ [[https://github.com/Kitware/CMake ][cmake-mode ]]
+ [[https://github.com/chachi/cuda-mode ][cuda-mode ]]
+ [[https://github.com/liblit/demangle-mode ][demangle-mode ]]
+ [[https://github.com/jart/disaster ][disaster ]]
+ [[https://github.com/ludwigpacifici/modern-cpp-font-lock ][modern-cpp-font-lock ]]
+ [[https://github.com/salmanebah/opencl-mode ][opencl-mode ]]
+ [[https://github.com/jimhourihan/glsl-mode ][glsl-mode ]]*
2019-10-08 20:07:44 +13:00
+ [[https://github.com/guidoschmidt/company-glsl ][company-glsl ]]*
2019-04-23 14:03:16 -04:00
+ =+lsp=
2020-05-28 11:55:32 +02:00
+ [[https://github.com/MaskRay/emacs-ccls ][ccls ]] if =:tools lsp= has *no* =+eglot= flag
2019-04-23 14:03:16 -04:00
+ =-lsp=
+ [[https://github.com/Sarcasm/irony-mode ][irony ]]
+ [[https://github.com/ikirill/irony-eldoc ][irony-eldoc ]]
+ [[https://github.com/Sarcasm/flycheck-irony ][flycheck-irony ]]
+ [[https://github.com/Sarcasm/company-irony ][company-irony ]]
+ [[https://github.com/hotpxl/company-irony-c-headers ][company-irony-c-headers ]]
+ [[https://github.com/Andersbakken/rtags ][rtags ]]
+ [[https://github.com/Andersbakken/rtags ][ivy-rtags ]]
+ [[https://github.com/Andersbakken/rtags ][helm-rtags ]]
2019-01-05 19:46:14 -05:00
* Prerequisites
2020-08-21 23:17:48 -04:00
This module's requirements change depending on how you use it.
+ If =+lsp= is enabled, you need one of *clangd v9+* or *ccls* .
+ If =+lsp= is *not* enabled, you need *irony-server* and *rtags* .
+ Other features in this module depend on:
+ (optional) glslangValidator, for GLSL completion in ~glsl-mode~
+ (optional) cmake, for code completion in ~cmake-mode~
+ You will need a C/C++ compiler, like =gcc= or =clang= .
** LSP servers
=lsp-mode= and =eglot= support a few LSP servers, but =clangd= and =ccls= are
recommended.
+ clangd (must be v9 or newer) :: Clangd is included with =llvm= which should be
available through your OS' package manager.
- Debian/Ubuntu: ~sudo apt-get install clangd-9~
- macOS: ~brew install llvm~
- Windows: install from [[https://releases.llvm.org/download.html ][LLVM download page ]]
- clangd is available on some Linux distros from a =clang-tools= package, if
you'd like to avoid the full =llvm= .
+ ccls :: Available in many OS' package managers as =ccls= . Otherwise, there are
alternative install methods listed [[https://github.com/MaskRay/ccls/wiki/Install ][in the project's wiki ]].
2017-12-27 00:32:11 -05:00
** irony-server
Irony powers the code completion, eldoc and syntax checking systems.
2019-01-05 19:46:14 -05:00
After installing its dependencies, run ~M-x irony-install-server~ in Emacs.
2020-08-21 23:17:48 -04:00
*** macOS
Due to linking issues, macOS users must compile irony-server manually:
2017-05-25 20:08:50 +02:00
2019-01-05 19:46:14 -05:00
#+BEGIN_SRC sh
2017-05-04 10:46:12 +02:00
brew install cmake
2017-05-25 20:08:50 +02:00
brew install llvm # 1gb+ installation! May take a while!
git clone https://github.com/Sarcasm/irony-mode irony-mode
mkdir irony-mode/server/build
pushd irony-mode/server/build
2019-10-07 12:59:05 -04:00
DEST="$HOME/.emacs.d/ .local/etc/irony-server/ "
2020-01-16 21:23:35 -08:00
cmake -DCMAKE_PREFIX_PATH=/usr/local/opt/llvm \
-DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON \
2017-05-25 20:08:50 +02:00
-DCMAKE_INSTALL_PREFIX="$DEST" ../
cmake --build . --use-stderr --config Release --target install
install_name_tool -change @rpath/libclang.dylib \
/usr/local/opt/llvm/lib/libclang.dylib \
"$DEST/bin/irony-server"
# cleanup
popd
rm -rf irony-mode
2017-05-04 10:46:12 +02:00
#+END_SRC
2017-12-27 00:32:11 -05:00
*** Arch Linux
2019-01-05 19:46:14 -05:00
#+BEGIN_SRC sh
pacman -S clang cmake
2017-05-04 10:40:51 +02:00
#+END_SRC
2017-05-25 20:08:50 +02:00
2019-08-27 10:50:37 +02:00
*** openSUSE
#+BEGIN_SRC sh :dir /sudo::
sudo zypper install clang cmake
#+END_SRC
2017-12-27 00:32:11 -05:00
** rtags
2019-01-05 19:46:14 -05:00
Code navigation requires an [[https://github.com/Andersbakken/rtags ][rtags ]] server (~rdm~ ) installed. This should be
available through your OS's package manager.
2017-12-27 00:32:11 -05:00
This module will auto-start ~rdm~ when you open C/C++ buffers (so long as one
2019-01-05 19:46:14 -05:00
isn't already running). If you prefer to run it yourself:
2017-12-27 00:32:11 -05:00
#+BEGIN_SRC sh
rdm &
rc -J $PROJECT_ROOT # loads PROJECT_ROOT's compile_commands.json
#+END_SRC
* Configure
2019-01-05 19:46:14 -05:00
** Project compile settings
2017-12-27 00:32:11 -05:00
By default, a set of default compile settings are defined in
2017-12-27 17:10:21 -05:00
~+cc-default-compiler-options~ for C, C++ and Objective C. Irony, rtags and
2020-08-21 23:17:48 -04:00
flycheck will fall back to these. *This variable does nothing for LSP users.*
2017-12-27 00:32:11 -05:00
2020-08-21 23:17:48 -04:00
For a more universal solution: both LSP servers and irony will recognize a
[[https://sarcasm.github.io/notes/dev/compilation-database.html#ninja ][compilation database ]] (a ~compile_commands.json~ file). There are [[https://sarcasm.github.io/notes/dev/compilation-database.html][many ways to
generate one]]. Here is an example using [[http://www.cmake.org/ ][CMake ]] and [[https://github.com/rizsotto/Bear ][bear ]]:
2017-12-27 00:32:11 -05:00
#+BEGIN_SRC sh
# For CMake projects
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
# For non-CMake projects
make clean
bear make
#+END_SRC
Use ~M-x +cc/reload-compile-db~ to reload your compile db in an already-open
C/C++/ObjC buffer.
2019-01-20 14:22:45 -05:00
*** Known issues with bear on macOS
2019-01-07 19:28:59 +01:00
MacOS' [[https://support.apple.com/en-us/HT204899 ][System Integrity Protection (SIP) ]] might interfere with bear if ~make~ is
under ~/usr/bin/~ which results in an empty compilation database.
From the bear [[https://github.com/rizsotto/Bear#empty-compilation-database-on-os-x-captain-or-fedora ][readme ]]:
#+begin_quote
Security extension/modes on different operating systems might disable library
preloads. This case Bear behaves normally, but the result compilation database
will be empty. (Please make sure it's not the case when reporting bugs.) Notable
examples for enabled security modes are: OS X 10.11 (check with csrutil status |
grep 'System Integrity Protection'), and Fedora, CentOS, RHEL (check with
sestatus | grep 'SELinux status').
Workaround could be to disable the security feature while running Bear. (This
might involve reboot of your computer, so might be heavy workaround.) Another
option if the build tool is not installed under certain directories. Or use
tools which are using compiler wrappers. (It injects a fake compiler which does
record the compiler invocation and calls the real compiler too.) An example for
such tool might be scan-build. The build system shall respect CC and CXX
environment variables.
#+end_quote
A workaround might be to install ~make~ via Homebrew which puts ~gmake~
under ~/usr/local/~ .
#+BEGIN_SRC sh
brew install make
#+END_SRC
#+BEGIN_SRC sh
make clean
bear gmake
#+END_SRC
Additional info:
+ [[https://github.com/rizsotto/Bear/issues/158 ][Empty compilation database with compiler in /usr/local ]]
+ [[https://github.com/rizsotto/Bear/issues/152 ][Workaround for 'Empty compilation database on OS X Captain ]]
2020-05-28 11:55:32 +02:00
* Appendix
** Eglot specific bindings
When using =+lsp= and =:tools (lsp +eglot)= , lsp-mode is replaced with eglot,
and an additional function to get inheritance type hierarchy is added
| Binding | Description |
|------------------------------+--------------------------------------------------|
| ~<localleader> c t~ | ~Display inheritance type hierarchy (upwards)~ |
| ~<prefix> <localleader> c t~ | ~Display inheritance type hierarchy (downwards)~ |