Emacs c/c++ mode

洋蔥工作室

Emacs c/c++

  1. Irony-mode
    • 自動程式碼完成: 可使用 company-irony
    • 語法檢查:使用 flycheck
    • 支援文件 eldoc
    • 跟 counsel 配合
  2. Rtags
    • 跳到函數定義
  3. clang-format
    • 程式自動排版

Youtube 影片

Irony-mode 預先安裝

  • 第一次啟動需要安裝 irony-server
  • 系統須先要安裝 clang, cmake, clang-devel (for LibClang)

Irony-mode

  • Linux (CentOS 7)
$sudo yum install cmake, clang-devel
  • Mac OS: for Mac, need to add below library to .code_completion, you need cmake and libclang
# for cmake
$brew install cmake

# for libclang
$brew install llvm
$echo 'export PATH="/usr/local/opt/llvm/bin:$PATH"' >> ~/.bash_profile

$echo | clang -x c++ -v -E - 2>&1 | sed -n '/^#include </,/^End/s|^[^/]*\([^ ]*/include[^ ]*\).*$|-I\1|p' > .clang_completion

Irony-mode 安裝

(use-package irony
  :ensure t
  :hook ((c++-mode . irony-mode)
         (c-mode . irony-mode))
  :config
  (add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)
  (use-package company-irony-c-headers
    :ensure t)
  (use-package company-irony
    :ensure t
    :config
    (add-to-list (make-local-variable 'company-backends) '(company-irony company-irony-c-headers)))
  (use-package flycheck-irony
  :ensure t
  :config
    (eval-after-load 'flycheck '(add-hook 'flycheck-mode-hook #'flycheck-irony-setup)))
  (use-package irony-eldoc
  :ensure t
  :config
    (add-hook 'irony-mode-hook #'irony-eldoc))
  )

Rtags 預先安裝 (Mac OS)

$brew install rtags

Rtags 預先安裝 (Linux CentOS 7),因沒有套件,須自己編譯

$sudo yum install llvm-devel
$sudo yum install cppunit-devel

#install gcc-4.9
$sudo yum install centos-release-scl-rh
$sudo yum install devtoolset-3-gcc devtoolset-3-gcc-c++
$update-alternatives --install /usr/bin/gcc-4.9 gcc-4.9 /opt/rh/devtoolset-3/root/usr/bin/gcc 10
$update-alternatives --install /usr/bin/g++-4.9 g++-4.9 /opt/rh/devtoolset-3/root/usr/bin/g++ 10

#install cmake 3.1
$git clone https://gitlab.kitware.com/cmake/cmake.git
$./bootstrap
$make
$sudo make install

# make rtags
$git clone --recursive https://github.com/Andersbakken/rtags.git
$cd rtags
# to change cmake gcc-4.9 and g++-4.9
$ccmake .
$cmake -DCMAKE_EXPORT_COMPILE_COMMANDS=1 .
$make
$sudo make install

Rtags 安裝

(use-package rtags
  :ensure t
  :config
  (rtags-enable-standard-keybindings)
  (setq rtags-autostart-diagnostics t)
  (rtags-diagnostics)
  (setq rtags-completions-enabled t)
  (rtags-start-process-unless-running)
  )


(use-package cmake-ide
  :ensure t
  :config
  (cmake-ide-setup)
  )

clang-format 安裝

(use-package clang-format
  :ensure t
  :config
  (setq clang-format-style-option "llvm")
  (add-hook 'c-mode-hook (lambda() (add-hook 'before-save-hook 'clang-format-buffer)))
  (add-hook 'c++-mode-hook (lambda() (add-hook 'before-save-hook 'clang-format-buffer)))
  )

cmake-mode 安裝,如果使用 cmake 的話

(use-package cmake-mode
  :ensure t
  :mode (("CMakeLists\\.txt\\'" . cmake-mode)
         ("\\.cmake\\'" . cmake-mode))
  :config
  (add-hook 'cmake-mode-hook (lambda()
                               (add-to-list (make-local-variable 'company-backends)
                                            'company-cmake)))
  )