因此我們大概會新加入用到的 Emacs 套件為: irony, rtags 跟 clang-format
Irony 在編譯時會使用到 cmake 跟 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
$sudo yum install cmake, clang-devel
(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
(add-hook 'flycheck-mode-hook #'flycheck-irony-setup)
)
(use-package irony-eldoc
:ensure t
:config
(add-hook 'irony-mode-hook #'irony-eldoc)
)
)
基本上就是安裝 irony,company-irony-c-headers,company-irony,flycheck-irony 跟 irony-eldoc 這些套件
在 Emacs 安裝好後,開啟第一個 C/C++ 檔案時,小緩衝區會提示要安裝 irony-server
使用 Meta-x irony-install-server
如圖
小緩衝區會帶出
Install command: cmake -DCMAKE_INSTALL_PREFIX\=/home/vagrant/.emacs.d/irony/ /home/vagrant/.emacs.d/elpa/irony-20180703.1740/server &\
& cmake --build . --use-stderr --config Release --target install
按 enter 來編譯,完成後 如圖
這樣就可以開始使用跟 irony 相關的功能
$brew install rtags
$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
安裝完後,系統會有 /usr/local/bin/rdm 跟 /usr/local/bin/rc 兩個程式
(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)
)
cmake-ide 是讓 Emacs 開啟 C/C++ 檔案時,自動啟動 rdm 跟 rc 如圖
最後安裝 cmake-format 在 Emacs 上
(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)))
)
指定在儲存 C/C++ 檔案前,先使用 clang-format-buffer 指令
如果使用 cmake 的話,還可以加裝 cmake-mode,使用 company-cmake 來做編輯 CMakeLists.txt 時的程式碼補全
(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)))
)
這樣把需要安裝的功能安裝好了,當然部分功能看個人需要,不一定全部要安裝
下一篇我們用一個實例來看這些功能如何使用