Emacs has a lot of options for C++ development.
Tags
Various tags database solutions exist, but those are old and sometimes hard to use. You can take a look at the EmacsWiki page for tag systems if you'd like to try them out. One of the major pros of a tags database is that it works entirely within Emacs, so you don't need to spin up a compilation/completion server to search the codebase.
Servers
The other major option for Emacs code completion is server-based solution. You do have to configure and run a server (this is usually done by the associated Emacs package), which is a drawback. Server-based solutions are hit-and-miss in a lot of languages, but there are some good ones out there. A nice modern completion package that I like is LSP, using the CLang LLVM server.
Code style
For coding style, I like to use the "linux" default style, but with two spaces:
;; Configure tabs to be two spaces in C/C++
(setq c-default-style "linux")
(setq c-basic-offset 2)
Since .h
files can be C++ files as well, I add those to my
auto-mode-alist
:
(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))
class
declaration, it
will activate c++-mode
. If not, it goes with the safe assumption and
picks c-mode
. You can force the C++ major mode by using an extension
like .hpp
or .hxx
.
You can also use clang-format
, part of the CLang tool suite, for
formatting code. It can be configured to format on save like this:
(require 'clang-format)
(setq clang-format-style "file")
(add-hook 'c++-mode-hook
(lambda ()
(add-hook 'before-save-hook
#'clang-format-buffer nil 'local)))
Syntax checking
I generally use flycheck
for on-the-fly syntax checking.
(add-hook 'c++-mode-hook 'flycheck-mode)