3

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
F.A
  • 31
  • 3
  • 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 09:57

1 Answers1

2

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)))
Scott Weldon
  • 2,695
  • 1
  • 17
  • 31
Libre Arch
  • 41
  • 5
  • it doessn't works, after installations nothing is completed like #include – 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 03 '16 at 19:28
  • 1
    Didn't work for me either. This whole thing is a confusing mess. God forbid the developers of this crap actually document something properly. – mrbean Jul 11 '20 at 09:21
  • Doesn't work is how I would describe my efforts trying multiple solutions, spending most time on irony-mode, documentation is pretty light, there's some steps on installing irony but it required a lot of redos and errors and looking at other sources. And then it doesn't really explain how you are supposed to get a compiler database and all that, I'm just left guessing and experimenting, I dunno if I am just bad at reading docs and sucks – Dennis Dec 29 '22 at 04:20
  • Honestly what I wanted I found in sublime text, it worked exactly like I wanted it, that's a nice app. VSCode probably does all this as well too. But I wanted to start with something simpler and more nitty gritty since I did not want an IDE but work directly with an editor and GCC and learn makefiles. But it feels like I can't work with emacs even, real confidence killer – Dennis Dec 29 '22 at 04:25