Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Thiel/cpputils-cmake

Open more actions menu
 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

108 Commits
108 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cpputils-cmake.el (v0.4.22)

cpputils-cmake gives you perfect syntax check (Flymake) and IntelliSense/code-completion if you use CMake.

It does all the dirty setup automatically for following plugins and commands,

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

Install

Easy way

cpputils-cmake is available at https://github.com/milkypostman/melpa.

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:

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

Setup

Insert below code into your .emacs:

(add-hook 'c-mode-common-hook
          (lambda ()
            (if (derived-mode-p 'c-mode 'c++-mode)
                (cppcm-reload-all)
              )))
;; OPTIONAL, somebody reported that they can use this package with Fortran
(add-hook 'c90-mode-hook (lambda () (cppcm-reload-all)))
;; OPTIONAL, avoid typing full path when starting gdb
(global-set-key (kbd "C-c C-g")
 '(lambda ()(interactive) (gud-gdb (concat "gdb --fullname " (cppcm-get-exe-path-current-buffer)))))
;; OPTIONAL, some users need specify extra flags forwarded to compiler
(setq cppcm-extra-preprocss-flags-from-user '("-I/usr/src/linux/include" "-DNDEBUG"))

WARNING: 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:

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

The executable’s name will be “hello-proj1-a1-a2”.

One minute step by step tutorial

One line bash to create “hello” project

mkdir -p hello/src;printf "#include <stdio.h>\nint main(void) {\nprintf(\"hello world\");\nreturn 0;\n}" > hello/src/main.cpp;printf "cmake_minimum_required(VERSION 2.6)\nadd_executable(main main.cpp)" > hello/src/CMakeLists.txt

Use cmake as usual

Please note you need run “Make” at least once before using cpputils-cmake:

mkdir hello/build;cd hello/build;cmake ../src;make clean;make

If you use another directory name like “mybuild” instead of “build”, you must add following line into your .emacs:

(setq cppcm-build-dirname "mybuild")

Flymake

Open C++ file. Run “M-x flymake-mode”. Please note all commands are available ONLY when you editing/debugging C/C++ code!

Now typing some random C++ code and watch the real time hint on your syntax errors.

If you don’t want to use flymake, you can tell cpputils-cmake NOT to create Makefile for flyemake by inserting below code into ~/.emacs:

(setq cppcm-write-flymake-makefile nil)

Flycheck

See https://github.com/flycheck/flycheck for flycheck setup. No setup needed here.

Compile

Compile the program: `M-x compile`

The command line displayed in minibuffer is `make -C ~/your-projects-blah-blah/hello/build`

BTW, you can also `M-x cppcm-compile` to compile the current excutable only.

You can see the actual command displayed in minibuffer is `make -C ~/your-project-blah-blah/hello/build/sub-project-dir-if-your-are-editing-its-cpp-file`

Start gdb

Press hotkey `C-c C-g` (suppose you’ve copied my configuration from previous section).

You can see the gud-gdb starts and the executable “~/your-projects-blah-blah/hello/build/main” is loaded automatically.

Auto-complete & auto-complete-clang (OPTIONAL)

Use them as usual. You can see that the Intellisense/auto-complete is more precise.

Company-mode (OPTIONAL)

Use them as usual. You can see that the Intellisense/auto-complete is more precise.

Open header file

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

This is the default feature of Emacs. What cpputils-cmake does is to set up the directories of those header files for you automatically so that the header files could be found by Emacs.

Tips

Avoid scanning when opening system header files

cpputils-cmake scanning is light weight enough so below code is optional:

(add-hook 'c-mode-common-hook
          (lambda ()
            (if (derived-mode-p 'c-mode 'c++-mode)
                (if  (not (or (string-match "^/usr/local/include/.*" buffer-file-name)
                              (string-match "^/usr/src/linux/include/.*" buffer-file-name)))
                    (cppcm-reload-all))
              )))

Get executable’s full path of executable to build

The command “cppcm-get-exe-path-current-buffer” will copy current executable into kill ring AND OS clipboard.

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` at any time.

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.

Here is the list of global variables third party plugins will use (and you can tweak):

variable nameplugin
ac-clang-flagsauto-complete-clang
company-clang-argumentscompany-mode
cc-search-directoriesff-find-other-file
flycheck-clang-include-pathflycheck
semantic-dependency-system-include-pathsemantic

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”

Target output directory is not default value

For example, if the CMakeLists.txt contains something like this:

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)

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:

(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))

You can insert above code into ~/.emacs!

Credits

Bug Report

Check https://github.com/redguardtoo/cpputils-cmake.

Here is the steps to send bug report:

  • open cpp file in your real project
  • `M-x eval-expression`
  • paste (setq cppcm-debug t) into mini-buffer and press ENTER
  • `M-x cppcm-reload-all` and send me the output in Message buffer
  • `C-h v cppcm-hash` and send me the output
  • `M-x cppcm-version` and send the output

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

License

Copyright (C) 2012 Chen Bin

Author: Chen Bin <chenbin DOT sh AT gmail DOT com> 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/.

About

Easy real time C++ syntax check and intellisense if you use CMake

Resources

Stars

0 stars

Watchers

1 watching

Forks

Packages

 
 
 

Contributors

Languages

  • Emacs Lisp 95.6%
  • C++ 3.6%
  • Shell 0.8%
Morty Proxy This is a proxified and sanitized view of the page, visit original site.