Setup C/GTK+ development environment with Emacs
Warning: be careful with my broken English, I got only about 450 in CET 6.
Table of Contents
1 Introduction
I'm a beginner at programming in C/GTK+ so I want to my development tools to give me more help like:
- auto complete function name / type name
- function parameter hint
- quick look API document
- jump to definition and back
- compile && run && edit or debug cycle
- and of course some basic editor feature e.g. syntax highlight, code style, search / replace
As you can see I actually should use an IDE for GTK+, but I do not find one even meets a half of my requirements. I have tried both Anjuta and geany, the former is a heavyweight IDE for development using GNOME techniques. It has a very handy tools to create classes but many features which I list above are missing or do not work out of box. The latter is a lightweight IDE, I think it maybe suitable for develop small project.
I do not know if that is possible GTK+/GNOME has an IDE like kdevelop and QT Create of QT/KDE?
For now, I have to use GNU Emacs to get start to develop my C/GTK+ project although I am not a Emacs master or even loyal fan.
2 General configuration
I fork emacs24-start-kit and customize to suit my need. It is a good starter for me because of literate way to embed code in document. I install many color themes and change everyday since I can not decide which one is my favorite.
I also taste many packages and find some of them are quiet useful:
- projectile, can manager project base on git repository and provide some handy functions like search within project via many external tools like ag or ack
- git-gutter, show current changes in frame's left in real time, so I do not need to use git diff to check what I have done
I am also using Helm, org-mode and maybe many not built-in package, they looks awesome but I do not know much about them.
3 C/GTK+ configuration
Emacs itself has many features with program mode, for example, code style, it has many rules not only indent as 4 space, setting up a suitable code style is necessary for editing and format code.
3.1 on-the fly Syntax check
I use clang as the syntax check back-end. To make it works, I add GTK+ include path to Flycheck Clang Include Path via "M-x customize".
3.2 Code complete
Auto-Complete (in short AC) is my choice for this task. AC is an intelligent auto-completion extension for Emacs1. It needs Source to complete the task, there are some built-in ones like words-in-buffer, dictionaries and TAGS etc.
auto-complete-clang is The AC sources for Clang. It is the most valuable source.
Here is my configurations according to related user manuals:
(require 'auto-complete-config) (ac-config-default) (require 'auto-complete-clang) (setq ac-quick-help-delay 0.5) (defun my-ac-config () (setq-default ac-sources '(ac-source-abbrev ac-source-dictionary ac-source-words-in-same-mode-buffers)) (add-hook 'emacs-lisp-mode-hook 'ac-emacs-lisp-mode-setup) (add-hook 'auto-complete-mode-hook 'ac-common-setup) (global-auto-complete-mode t)) (defun my-ac-cc-mode-setup () (setq ac-sources (append '(ac-source-clang ac-source-yasnippet) ac-sources))) (add-hook 'c-mode-common-hook 'my-ac-cc-mode-setup) (my-ac-config) (setq ac-clang-flags (mapcar (lambda (item)(concat "-I" item)) (split-string " /usr/lib/gcc/x86_64-redhat-linux/4.8.3/include /usr/local/include /usr/include /usr/include/gio-unix-2.0/ /usr/lib64/glib-2.0/include /usr/include/glib-2.0 ")))
3.3 Code browse
C-s*/*C-r and occur are quit easy and intimate way to move around within a source file. Helm has a search base directory named helm-do-grep which shows result when you are typing.
I use GNU global and its emacs front ggtags instead of cscope, no special reason for it, just since I'd like to try new things.
3.4 lookup GTK+ API
I always need to read the API document when I read / write code. After some search, I do find two solutions: gtklookup and devhelp-index, but both are not work for me.
So I write a small function to meet my needs partly:
(defun devhelp() "Search current symbol in devhelp" (interactive) (shell-command (format "devhelp -s %s" (thing-at-point 'symbol)))) (global-set-key [f1] 'devhelp)
At least I do not need copy-paste text between Emacs and devhelp. it is better if the API document (in html) can be shown within Emacs.