Search code examples
c++emacscode-completion

Emacs company mode doesn't support auto completion for c++ STL functions


I want emacs to autocomplete std functions such as push_back of vector

#include <vector>

using namespace std;

int main() {
  std::vector v;
  v.push_back(3);
}

However, company mode seems don't support this, when I type v. and hit , push_back and other vector functions not show up in the popup.

My company mode config is like below:

(require-package 'company)
(require-package 'irony)
(require-package 'company-irony)
(require-package 'company-irony-c-headers)
(require-package 'company-quickhelp)
(require 'company)
(require 'irony)
(require 'company-irony)
(require 'company-irony-c-headers)
(require 'company-quickhelp)

(add-hook 'after-init-hook 'global-company-mode)
(add-hook 'c++-mode-hook 'irony-mode)
(add-hook 'c-mode-hook 'irony-mode)
(add-hook 'objc-mode-hook 'irony-mode)
(add-hook 'irony-mode-hook 'irony-cdb-autosetup-compile-options)
(add-hook 'irony-mode-hook #'irony-eldoc)
(add-hook 'after-init-hook 'company-quickhelp-mode)

(setq company-backends (delete 'company-semantic company-backends))
(add-to-list 'company-backends '(company-irony-c-headers
                                 company-irony
                                 company-clang
                                 company-yasnippet))

BTW, My platform is Ubuntu 20.04 and I've installed clang and cmake via sudo apt-get


Solution

  • Assuming you've installed the irony-mode-server, try M-xirony-cdb-menu. If that shows no compilation database, there are a few options to tell irony where to look for includes (discussed in the documentation). Irony will provide completion based on sources found in the compilation database.

    Of those options, I use a .clang_complete file in my project directory. To complete for vector in your example, assuming you are using g++ version 9 (replace the '9' with your major version), the following simple .clang_complete file may be sufficient (add -I entries for additional include paths as necessary)

    .clang_complete

    -I/usr/include/c++/9
    -I/usr/include/x86_64-linux-gnu/c++/9