doomemacs/modules/lang/cc
2019-03-07 01:35:22 +10:00
..
autoload.el Fix cc fontification for constants with numbers 2019-03-07 01:35:22 +10:00
config.el Refactor lang/cc 2019-03-02 01:51:51 -05:00
doctor.el Use configured variables for rtags binary names 2019-02-27 12:53:03 +05:30
packages.el Replace cquery with ccls for C/C++ LSP support 2019-02-25 22:43:59 +10:00
README.org

lang/cc

Description

This module adds support for the C-family of languages: C, C++, and Objective-C.

  • Code completion (company-irony)
  • eldoc support (irony-eldoc)
  • Syntax-checking (flycheck-irony)
  • Code navigation (rtags)
  • File Templates (c-mode, c++-mode)
  • Snippets (cc-mode, c-mode, c++-mode)
  • Several improvements to C++11 indentation and syntax highlighting.

Module Flags

  • +irony Enable Irony as a backend for code completion, syntax checking, and eldoc support. This must be disabled to use LSP or another backend.
  • +rtags Enable rtags integration. A daemon will be spawned the first time you open a C/C++/ObjC buffer, if one hasn't already.

Prerequisites

This module requires

  • irony-server (if +irony is enabled)
  • rtags (if +rtags is enabled)

irony-server

Irony powers the code completion, eldoc and syntax checking systems.

After installing its dependencies, run M-x irony-install-server in Emacs.

MacOS

Due to linking issues, MacOS users must compile irony-server manually:

brew install cmake
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

DEST="$HOME/.emacs.d/.local/$(hostname)/etc/irony-server/"
cmake -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=ON \
      -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

Arch Linux

pacman -S clang cmake

rtags

Code navigation requires an rtags server (rdm) installed. This should be available through your OS's package manager.

This module will auto-start rdm when you open C/C++ buffers (so long as one isn't already running). If you prefer to run it yourself:

rdm &
rc -J $PROJECT_ROOT  # loads PROJECT_ROOT's compile_commands.json

Configure

Project compile settings

By default, a set of default compile settings are defined in +cc-default-compiler-options for C, C++ and Objective C. Irony, rtags and flycheck will fall back to these.

To make these tools aware of project specific build settings, you need a JSON compilation database present (i.e. a compile_commands.json file).

There are many ways to generate one. I use CMake or bear:

# For CMake projects
cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=ON .

# For non-CMake projects
make clean
bear make

Use M-x +cc/reload-compile-db to reload your compile db in an already-open C/C++/ObjC buffer.

Known issues with bear on macOS

MacOS' System Integrity Protection (SIP) might interfere with bear if make is under /usr/bin/ which results in an empty compilation database.

From the bear readme:

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.

A workaround might be to install make via Homebrew which puts gmake under /usr/local/.

brew install make
make clean
bear gmake

Additional info: