redguardtoo / cpputils-cmake

Easy real time C++ syntax check and intellisense if you use CMake
196 stars 28 forks source link

[[http://melpa.org/#/cpputils-cmake][file:http://melpa.org/packages/cpputils-cmake-badge.svg]] [[http://stable.melpa.org/#/cpputils-cmake][file:http://stable.melpa.org/packages/cpputils-cmake-badge.svg]]

Perfect syntax check (Flymake) and code-completion if you use [[http://www.cmake.org][CMake]].

cpputils-cmake does all the dirty setup automatically for following plugins and commands,

Please note cpputils-cmake is independent on those plugins. Only CMake is required.

Tested at Emacs 23.4, 24.3, 24.4

The best way is using Emacs package manager.

** Manual way Download and place cpputils-cmake.el somewhere (say ~/.emacs/lisp), add below code into your .emacs:

+BEGIN_SRC lisp

(add-to-list 'load-path "~/.emacs.d/lisp/") (require 'cpputils-cmake)

+END_SRC

Quick start:

If your project don't use default path from CMake, check the section "Customize target path" in FAQ for the solution.

flycheck [[https://cloud.githubusercontent.com/assets/184553/5368798/ca25c044-805e-11e4-9859-805601784519.png]] M-x compile [[https://cloud.githubusercontent.com/assets/184553/5368693/43295ad4-805d-11e4-9125-f21209c8bd88.png]]

You can also M-x cppcm-compile to compile the current excutable only.

** gdb Press hotkey C-c C-g (I suppose you've copied my setup).

gud-gdb starts and the binary "~/your-projects-blah-blah/hello/build/main" is loaded automatically.

** auto-complete You also need install auto-complete-clang. [[https://cloud.githubusercontent.com/assets/184553/5369140/721c9648-8063-11e4-9ab4-75f0e8ea88c8.png]]

company-mode [[https://cloud.githubusercontent.com/assets/184553/5368579/c03fec24-805b-11e4-8fce-b611b2621318.png]] company-c-headers [[https://cloud.githubusercontent.com/assets/184553/5368489/e7b8ecf2-805a-11e4-8b06-818b96fb2049.png]]

** Open header file Press the hotkey C-x C-o or M-x ff-find-other-file. The corresponding header is opened.

You need install xsel under Linux to support OS clipboard.

This could be useful if you need access the executable's directory.

You can yank (paste) the full path to eshell or minibuffer and press "M-backspace" to get the directory name.

* Reload cpputils-cmake You can always M-x cppcm-reload-all any time in C/C++ files*.

There is also cppcm-reload-all-hook which will be called after cppcm-reload-all. This give you a chance to tweak or override the setup if and only if you are Emacs Lisp expert. Most users don't need tweak these global variables.

Here is the list of global variables third party plugins use: | variable name | plugin | |-----------------------------------------+---------------------| | ac-clang-flags | auto-complete-clang | | company-clang-arguments | company-mode | | cc-search-directories | ff-find-other-file | | flycheck-clang-include-path | flycheck | | semantic-dependency-system-include-path | semantic | | company-c-headers-path-system | company-c-headers |

** Compile only current target "M-x cppcm-compile".

Please press "C-h v cppcm-compile-list" for other compile options.

** Make clean && make "M-x cppcm-recompile"

** Customize target path For example, if the CMakeLists.txt contains something like this:

+begin_src cmake

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib) set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin)

+end_src

cpputils-cmake cannot find the target (executable or library).

It will then call the variable cppcm-get-executable-full-path-callback which could be a function object.

Here is a sample:

+begin_src elisp

(setq cppcm-get-executable-full-path-callback (lambda (path type tgt-name) ;; path is the supposed-to-be target's full path ;; type is either add_executabe or add_library ;; tgt-name is the target to built. The target's file extension is stripped (message "cppcm-get-executable-full-path-callback called => %s %s %s" path type tgt-name) (let* ((dir (file-name-directory path)) (file (file-name-nondirectory path))) (cond ((string= type "add_executable") (setq path (concat dir "bin/" file))) ;; for add_library (t (setq path (concat dir "lib/" file))) )) ;; return the new path (message "cppcm-get-executable-full-path-callback called => path=%s" path) path))

+end_src

You can insert above code into ~/.emacs!

** Macro and included cmake file cpputils-cmake assumes that CMakeLists.txt contains a rule to create executable.

The rule is either "add_executable" or "add_library".

Included file and macro in CMakeLists.txt will be ignored.

A sample CMakeLists.txt we can handle:

+BEGIN_SRC cmake

project(proj1) set(VAR2 "a2") set(VAR1 a1-${VAR2}) set(TGT hello-${PROJECT_NAME}-${VAR1}") add_executable(${TGT} ${SOURCES})

+END_SRC

The executable's name will be "hello-proj1-a1-a2".

** Stop creating Makefiles for flymake Insert below code into ~/.emacs:

+BEGIN_SRC elisp

(setq cppcm-write-flymake-makefile nil)

+END_SRC

Here is the steps to send bug report:

Besides, I still need general environment information like Emacs version and OS version.

Author: Chen Bin Keywords: flymake IntelliSense cmake

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see [[http://www.gnu.org/licenses/]].