Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell emacs to open .h file in C++ mode?

Tags:

emacs

People also ask

How do I open a .h file?

How to open an H file. Since header files are plain text files, you can open and view the file contents in a text editor. However, it's easier to edit *. h files and other source code files using a code editor like Microsoft Visual Studio or Apple Xcode.

What is a .h file for C?

Advertisements. A header file is a file with extension . h which contains C function declarations and macro definitions to be shared between several source files. There are two types of header files: the files that the programmer writes and the files that comes with your compiler.

How do I include a .h file in CPP?

You make the declarations in a header file, then use the #include directive in every . cpp file or other header file that requires that declaration. The #include directive inserts a copy of the header file directly into the . cpp file prior to compilation.

Can you include in .h files?

A header file should be included only when a forward declaration would not do the job. The header file should be so designed that the order of header file inclusion is not important. This is achieved by making sure that x.h is the first header file in x.


Try this:

(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))

Whenever you open .h files, C++-mode will be used.


Another approach for using both c-mode and c++-mode as appropriate, is to use directory local variables to set the mode.

Directory variables are evaluated after the mode has been set1, so you can actually write a .dir-locals.el file for your C++ project containing this:

((c-mode . ((mode . c++))))

And Emacs will change the mode to c++-mode whenever it had initially set it to c-mode.

If you work with a mix of C and C++ projects, this makes for a pretty trivial solution on a per-project basis.

Of course, if the majority of your projects are C++, you might set c++-mode as the default2, and you could then use this approach in reverse to switch to c-mode where appropriate.


1normal-mode calls (set-auto-mode) and (hack-local-variables) in that order. See also: How can I access directory-local variables in my major mode hooks?

2 To do so, add

(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))

to your .emacs file which open .h files in C++ mode by default.


If you don't want this to apply to every .h file, you can add the following to the bottom of your C++ header files.

// Local Variables:
// mode: c++
// End:

This will work for any Emacs variables that you want to set on a per file basis. Emacs ignores the leading characters, so use whatever comment characters are appropriate for the file type.


Apparently you can also put this at the top of the file:

// -*-c++-*-

to tell Emacs it's a C++ file.

I use this since I quite frequently end up on a vanilla Emacs and it works without configuring Emacs in any way.


Since I use both C and C++ regularly, I wrote this function to try and "guess" whether a .h file is meant to be C or C++

;; function decides whether .h file is C or C++ header, sets C++ by
;; default because there's more chance of there being a .h without a
;; .cc than a .h without a .c (ie. for C++ template files)
(defun c-c++-header ()
  "sets either c-mode or c++-mode, whichever is appropriate for
header"
  (interactive)
  (let ((c-file (concat (substring (buffer-file-name) 0 -1) "c")))
    (if (file-exists-p c-file)
        (c-mode)
      (c++-mode))))
(add-to-list 'auto-mode-alist '("\\.h\\'" . c-c++-header))

And if that doesn't work I set a key to toggle between C and C++ modes

;; and if that doesn't work, a function to toggle between c-mode and
;; c++-mode
(defun c-c++-toggle ()
  "toggles between c-mode and c++-mode"
  (interactive)
  (cond ((string= major-mode "c-mode")
         (c++-mode))
        ((string= major-mode "c++-mode")
         (c-mode))))

It's not perfect, there might be a better heuristic for deciding whether a header is C or C++ but it works for me.


I could swear I saw this question answered appropriately already? Weird.

You want this:

(add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode))