Emacs as C++ IDE

 

this blog will make your emacs as C++ IDE. It implements code-completion, google-style-check and project manager.

1 auto completion

 

1.1 auto-completion

(require 'auto-complete)
(require 'auto-complete-config)
(ac-config_default)

Auto-completion is a basic plugin. When you input the existed string in a file, emacs will give your some tips.

1.2 yasnippet

(require 'yasnippet)
(yas-global-mode 1)

Yasnnipet is used to complete your code with code-fragments. For example, when you input "for" and press tab in a cpp file, code will be automatically completed as below:

for (i = 0; i < N; i++) {    
}

1.3 auto-complete-c-headers

(defun my:ac-c-header-init()
  (require 'auto-complete-c-headers)
  (add-to-list 'ac-sources 'ac-source-c-headers)
  (add-to-list 'achead:include-directories '(("/usr/include/c++/4.8")
											 ("/usr/include/x86_64-linux-gnu/c++/4.8")
											 ("/usr/include/c++/4.8/backward")
											 ("/usr/lib/gcc/x86_64-linux-gnu/4.8/include")
											 ("/usr/local/include")
											 ("/usr/lib/gcc/x86_64-linux-gnu/4.8/include-fixed")
											 ("/usr/include"))))
(add-hook 'c++-mode-hook 'my:ac-c-header-init)
(add-hook 'c-mode-hook 'my:ac-c-header-init)

The "achead:include-directories" are my configuration. The include-directories vary with different systems. In terminal you can check include-directories as below command:

gcc -xc++ -E -v -

Use this plugins, when you press "#include <f", emacs will give your some tips, such as "float.h", "fstream.h" and so on.

2 google style

 

2.1 google-cpplint

Some tools are required in your system

sudo apt-get install python-pip python-dev build-essential
sudo pip install cpplint

Then your can configure your emacs.

(defun my:flymake-google-init()
  (require 'flymake-google-cpplint)
  (custom-set-variables
   '(flymake-google-cpplint-command "/usr/local/bin/cpplint"))
  (flymake-google-cpplint-load))
(add-hook 'c-mode-hook 'my:flymake-google-init)
(add-hook 'c++-mode-hook 'my:flymake-google-init)

(require 'google-c-style)
(add-hook 'c-mode-common-hook 'google-set-c-style)
(add-hook 'c-mode-common-hook 'google-make-newline-indent)

This is my configure and maybe your cpplint-command directory isn't "/usr/local/bin/cpplint". you can set the directory as below command.

whereis cpplint

Using cpplint, you will see some "!, ?" at the begging of line in your .cpp or .h file, because your code isn't fit with google style.

2.2 clang

sudo apt-get install clang3.5 libclang-dev
git clone https://github.com/Sarcasm/irony-mode.git
git clone https://github.com/MJPA/SimpleJSON.git
mv irony-mode* irony-mode
mv SimpleJSon* SimpleJSon
mv SimpleJSon  irony-mode/lib
mv SimpleJSon ~/.emacs.d/
cd ~/.emacs.d/SimpleJSon/build
cmake ..
make
sudo make install

The clang version varies with diffrent systems. you can check the package version in your system as the below command:

sudo apptitude search clang

Then you can configure your emacs as below:

(setenv "LD_LIBRARY_PATH" "/usr/lib/llvm-3.5/lib/")
(add-to-list 'load-path (expand-file-name "~/.emacs.d/irony-mode/elisp/"))
(require 'irony)
(irony-enable 'ac)
(defun my:irony-enable()
  (when (member major-mode irony-known-modes)
	(irony-mode 1)))
(add-hook 'c++-mode-hook 'my:irony-enable)
(add-hook 'c-mode-hook 'my:irony-enable)

Maybe the llvm-lib directory isn't "usr/lib/llvm-3.5/lib". you can search llvm-lib directory as below:

find / -name "llvm"

In reality, We setenv llv-lib because of iron's run is dependent on llvm-lib and it equals with adding llvm-lib in "/etc/ld.so.conf" file.

3 project manager

 

3.1 projectile

(require 'projectile)

;; 默认全局使用
(projectile-global-mode)
;; 默认打开缓存
(setq projectile-enable-caching t)
;; 使用f5键打开默认文件搜索
(global-set-key [f5] 'projectile-find-file)

In your project-root-directory, you can create a empty file named with ".projectile". When you open any file in your project and press f5, you can rapidly open any other file in your project you want to open. you will benefit from the multi-hierarchy-folder projects.

posted @ 2014-06-08 22:15  CharellkingQu  阅读(2817)  评论(0编辑  收藏  举报