If you ever wrote header files that have to be used by both C compiler and C++ compiler, you'd encounter this particular ifdef. This construct causes one to insert extra curly braces in the header file, which later makes Emacs attempt to indent everything one level deeper to the right.

Is there any way to make Emacs recognize this ifdef and ignore the indenting created by it?

Here's an example:

#ifdef __cplusplus
extern "C" {

/* Emacs will try to indent everything here one level to the right */

#ifdef __cplusplus

1 Answer 1


Set the inextern-lang offset to 0 in c-offsets-alist for your indentation style.

Here's some code adapted from my own init file that creates a C style called wvxvw and defines it to have 4-space indents, indented case statements inside switch blocks, non-indented labels, and non-indented extern blocks (the other settings are just to illustrate some of the possibilities).

(defun cc-style-make-my-wvxvw ()
  (let ((entry (assoc "wvxvw" c-style-alist)))
    (unless entry
      (setq entry (cons "wvxvw" nil))
      (setq c-style-alist (cons entry c-style-alist)))
    (setcdr entry '((c-basic-offset . 4)
                     (label . 0)
                     (case-label . +)
                     (inextern-lang . 0)
(eval-after-load "cc-styles" '(cc-style-make-wvxvw))

The obvious follow-up is how to get this style to apply automatically. There are multiple solutions; what I do is, at the top of the directory where I want to use this style, I use the following .dir-locals.el:

  (c-file-style . "wvxvw")))

If other people working on the same code use Emacs, you may want to share these definitions. You can put any string in c-file-style in a file or directory local variable declaration, but it's annoying for people who don't have the style name defined. Unfortunately, you can't define a style or tweak offsets through file-local variables. So if you want to share the style, you need all Emacs users to copy the style definition to (a file loaded by) their own init file.

  • This is really awesome!
    – wvxvw
    Aug 30, 2018 at 2:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.