randomphrase / company-c-headers

Auto-completion for C/C++ headers using Company
GNU General Public License v2.0
76 stars 9 forks source link

Company c headers doesn't work #20

Closed julzheng closed 5 years ago

julzheng commented 6 years ago

init.el

;; init.el --- Emacs configuration

;; INSTALL PACKAGES
;; ------------------------------

(require 'package)

(add-to-list 'package-archives
         '("melpa" . "http://melpa.org/packages/") t)

(add-to-list 'load-path "~/.emacs.d/lisp/")

(package-initialize)
(when (not package-archive-contents)
  (package-refresh-contents))

(defvar myPackages
  '(better-defaults
    elpy ;; add the elpy package (require
    flycheck ;; add the flycheck package
    material-theme
    py-autopep8)) ;;add the autopep8 package

(mapc #'(lambda (package)
      (unless (package-installed-p package)
        (package-install package)))
      myPackages)

;; BASIC CUSTOMIZATION
;; ------------------------------

(setq inhibit-startup-message t) ;; hide the startup message
(load-theme 'material t) ;; load material theme
(set-frame-font "DejaVu Sans Mono-10") ;; font type
;; (set-face-attribute 'default nil :height 100) ;; font size
(global-linum-mode t) ;; enable line numbers globally
(setq linum-format "%2d \u2502") ;; padding for line numbers and solid separator line

;; Others
;; --------------------------------------
(global-set-key [f8] 'neotree-toggle)

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(go-add-tags-style (quote lower-camel-case))
 '(package-selected-packages
   (quote
    (irony company-irony-c-headers company-c-headers flycheck-irony magit popwin go-guru go-direx go-add-tags go-eldoc windata tree-mode py-autopep8 neotree material-theme lua-mode jedi irony-eldoc go-mode go-autocomplete flycheck elpy company-irony better-defaults all-the-icons alchemist)))
 '(tab-width 4))

(defun my-indent-region (N)
  (interactive "p")
  (if (use-region-p)
      (progn (indent-rigidly (region-beginning) (region-end) (* N 4))
             (setq deactivate-mark nil))
    (self-insert-command N)))

(defun my-unindent-region (N)
  (interactive "p")
  (if (use-region-p)
      (progn (indent-rigidly (region-beginning) (region-end) (* N -4))
             (setq deactivate-mark nil))
    (self-insert-command N)))

(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
;;(setq-default c-basic-offset 4)

(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

(defconst user-init-dir
  (cond ((boundp 'user-emacs-directory)
         user-emacs-directory)
        ((boundp 'user-init-directory)
         user-init-directory)
        (t "~/.emacs.d/")))

(defun load-user-file (file)
  (interactive "f")
  "Load a file in current user's configuration directory"
  (load-file (expand-file-name file user-init-dir)))

;; Global setup

(require 'yasnippet)
(yas-global-mode 1)

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

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

(load-user-file "python.el")
(load-user-file "elixir.el")
(load-user-file "keys.el")
(load-user-file "go.el")
(load-user-file "cpp.el")

cpp.el

;; C++ CONFIGURATION
;; ---------------------------------------

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

(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)

(eval-after-load 'company
  '(add-to-list
    'company-backends '(company-irony company-c-headers)))

(add-to-list 'company-c-headers-path-system "/usr/include/c++/5/")

(eval-after-load 'flycheck
  '(add-hook 'flycheck-mode-hook #'flycheck-irony-setup))

(defun my-c++-mode-hook ()
  (setq c-basic-offset 4)
  (c-set-offset 'substatement-open 0))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)

So this is my current setup, but it doesn't work. any ideas?

randomphrase commented 6 years ago

probaby company-irony is interfering, try disabling that

tianhongw commented 6 years ago

I met the same problem,and i solve it by adding company-c-headers before company-irony. Modify your cpp.el (eval-after-load 'company '(add-to-list 'company-backends '(company-c-headers company-irony))) This works for me.