# Configuring yasnippet and autocomplete to work with C/C++ headers

My computer system and set up are

OS: Arch Linux 64bit
emacs version: 24.4
alias emacs='emacsclient -t -s server --alternate-editor='


Update:

auto complete is working with yasnippet occasionally now but not as expected. I changed the line

(add-to-list 'achead:include-directories
'"/usr/include/c++/4.9.2/")


If I type #include <io and hit tab, I get back #include <#include <iostream>. Therefore, I then tried just typing <io and tab which produces <#include <iostream>. If I type ios and hit type, the desired result is achieved. I then tested this with float.h. No iteration works.

I then tried re-testing io and tab. This doesn't work 100% of the time. 50% of the time I get #include <iostream> and the other 50% nothing happens. Needless to say, I have no idea what is going on with this package or feature.

I am trying to get auto complete to work with C/C++ headers; however, I have been unable to do so. I executed the following command in the terminal to locate the C/C++ headers:

[dustin@dustin ~]$g++ -print-prog-name=cc1plus -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/lto-wrapper /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/cc1plus  but using /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/cc1plus does not produce the desired outcome. I have also ran a search looking for float.h just to determine locations as well and came up with [dustin@dustin ~]$ sudo find / -name float.h
/usr/include/c++/4.9.2/tr1/float.h
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/include/float.h


So I also tried /usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/include. I am not to sure about /usr/include/c++/4.9.2/tr1 though.

Below is the relevant information in my .emacs. All packages were downloaded and install from melpa are up-to-date. If I run 'emacs' --debug-init, there are no error messages. The ' ' around emacs is so that emacs will open without options for debugging only.

;; Start server and set directory
(setq server-socket-dir (format "/tmp/emacs1000"))

;; Package list M-x package-list-packages
(require 'package)
'("melpa" . "http://melpa.milkbox.net/packages/"))

;; Package initialize
(package-initialize)

;; C/C++ configuration
(require 'cc-mode)

;; auto complete for C/C++
(require 'auto-complete)
;; default config for auto-complete
(require 'auto-complete-config)
(ac-config-default)

;; start yasnippets
(require 'yasnippet)
(yas-global-mode 1)

'"/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.2/include")
)

;; call ac-c-headers from c/c++ hooks

• Seems like you are new to Emacs. If so, my C/C++ guide may help you get a good development environment with IDE like features like navigate around large project with 30k files. – Tu Do Apr 15 '15 at 16:38

You can use hippie-expand and avoid all this key binding conflict headache altogether:

  (global-set-key (kbd "M-/") 'hippie-expand) ;; replace dabbrev-expand
(setq hippie-expand-try-functions-list
'(
;; Try to expand yasnippet
yas-hippie-try-expand
;; Try to expand word "dynamically", searching the current buffer.
try-expand-dabbrev
;; Try to expand word "dynamically", searching all other buffers.
try-expand-dabbrev-all-buffers
;; Try to expand word "dynamically", searching the kill ring.
try-expand-dabbrev-from-kill
;; Try to complete text as a file name, as many characters as unique.
try-complete-file-name-partially
;; Try to complete text as a file name.
try-complete-file-name
;; Try to expand word before point according to all abbrev tables.
try-expand-all-abbrevs
;; Try to complete the current line to an entire line in the buffer.
try-expand-list
;; Try to complete the current line to an entire line in the buffer.
try-expand-line
;; Try to complete as an Emacs Lisp symbol, as many characters as
;; unique.
try-complete-lisp-symbol-partially
;; Try to complete word as an Emacs Lisp symbol.
try-complete-lisp-symbol))


Now, whenever you want to expand a snippet, press M-/ and want to complete, press <tab>.

• It only expands to float not float.h. Is that intended? – dustin Apr 15 '15 at 16:59
• @dustin I added the function for expanding yasnippet snippets yas-hippie-try-expand. Sorry about that. It should expand snippets properly now. As for completing headers, I'm not sure about it. I only use company-c-header. As for hippie-expand, it only expands from the candidates generated by functions in the list, so it won't complete your header files. – Tu Do Apr 15 '15 at 17:27

I found on SO the following answer that works by abdimuna:

First locate C/C++ headers on your computer by running the following command.

gcc -xc++ -E -v -


On my ArchLinux they are located here

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/../../../../include/c++/4.9.1
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/../../../../include/c++/4.9.1/x86_64-unknown-linux-gnu
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/../../../../include/c++/4.9.1/backward
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/include
/usr/local/include
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/include-fixed
/usr/include


Then the complete C/C++ auto-completion which works for me is this.

;; C++ auto completion mode
(require 'auto-complete)
(require 'auto-complete-config)
(ac-config-default)
;a function which initializes auto-complete-c-headers and get called for c/c++ hooks