1

I'm new to Emacs. I followed a tutorial for Emacs as IDE for C/C++ but there are steps that do not work fully for me: the auto-complete for c-headers.

I installed yasnippet and auto-complete for C/C++ headers.

1) I get the normal auto-complete behavior for #include <stdio.h but > closing hook is not added even if I type tab on h .

2) And some functions (I don't know how many) aren't called even with their headers.

Other posts, particularly this one, helped me to solve an issue about #include <float when is not completed with .h, now it works but as alwas tab on h does not add >.

So after changing my .emacs from the link above , #include <ctype.h> doesn't call at least isdig knowing that I want isdigit ... And for getchar it doesn't add ()wherease for while, for loops it works fine...

Here is my new .emacs after following the link above.

; start package.el with emacs
(require 'package)
;add MELPA to repository list
(add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
;initialize package.el
(package-initialize)
;start auto-complete with emacs
(require 'auto-complete)
;do default config for auto-complete
(require 'auto-complete-config)
(ac-config-default)
;start yasineppet with emacs
(require 'yasnippet)
(yas-global-mode 1)
;let's define a function which initializes auto-complete-c-headers and gets called for c/c++ headers hooks
(defun my:ac-c-header-init ()
  (require 'auto-complete-c-headers)
  (add-to-list 'ac-sources 'ac-source-c-headers)
  (setq achead:include-directories
  (append '("/usr/include/c++/5"
        "/usr/include/x86_64-linux-gnu/c++/5"
        "/usr/include/c++/5/backward"
        "/usr/lib/gcc/x86_64-linux-gnu/5/include"
        "/usr/local/include"
        "/usr/lib/gcc/x86_64-linux-gnu/5/include-fixed"
        "/usr/include/x86_64-linux-gnu")
        achead:include-directories)))
;now let's call this function from c/c++ hooks
(add-hook 'c++-mode-hook 'my:ac-c-header-init)
(add-hook 'c-mode-hook 'my:ac-c-header-init)

Something is missing ? The path for headers comes from gcc -xc++ -E -v -

ignoring duplicate directory "/usr/include/x86_64-linux-gnu/c++/5"
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/lib/gcc/x86_64-linux-gnu/5/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/include/c++/5
 /usr/include/x86_64-linux-gnu/c++/5
 /usr/include/c++/5/backward
 /usr/lib/gcc/x86_64-linux-gnu/5/include
 /usr/local/include
 /usr/lib/gcc/x86_64-linux-gnu/5/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
  • I usually use yasnippet to insert #include<|> or #include"|", where | indicates the cursor position, then auto-complete kicks in when I press TAB, optionally after I have typed a few leading characters. In this way I've never run into your current issue. – InHarmsWay Aug 12 '16 at 9:57
1

I'm not sure how you would accomplish this with auto-complete but I do know that you can do all of this and more with company-mode, company-irony, company-irony-c-headers, and irony. You can find all of them on MELPA.

I'll walk you through my init.el file and you can make the relevant changes in yours accordingly. However, you should read the guides of the respective packages on their website instead of just copying mine.

First, enable company-mode using

(add-hook 'after-init-hook #'global-company-mode)

The following are some optimizations that I made. You may or may not want them.

(setq company-idle-delay 0.5)
(setq company-minimum-prefix-length 2)
(setq company-selection-wrap-around t)
(setq company-show-numbers t)
(setq company-tooltip-align-annotations t)

The following line enables irony-mode.

(add-hook 'c-mode-hook #'irony-mode)

I found the following code here and this is supposed to replace the default completion options with the options that irony-mode would provide.

(defun my-irony-mode-hook ()
  (define-key irony-mode-map [remap completion-at-point]
    'irony-completion-at-point-async)
  (define-key irony-mode-map [remap complete-symbol]
    'irony-completion-at-point-async))
(add-hook 'irony-mode-hook #'my-irony-mode-hook)
(add-hook 'irony-mode-hook #'irony-cdb-autosetup-compile-options)

Add company-irony-c-headers and company-irony to the list of backends that company-mode uses.

(eval-after-load 'company
  '(add-to-list
    'company-backends '(company-irony-c-headers company-irony)))
  • it doessn't works, after installations nothing is completed like #include <stdio .... When I open a file.c it shows me : Company backend 'company-clang' could not be initialized: Company found no clang executable While I had already uninstalled clang, and I have nothing of clang in my .emacs but somefiles from clang in usr/lib or usr/hare are still there don't know if it can make conflict with new installations ... sudo apt-get remove clang : clang isn't installed nothing to delet... I have to install clang to not have those errors, but no completion again ... – F.A Aug 22 '16 at 17:35
  • @F.A You should read the guides of the respective packages on their website. From what I understood, you haven't installed clang and that is the reason company is giving you errors. You should install clang and try again. – Libre Arch Aug 22 '16 at 19:12
  • already done and no completion as I said " I have to install clang to not have those errors, but no completion again ..." – F.A Aug 22 '16 at 19:17
  • Can you give the output of clang --version and clang++ --version just to check what it says if this is still a problem? – zaile Nov 3 '16 at 19:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.