Use clang to complete C ++ in Emacs.

Source: Internet
Author: User

Author: winterttr reprinted, please note

I think many people want to implement C ++ auto-completion in vim or Emacs, and I am no exception.

We 've moved through ctags, etags, csags, and cedet. To be honest, cedet can still achieve completion, but the speed is a bit unbearable.

The preceding tools are used to pre-process the source code and analyze the code information. However, most programs analyze code through static analysis, which is a headache when the code or header file changes frequently. Code complementing in visual stuido is basically dynamic. When the header file changes, the completion will also change immediately.

Can we also implement this function?

We may have to say sorry before the appearance of the C ++ front-end of the llvm clang. Static ctags must completely scan all files, which is unacceptable.

But now we have clang, which provides us with a very good tool to achieve this desire.

Preparations

1. Needless to say, clang is required.

Clang needs to be compiled by yourself. Because I am using Windows, there is a self-compiled version here.

Https://github.com/winterTTr/emacs-of-winterTTr/tree/master/.emacs.d/extra-bin/clang

For environment settings, refer to the previous article.

[Plain]
View plaincopyprint?
  1. (Wttr/prepend-to-Exec-path "~ /. Emacs. d/extra-bin/clang ")
(wttr/prepend-to-exec-path "~/.emacs.d/extra-bin/clang")

2. Auto-complete and auto-complete-clang.el

Auto-complete is a very useful Emacs extension, and auto-complete-Clang is based on this extension. Therefore, the two must be installed simultaneously:

Here is the auto-complete extension I am currently using, which already contains clang.

Https://github.com/winterTTr/emacs-of-winterTTr/tree/master/.emacs.d/plugins/auto-complete-1.3.1

Configuration

First, load the auto-complete extension:

[Plain]
View plaincopyprint?
  1. ; Load Auto-complete
  2. (Wttr/prepend-to-load-path "~ /. Emacs. d/plugins/auto-complete-1.3.1 ")
  3. (Require 'pos-tip)
  4. (Require 'auto-complete)
  5. (Require 'auto-complete-config)
  6. (Add-to-list 'ac-dictionary-directories "~ /. Emacs. d/plugins/auto-complete-1.3.1/dict ")
  7. (Ac-config-default)
  8. (Global-auto-complete-mode t)
  9. ; Use Ctrl + enter to trigger auto-completion
  10. (Define-key ac-mode-map [(control return)] 'auto-complete)
; Load Auto-complete (wttr/prepend-to-load-path "~ /. Emacs. d/plugins/auto-complete-1.3.1 ") (require 'pos-tip) (require 'auto-complete-config) (add-to-list 'ac-dictionary-directories "~ /. Emacs. d/plugins/auto-complete-1.3.1/dict ") (Ac-config-default) (Global-auto-complete-mode t );; use Ctrl + enter to trigger Automatic completion (define-key ac-mode-map [(control return)] 'auto-complete)

 

Set auto-complete-clang below

[Plain]
View plaincopyprint?
  1. ; Load clang
  2. (Require 'auto-complete-clang)
  3. ; Add c-mode and C ++-mode hooks to enable auto-complete clang Extension
  4. (Defun wttr/ac-CC-mode-setup ()
  5. (Make-local-variable 'ac-auto-start)
  6. (Setq ac-auto-start nil); Auto complete using Clang is CPU sensitive
  7. (Setq ac-sources (append' (Ac-source-clang ac-source-yasnippet) Ac-sources )))
  8. (Add-hook 'C-mode-hook 'wttr/ac-CC-mode-Setup)
  9. (Add-hook 'C ++-mode-hook 'wttr/ac-CC-mode-Setup)
; Load clang (require 'auto-complete-clang); add c-mode and C ++-mode hooks, enable auto-complete clang extension (defun wttr/ac-CC-mode-setup () (make-local-variable 'ac-auto-start) (setq ac-auto-start nil); Auto complete using Clang is CPU sensitive (setq ac-sources (append' (Ac-source-clang ac-source-yasnippet) ac-sources) (add-hook 'C-mode-hook' wttr/ac-CC-mode-Setup) (add-hook 'C ++-mode-hook 'wttr/ac-CC-mode-Setup)

Add the system header fileDirectoryClang correctly finds all system header files by setting the ac-clang-flags variable.

However, note that the * directory * of the header file is not added *.

In fact, this variable is the parameter that clang can accept in the command line.-ipath indicates include this path as the system header file path. -Dsome indicates defining the macro "some.

Anyone who wants to use GCC knows that this is the command line parameter of the compiler.

I am using the path of the mingw header file in windows, so the settings are as follows:

[Plain]
View plaincopyprint?
  1. (Setq ac-clang-flags (list
  2. "-Ic:/mingw/include"
  3. "-Ic:/mingw/lib/GCC/mingw32/4.6.1/include"
  4. "-Ic:/mingw/lib/GCC/mingw32/4.6.1/include/C ++"
  5. "-Ic:/mingw/lib/GCC/mingw32/4.6.1/include/C ++/mingw32"
  6. "-Dsf-msvcrt __= "))
(setq ac-clang-flags  (list                                  "-IC:/MinGW/include"                                 "-IC:/MinGW/lib/gcc/mingw32/4.6.1/include"                                 "-IC:/MinGW/lib/gcc/mingw32/4.6.1/include/c++"                                 "-IC:/MinGW/lib/gcc/mingw32/4.6.1/include/c++/mingw32"                                 "-D__MSVCRT__="))

Now, you can open a test. cpp file and try again. Due to my settings, it is triggered by pressing Ctrl-Enter.

Auto-complete is a very good framework.

PS:

Wttr starts with all functions and is defined by myself. For more information, see here.

Https://github.com/winterTTr/emacs-of-winterTTr/blob/master/.emacs.d/wttr-config/wttr-utils.el

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.