Search code examples
c++ubuntuemacsclangclang-format

How do you use Clang-format on Emacs, Ubuntu?


I'm new to Ubuntu and Linux, so I'm sorry if this question seems to be too stupid. I hoped to have a function that can automatically format my C++ codes, so I installed clang-format by the following terminal command:

sudo aptitude install clang-format

To make it work with Emacs, I searched on the Internet for a solution and modified my "~/.emacs" file, adding the following line:

(setq clang-format-executable "/usr/bin/clang-format-6.0")

Now in Emacs, when I used the command M-x clang-format-buffer or similar commands on a C++ file, it succeeded, but the source code didn't seem to be formatted at all (sorry that I can't directly post images for some reason):

https://i.sstatic.net/gNIvn.png

https://i.sstatic.net/eKLXl.png

Is there anything else I'm missing in setting up clang-format, or what's the proper way to set it up?

I appreciate any help!


Solution

  • I got it.

    First, installing Clang-format this way was unnecessary. The proper way to do it was by using Emacs' package-install command.

    package-install clang-format
    

    Then I did this in the .emacs file:

    (load "/usr/share/emacs/site-lisp/clang-format-6.0/clang-format.el")
    

    Hope this helps anyone facing the same problem.