Emacs as C++ IDE

9163 ワード


this blog will make your emacs as C++ IDE. It implements code-completion, google-style-check and project manager.
<!--
/*--><![CDATA[/*><!--*/
.title { text-align: center; }
.todo { font-family: monospace; color: red; }
.done { color: green; }
.tag { background-color: #eee; font-family: monospace;
padding: 2px; font-size: 80%; font-weight: normal; }
.timestamp { color: #bebebe; }
.timestamp-kwd { color: #5f9ea0; }
.right { margin-left: auto; margin-right: 0px; text-align: right; }
.left { margin-left: 0px; margin-right: auto; text-align: left; }
.center { margin-left: auto; margin-right: auto; text-align: center; }
.underline { text-decoration: underline; }
#postamble p, #preamble p { font-size: 90%; margin: .2em; }
p.verse { margin-left: 3%; }
pre {
border: 1px solid #ccc;
box-shadow: 3px 3px 3px #eee;
padding: 8pt;
font-family: monospace;
overflow: auto;
margin: 1.2em;
}
pre.src {
position: relative;
overflow: visible;
padding-top: 1.2em;
}
pre.src:before {
display: none;
position: absolute;
background-color: white;
top: -10px;
right: 10px;
padding: 3px;
border: 1px solid black;
}
pre.src:hover:before { display: inline;}
pre.src-sh:before { content: 'sh'; }
pre.src-bash:before { content: 'sh'; }
pre.src-emacs-lisp:before { content: 'Emacs Lisp'; }
pre.src-R:before { content: 'R'; }
pre.src-perl:before { content: 'Perl'; }
pre.src-java:before { content: 'Java'; }
pre.src-sql:before { content: 'SQL'; }

table { border-collapse:collapse; }
caption.t-above { caption-side: top; }
caption.t-bottom { caption-side: bottom; }
td, th { vertical-align:top; }
th.right { text-align: center; }
th.left { text-align: center; }
th.center { text-align: center; }
td.right { text-align: right; }
td.left { text-align: left; }
td.center { text-align: center; }
dt { font-weight: bold; }
.footpara:nth-child(2) { display: inline; }
.footpara { display: block; }
.footdef { margin-bottom: 1em; }
.figure { padding: 1em; }
.figure p { text-align: center; }
.inlinetask {
padding: 10px;
border: 2px solid gray;
margin: 10px;
background: #ffffcc;
}
#org-div-home-and-up
{ text-align: right; font-size: 70%; white-space: nowrap; }
textarea { overflow-x: auto; }
.linenr { font-size: smaller }
.code-highlighted { background-color: #ffff00; }
.org-info-js_info-navigation { border-style: none; }
#org-info-js_console-label
{ font-size: 10px; font-weight: bold; white-space: nowrap; }
.org-info-js_search-highlight
{ background-color: #ffff00; color: #000000; font-weight: bold; }
-->
Table of Contents
  • 1. auto completion
  • 1.1. auto-completion
  • 1.2. yasnippet
  • 1.3. auto-complete-c-headers

  • 2. google style
  • 2.1. google-cpplint
  • 2.2. clang

  • 3. project manager
  • 3.1. projectile


  • 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 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.