2022-03-28 15:03:21 +02:00
#+title : :lang cc
#+subtitle : C > C++ == 1
#+created : February 20, 2017
#+since : 2.0.0
2021-10-16 01:28:32 +02:00
* Description :unfold:
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
2021-10-16 01:28:32 +02:00
- Code completion (~company-irony~ )
- eldoc support (~irony-eldoc~ )
- Syntax-checking (~flycheck-irony~ )
- Code navigation (~rtags~ )
- File Templates ([[../../editor/file-templates/templates/c-mode ][c-mode ]], [[../../editor/file-templates/templates/c++-mode ][c++-mode ]])
- 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 ]])
- Several improvements to C++11 indentation and syntax highlighting.
** Maintainers
- [[doom-user: ][@hlissner ]]
[[doom-contrib-maintainer: ][Become a maintainer? ]]
** Module flags
- +lsp ::
Enable LSP support for ~c-mode~ , ~c++-mode~ , and ~objc-mode~ . Requires [[doom-module:][:tools
lsp]] and a langserver (supports ccls, clangd, and cquery). Replaces irony &
rtags.
2022-04-09 18:38:04 +03:00
- +tree-sitter ::
Leverages tree-sitter for better syntax highlighting and structural text
editing. Requires [[doom-module: ][:tools tree-sitter ]].
2021-10-16 01:28:32 +02:00
** Packages
- [[doom-package: ][cmake-mode ]]
- [[doom-package: ][company-glsl ]]
- [[doom-package: ][cuda-mode ]]
- [[doom-package: ][demangle-mode ]]
- [[doom-package: ][disaster ]]
- [[doom-package: ][glsl-mode ]]
- [[doom-package: ][modern-cpp-font-lock ]]
- [[doom-package: ][opencl-mode ]]
- if [[doom-module: ][+lsp ]]
- [[doom-package: ][ccls ]] if [[doom-module: ][:tools lsp -eglot ]]
- else
- [[doom-package: ][company-irony ]]
- [[doom-package: ][company-irony-c-headers ]]
- [[doom-package: ][flycheck-irony ]]
- [[doom-package: ][helm-rtags ]] if [[doom-module: ][:completion helm ]]
- [[doom-package: ][irony ]]
- [[doom-package: ][irony-eldoc ]]
- [[doom-package: ][ivy-rtags ]] if [[doom-module: ][:completion ivy ]]
- [[doom-package: ][rtags ]]
** Hacks
/No hacks documented for this module./
** TODO Changelog
# This section will be machine generated. Don't edit it by hand.
/This module does not have a changelog yet./
* Installation
[[id:01cffea4-3329-45e2-a892-95a384ab2338 ][Enable this module in your ~doom!~ block. ]]
2020-08-21 23:17:48 -04:00
This module's requirements change depending on how you use it.
2021-10-16 01:28:32 +02:00
- If [[doom-module: ][+lsp ]] is enabled, you need one of *clangd v9+* or *ccls* .
- If [[doom-module: ][+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= .
2020-08-21 23:17:48 -04:00
** LSP servers
=lsp-mode= and =eglot= support a few LSP servers, but =clangd= and =ccls= are
recommended.
2021-02-23 16:51:47 -08:00
+ clangd (must be v9 or newer) :: clangd is included with =llvm= which should be
2020-08-21 23:17:48 -04:00
available through your OS' package manager.
2021-02-23 16:51:47 -08:00
- Linux:
2021-10-16 01:28:32 +02:00
- Debian 11 & Ubuntu 20.10: ~$ apt-get install clangd-11~
2021-02-23 16:51:47 -08:00
- 20.04 LTS: [[https://pkgs.org/search/?q=clangd ][clangd-10 ]]
2021-10-16 01:28:32 +02:00
- Fedora & CentOS/RHEL 8+: ~$ dnf install clang-tools-extra~
- openSUSE: ~$ zypper install clang~
- Arch: ~$ pacman -S clang~
2021-02-23 16:51:47 -08:00
- BSDs:
2021-10-16 01:28:32 +02:00
- NetBSD & OpenBSD: ~$ pkg_add clang-tools-extra~
2021-02-23 16:51:47 -08:00
- macOS: ~$ brew install llvm~ // 1GB+ installation! May take a while!
- Windows: use the win64 installer from [[https://releases.llvm.org/download.html ][LLVM's GitHub release page ]].
2020-08-21 23:17:48 -04:00
+ 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 ]].
2020-11-18 06:25:36 +01:00
+ cmake-language-server :: available through ~pip~ on most distributions
2017-12-27 00:32:11 -05:00
** irony-server
Irony powers the code completion, eldoc and syntax checking systems.
2021-02-23 16:51:47 -08:00
After installing its dependencies (Clang and CMake), run ~M-x
irony-install-server~ in Emacs.
2019-01-05 19:46:14 -05:00
2020-08-21 23:17:48 -04:00
*** macOS
Due to linking issues, macOS users must compile irony-server manually:
2021-10-16 01:28:32 +02:00
#+begin_src sh
2017-05-04 10:46:12 +02:00
brew install cmake
2021-02-23 16:51:47 -08:00
brew install llvm
2017-05-25 20:08:50 +02:00
git clone https://github.com/Sarcasm/irony-mode irony-mode
2021-10-16 01:28:32 +02:00
#+end_src
2021-02-23 16:51:47 -08:00
2021-10-16 01:28:32 +02:00
#+begin_src sh
2017-05-25 20:08:50 +02:00
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"
2021-02-23 16:51:47 -08:00
# Cleanup
2017-05-25 20:08:50 +02:00
popd
rm -rf irony-mode
2021-10-16 01:28:32 +02:00
#+end_src
2017-05-04 10:46:12 +02:00
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
2021-10-16 01:28:32 +02:00
#+begin_src sh
2017-12-27 00:32:11 -05:00
rdm &
rc -J $PROJECT_ROOT # loads PROJECT_ROOT's compile_commands.json
2021-10-16 01:28:32 +02:00
#+end_src
* TODO Usage
#+begin_quote
🔨 /This module's usage documentation is incomplete./ [[doom-contrib-module: ][Complete it? ]]
#+end_quote
1. Enable this module.
2. Enable the ~+lsp~ flag for the appropriate modules you want LSP support for
(e.g. ~:lang (python +lsp)~ or ~:lang (rust +lsp)~ ),
3. Install the prerequisite LSP servers through your package manager or other
means. You can find a list of supported servers on [[https://github.com/emacs-lsp/lsp-mode#supported-languages ][the lsp-mode project page ]].
4. Run ~$ doom sync~ on the command line and restart Emacs.
** Eglot-specific bindings
When using [[doom-module: ][+lsp ]] and [[doom-module: ][:tools lsp +eglot ]], [[doom-package: ][lsp-mode ]] is replaced with [[doom-package: ][eglot ]], and an
additional function to get inheritance type hierarchy is added:
| Binding | Description |
|----------------------------+------------------------------------------------|
| [[kbd:][<localleader> c t]] | Display inheritance type hierarchy (upwards) |
| [[kbd:][<prefix> <localleader> c t]] | Display inheritance type hierarchy (downwards) |
* TODO Configure
#+begin_quote
🔨 /This module's configuration documentation is incomplete./ [[doom-contrib-module: ][Complete it? ]]
#+end_quote
2017-12-27 00:32:11 -05:00
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
2021-10-16 01:28:32 +02:00
#+begin_src sh
2017-12-27 00:32:11 -05:00
# For CMake projects
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .
2021-10-16 01:28:32 +02:00
#+end_src
2017-12-27 00:32:11 -05:00
2021-10-16 01:28:32 +02:00
#+begin_src sh
2017-12-27 00:32:11 -05:00
# For non-CMake projects
make clean
bear make
2021-10-16 01:28:32 +02:00
#+end_src
2017-12-27 00:32:11 -05:00
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
2021-10-16 01:28:32 +02:00
A workaround might be to install ~make~ via Homebrew which puts ~gmake~ under
=/usr/local/= :
#+begin_src sh
2019-01-07 19:28:59 +01:00
brew install make
2021-10-16 01:28:32 +02:00
#+end_src
#+begin_src sh
2019-01-07 19:28:59 +01:00
make clean
bear gmake
2021-10-16 01:28:32 +02:00
#+end_src
2019-01-07 19:28:59 +01:00
Additional info:
2021-10-16 01:28:32 +02:00
- [[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
2020-11-13 01:35:54 +01:00
** Configure LSP servers
Search for your combination of =(LSP client package, LSP server)= . You are using
2021-10-16 01:28:32 +02:00
[[doom-package: ][lsp-mode ]] by default, [[doom-package: ][eglot ]] if you have [[doom-module: ][:tools lsp +eglot ]] active in
=$DOOMDIR/init.el= file.
2020-11-13 01:35:54 +01:00
*** LSP-mode with clangd
2021-10-16 01:28:32 +02:00
#+begin_src emacs-lisp
(after! lsp-clangd
(setq lsp-clients-clangd-args
'("-j=3"
"--background-index"
"--clang-tidy"
"--completion-style=detailed"
"--header-insertion=never"
"--header-insertion-decorators=0"))
(set-lsp-priority! 'clangd 2))
#+end_src
This will both set your clangd flags and choose =clangd= as the default LSP
server everywhere clangd can be used.
2020-11-13 01:35:54 +01:00
*** LSP-mode with ccls
2021-10-16 01:28:32 +02:00
#+begin_src emacs-lisp
2020-11-13 01:35:54 +01:00
(after! ccls
(setq ccls-initialization-options '(:index (:comments 2) :completion (:detailedLabel t)))
(set-lsp-priority! 'ccls 2)) ; optional as ccls is the default in Doom
2021-10-16 01:28:32 +02:00
#+end_src
2020-11-13 01:35:54 +01:00
This will both set your ccls flags and choose ccls as the default server. [[https://github.com/MaskRay/ccls/wiki/Customization#--initjson][CCLS
documentation]] lists available options, use =t= for ~true~ , =:json-false= for
~false~ , and =:json-null= for ~null~ .
*** Eglot with clangd
2021-10-16 01:28:32 +02:00
#+begin_src emacs-lisp
2020-11-13 01:35:54 +01:00
(set-eglot-client! 'cc-mode '("clangd" "-j=3" "--clang-tidy"))
2021-10-16 01:28:32 +02:00
#+end_src
2020-11-13 01:35:54 +01:00
This will both set your clangd flags and choose clangd as the default server (if
it is the last =set-eglot-client! 'cc-mode= in your config).
*** Eglot with ccls
2021-10-16 01:28:32 +02:00
#+begin_src emacs-lisp
2020-11-13 01:35:54 +01:00
(set-eglot-client! 'cc-mode '("ccls" "--init={\"index\": {\"threads\": 3}}"))
2021-10-16 01:28:32 +02:00
#+end_src
2020-11-13 01:35:54 +01:00
This will both set your ccls flags and choose ccls as the default server (if it
is the last =set-eglot-client! 'cc-mode= in your config). [[https://github.com/MaskRay/ccls/wiki/Customization#--initjson ][CCLS documentation ]]
lists available options
2021-10-16 01:28:32 +02:00
* Troubleshooting
/There are no known problems with this module./ [[doom-report: ][Report one? ]]
* Frequently asked questions
/This module has no FAQs yet./ [[doom-suggest-faq: ][Ask one? ]]
* TODO Appendix
#+begin_quote
🔨 This module has no appendix yet. [[doom-contrib-module: ][Write one? ]]
#+end_quote