{"id":51,"date":"2012-07-02T18:18:00","date_gmt":"2012-07-02T18:18:00","guid":{"rendered":"https:\/\/www.root42.de\/blog\/?p=51"},"modified":"2012-07-02T18:18:00","modified_gmt":"2012-07-02T18:18:00","slug":"a-nice-c-autocomplete-configuration-for-emacs","status":"publish","type":"post","link":"https:\/\/www.root42.de\/blog\/?p=51","title":{"rendered":"A nice C++ autocomplete configuration for Emacs"},"content":{"rendered":"<p><b>Update:<\/b> I have written a <a href=\"http:\/\/root42.blogspot.de\/2014\/11\/autocompletion-for-c-and-c-with-emacs-24.html\">new blog post<\/a> about this, with updated information and using new and current packages.<\/p>\n<p>Autocompletion for C++ in Emacs is a hot topic &#8212; at least for Emacs users. At least for Emacs users who code C++&#8230; So here is my setup that I use, and which works reasonably well. I use the <span style=\"font-family: 'Courier New',Courier,monospace;\">autocomplete<\/span> package with the <span style=\"font-family: 'Courier New',Courier,monospace;\">autocomplete-clang<\/span> extension.<\/p>\n<div style=\"clear: both; text-align: center;\"><a href=\"http:\/\/4.bp.blogspot.com\/-GbUbO0yZwGs\/T_MOXc4RdhI\/AAAAAAAACkU\/tIW51gHBOCM\/s1600\/auto-complete-clang-1.png\" style=\"margin-left: 1em; margin-right: 1em;\"><img loading=\"lazy\" border=\"0\" src=\"http:\/\/4.bp.blogspot.com\/-GbUbO0yZwGs\/T_MOXc4RdhI\/AAAAAAAACkU\/tIW51gHBOCM\/s400\/auto-complete-clang-1.png\" height=\"173\" width=\"400\" \/><\/a><\/div>\n<p>You can get the autocomplete package at <a href=\"https:\/\/github.com\/m2ym\/auto-complete\">github<\/a> and the clang extension is available also on many repositories on github I used the one by <a href=\"https:\/\/github.com\/brianjcj\/auto-complete-clang.git\">brianjcj<\/a>. Under OS X I use clang 3.1, under Linux I use clang 3.0. You may have to point autocomplete to the correct binary via the customizable variable&nbsp;<span style=\"font-family: 'Courier New',Courier,monospace;\">ac-clang-executable<\/span>.<\/p>\n<p>So basically you have to clone the autocomplete package, clone the clang extension and copy the <span style=\"font-family: 'Courier New',Courier,monospace;\">auto-complete-clang.el<\/span> file into the same folder. I used <span style=\"font-family: 'Courier New',Courier,monospace;\">~\/bin\/emacs\/auto-complete<\/span>, but you can choose any directory. Maybe you have your own site-lisp folder.<\/p>\n<pre>(defcustom mycustom-system-include-paths '(\".\/include\/\" \"\/opt\/local\/include\" \"\/usr\/include\" )<br \/>  \"This is a list of include paths that are used by the clang auto completion.\"<br \/>  :group 'mycustom<br \/>  :type '(repeat directory)<br \/>  )<br \/><br \/>(add-to-list 'load-path \"~\/bin\/emacs\/auto-complete\")<br \/>(require 'auto-complete-config)<br \/>(add-to-list 'ac-dictionary-directories \"~\/bin\/emacs\/auto-complete\/ac-dict\")<br \/>(ac-config-default)<br \/>(require 'auto-complete-clang)<br \/>(setq clang-completion-suppress-error 't)<br \/>(setq ac-clang-flags<br \/>      (mapcar (lambda (item)(concat \"-I\" item))<br \/>              (append <br \/>               mycustom-system-include-paths<br \/>               )<br \/>              )<br \/>      )<br \/><br \/>(defun my-ac-clang-mode-common-hook()<br \/>  (define-key c-mode-base-map (kbd \"M-\/\") 'ac-complete-clang)<br \/>)<br \/><br \/>(add-hook 'c-mode-common-hook 'my-ac-clang-mode-common-hook)<br \/><\/pre>\n<p>The key binding to <span style=\"font-family: 'Courier New',Courier,monospace;\">M-\/<\/span> is useful on US-keyboard, you may change that to your liking. So to start autocompletion, just type something and hit <span style=\"font-family: 'Courier New',Courier,monospace;\">M-\/<\/span>. Clang will try to parse your file up to point and give useful completions. If it cannot parse your source, the cursor at point will turn red, and the minibuffer will show the error message. Most likely you forgot to add some include paths to <span style=\"font-family: 'Courier New',Courier,monospace;\">mycustom-system-include-paths<\/span>, or you really do have a syntax error.<\/p>\n<p>The customizable variable <span style=\"font-family: 'Courier New',Courier,monospace;\">mycustom-system-include-paths<\/span> can be named arbitrarily. Many thanks to the helpful people over at stackoverflow for <a href=\"http:\/\/stackoverflow.com\/questions\/11276035\/how-to-customize-a-list-of-directories-in-emacs\">supplying me with the answer<\/a> how to make a directory list nicely customizable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Update: I have written a new blog post about this, with updated information and using new and current packages. Autocompletion for C++ in Emacs is a hot topic &#8212; at least for Emacs users. At least for Emacs users who code C++&#8230; So here is my setup that I use, and which works reasonably well. &hellip; <a href=\"https:\/\/www.root42.de\/blog\/?p=51\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;A nice C++ autocomplete configuration for Emacs&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[38,39,7,6,8],"tags":[],"_links":{"self":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/51"}],"collection":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=51"}],"version-history":[{"count":0,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=\/wp\/v2\/posts\/51\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=51"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=51"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.root42.de\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=51"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}