• Stars
    star
    196
  • Rank 198,553 (Top 4 %)
  • Language
    Emacs Lisp
  • Created about 12 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

cpputils-cmake.el v0.5.8

http://melpa.org/packages/cpputils-cmake-badge.svg http://stable.melpa.org/packages/cpputils-cmake-badge.svg

Perfect syntax check (Flymake) and code-completion if you use 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

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)

Usage

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

Quick start:

  • Step 1, The build directory should have the same name as value of cppcm-build-dirname. It is “build” by default. Make sure the build is located somewhere in the directory hierarchy from C++ file
  • Step 2, Go to build directory to run cmake and make at least once. The compile should be successful!

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

Demo (OPTIONAL)

Let’s start by creating a “hello world” C++ 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

flymake

https://cloud.githubusercontent.com/assets/184553/5368660/cbc0c70c-805c-11e4-8723-a2ae752a549d.png

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.

FAQ

Avoid scanning system header files

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

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` 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 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
company-c-headers-path-systemcompany-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:

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!

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:

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”.

Stop creating Makefiles for flymake

Insert below code into ~/.emacs:

(setq cppcm-write-flymake-makefile nil)

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/.

More Repositories

1

mastering-emacs-in-one-year-guide

Be great at emacs in one year
6,370
star
2

emacs.d

Fast and robust Emacs setup.
Emacs Lisp
2,411
star
3

find-file-in-project

Quick access to project files in Emacs
Emacs Lisp
428
star
4

evil-nerd-commenter

Comment/uncomment lines efficiently. Like Nerd Commenter in Vim
Emacs Lisp
390
star
5

elpa-mirror

Create local emacs package repository. 15 seconds to install 115 packages.
Emacs Lisp
315
star
6

evil-matchit

Vim matchit ported into Emacs
Emacs Lisp
287
star
7

counsel-etags

Fast, energy-saving, and powerful code navigation solution
Emacs Lisp
174
star
8

wucuo

Fastest solution to spell check camel case code or plain text
Emacs Lisp
121
star
9

eacl

eacl - Emacs auto complete lines by grepping project
Emacs Lisp
97
star
10

vc-msg

Show commit message of current line in Emacs
Emacs Lisp
79
star
11

cliphist

Paste from clipboard manager into Emacs
Emacs Lisp
75
star
12

js-comint

js-comint will send the code from Emacs into node.js or rhino
Emacs Lisp
71
star
13

company-ctags

Fastest Emacs auto-completion using Company and Ctags
Emacs Lisp
56
star
14

pyim-tsinghua-dict

用清华大学开放中文词库数据建立的pyim 输入法]词库. 已基于词频统计信息DF值(Document Frequency)优化
Python
48
star
15

mybigword

Use Zipf frequency of each word to extract English big words
Emacs Lisp
38
star
16

org2nikola

export org into html used by static blog generator like https://github.com/getnikola/nikola
Emacs Lisp
27
star
17

myelpa

Mirror of Emacs packages I'm using
23
star
18

vscode-setup

21
star
19

gmail2bbdb

convert gmail contacts to BBDB file, easy to use, robust, no dependency
Emacs Lisp
20
star
20

vscode-matchit

Jump between matching HTML tags and brackets smartly in VS Code. It's ported from Vim matchit by Benji Fisher
TypeScript
20
star
21

find-by-pinyin-dired

Find file by first Pinyin characters of Chinese Hanzi. 输入拼音首字母定位对应的中文目录/文件
Emacs Lisp
18
star
22

imenu-extra

Add extra items into lsp-mode/js2-mode imenu items
Emacs Lisp
15
star
23

redguardtoo.github.io

my blog
HTML
14
star
24

NinjaWebCoder

Use keyboard to copy code from stackoverflow
HTML
14
star
25

test-git-mergetool

toy project to test git mergetool
14
star
26

dianyou

Search/Analyze mails in Gnus
Emacs Lisp
12
star
27

pyim2fcitx

Conver pyim dictionary to fcitx dictionary
Python
12
star
28

lazyflymake

Lightweight syntax checker for Emacs, alternative of `flymake-mode'
Emacs Lisp
11
star
29

shellcop

Analyze errors reported in Emacs builtin shell
Emacs Lisp
10
star
30

evil-mark-replace

replace string in marked region effectively
Emacs Lisp
9
star
31

counsel-bbdb

Quick search&input email from BBDB based on ivy
Emacs Lisp
9
star
32

js2hl

Highlight/rename things using `js2-mode' parser
Emacs Lisp
8
star
33

shenshou

download subtitles from opensubtitles.org
Emacs Lisp
8
star
34

spell-check-code-in-ci

Free and powerful solution to spell check code at Continuous integration server
Makefile
7
star
35

fastdef

Insert terminology faster
Emacs Lisp
6
star
36

diff-lisp

Diff files&strings in pure Emacs Lisp.
Emacs Lisp
6
star
37

root-dotfiles

my dotfiles as root at Linux host
Shell
6
star
38

jqcuo

Powerful and simple solution to spell check code in command line
Emacs Lisp
6
star
39

jest-coverage-for-files-outside-of-root

jest coverage for files outside of root
JavaScript
5
star
40

find-and-ctags

Use `find' and `ctags' for code navigation
Emacs Lisp
5
star
41

portable-percol

portable percol (https://github.com/mooz/percol)
Python
5
star
42

zhfreq

processing chinese text using word frequency data
Emacs Lisp
4
star
43

my-emacs.d-snapshot

Emacs Lisp
4
star
44

AX88772C_Source

my usb to net adapter
C
4
star
45

test-git-blame

A project to test git blame
JavaScript
3
star
46

roblox-mode

Emacs major mode to edit Roblox Studio rbxlx files
Emacs Lisp
3
star
47

org-mime

org html export for text/html MIME emails
3
star
48

perl-recordmydesktop

perl-recordmydesktop.pl is the frontend for recordMyDesktop screencast tool
Prolog
3
star
49

counsel-ctags

Fast code navigation using Universal Ctags
Emacs Lisp
3
star
50

test-git-log

JavaScript
1
star
51

asus-pce-n53-11n-n600

ASUS PCE-N53 wireless patched driver and gentoo installation mini-guide
C
1
star
52

ms-frontend-sandbox

sandbox for interviewers
JavaScript
1
star
53

helloworld

hello world
C
1
star
54

wxwidgets-help

Look up wxWidgets API in Emacs by using local html manual
Emacs Lisp
1
star