atilaneves / cmake-ide

Use Emacs as a C/C++ IDE
BSD 3-Clause "New" or "Revised" License
715 stars 92 forks source link
autocomplete autocompletion clang cmake cmake-ide cpp cpp11 cpp14 cpp17 emacs emacs-lisp flycheck flycheck-extension goto-definition ide libclang melpa rtags

cmake-ide

Build Status Coverage Status Melpa Status

CppCon 2015 Lightning talk on cmake-ide:

CppCon 2015 Lightning talk on cmake-ide

Introduction

cmake-ide is a package to enable IDE-like features on Emacs for CMake projects. It also supports non-CMake projects as long as a compilation database is generated out-of-band.This includes autocompletion and on-the-fly syntax checking in Emacs for CMake projects with minimal configuration. It uses other packages to do its heavy lifting, in a combination of:

cmake-ide will set variables and call functions for the installed dependent packages.

It works by running CMake in Emacs in order to obtain the necessary compiler flags to pass to the other tools. Since all the dependencies are specified in the CMake scripts, there is no need to maintain a parallel dependency tracking system for Emacs. Just ask CMake.

Features

Build Pool Directories and Persistent Naming of Automatic Build Directories

cmake-ide can automatically create build directories for you -- either in the system's tmp-directory or under cmake-ide-build-pool-dir (if set). By default, all automatically created build directories (no matter where created) will have temporary and unique names, that will change with each new session and are thus not reusable. You can, however, by setting cmake-ide-build-pool-use-persistent-naming use a reproducible naming scheme that is based on the project's path and will not change as long as the project's path is the same. This way, you can reuse the build directory.

By using both cmake-ide-build-pool-dir and cmake-ide-build-pool-use-persistent-naming, you can fully do away with the need to configure a build directory per project with directory local variables (for example).

Non-CMake projects

Use .dir-locals.el to set the cmake-ide-project-dir and cmake-ide-build-dir variables (use absolute paths).

.dir-locals.el: ((nil . ((cmake-ide-build-dir . "/path/to/build/dir") (cmake-ide-project-dir . "/path/to/project/dir"))))

If a file called compile_commands.json exists in cmake-ide-build-dir, it will work just as well as for CMake projects. Bear (https://github.com/rizsotto/Bear) can be used to generate a compile_commands.json from a make command.

Installation

Install from MELPA or MELPA Stable with:

M-x package-install RET cmake-ide.

Usage

Add this to your .emacs / init.el:

(require 'rtags) ;; optional, must have rtags installed
(cmake-ide-setup)

If cmake-ide-flags-c or cmake-ide-flags-c++ are set, they will be added to ac-clang-flags and company-clang-arguments. These variables should be set. Particularly, they should contain the system include paths (e.g. '("-I/usr/include/c++/4.9.1" "..."). For a system with gcc, you can get this information by running gcc -v -xc++ /dev/null -fsyntax-only (it's the same prerequisite for auto-complete-clang to work, since that's how clang itself works).

And... that's it. It works by calling cmake and parsing the resulting JSON file with compiler flags. Set cmake-ide-build-dir to where your project is being built and you won't have to call CMake manually again (except for the first time to specify options). Best done with directory local variables.

Related Projects: