Document directory
- References:
- Copyright description:
- Edit Environment Configuration
- Auto-completion
-->
Copyright description and references for C/C ++ programming in Emacs
By convention, I wrote articles with copyright instructions and references at the beginning.
References:
- Hhuu @ newsmth's daily life of Emacs
- Emacs documentation
- Emacs plug-in documentation
Copyright description:
Reprinted, please refer to the personal homepage of caole
Www.caole.net. please ensure that the full text is reprinted, especially this part cannot be omitted.
Collation
It has been five years since I used Emacs to write programs. I deeply realized the strength of Emacs. There are three types of programmers: VI, emacs, and others. It may be exaggerated, but it also shows the role of Emacs in programmers.
The biggest problem with Emacs is the high entry threshold. It looks very different from the IDE that most people think. Many people think it is a notepad (or a very difficult notepad) at the first sight of Emacs ), A little better, I often think that Emacs is just an ultraeditor. It's really a storm.
I am a lazy and don't like to remember too many shortcut keys. I believe many people are the same as me. As I can see later, except Common commands are shortcut keys, most of the other commands are executed using M-X or click the menu with the mouse. This is just a matter of personal style.
My basic programming environment is:
- Debian GNU/Linux Sid Operating System
- Gnome 2.10.0 desktop environment
- Gun Emacs 23.0.0.1 for Debian
- Use GNU tool chains (GCC, make, GDB, etc)
The subsequent descriptions are based on the above environment. In addition, this article focuses on C/C ++ program development and is also applicable to other languages. In terms of difficulty, This article focuses on beginners.
This article will certainly have many errors. please correct me
Thank you.
Basic Process
Basically, writing a C ++ program involves the following steps:
- Edit code
- Write makefile
- Compile the code and modify the compilation error.
- Debug code and modify logic errors
Of course, you often need to read other people's code.
Based on the above steps, This article focuses on the following aspects:
- Configure Emacs to create a convenient code editing environment and makefile compiling environment.
- Compile the code in Emacs and modify the compilation error.
- Debug the program with GDB in Emacs.
- Use cssag and ECB to read the code in Emacs.
Basic Environment Settings Edit Environment Configuration
To write c ++ programs, you must use the CC-mode plug-in. CC-mode originally supports C language, but now supports many languages, such
C ++, Java, objective-C, CORBA, awk, Pike, and so on. CC-mode is the standard plug-in of GNU-Emacs. If your requirements are not high
The configuration may be satisfied. Various CC-mode behaviors can be customized freely. For more information, see the CC-mode reference document.
Here is the section about CC-mode configuration in my. emacs file, for your reference only:
; CC-mode configure http://cc-mode.sourceforge.net/(require 'CC-mode) (C-set-offset 'inline-open 0) (C-set-offset 'friend '-) (C-set-offset 'substatement-open 0)
; My C/C ++ language editing Policy (defun my-C-mode-common-hook () (setq tab-width 4 indent-Tabs-mode nil); Hungry-delete and auto-newline (c-toggle-auto-hungry-state 1 );; key definition (define-key C-mode-base-map [(control/')] 'hs-toggle-hiding) (define-key C-mode-base-map [(return)] 'newline-and-indent) (define-key C-mode-base-map [(F7)] 'compile) (define-key C-mode-base-map [(META/')] 'C-indent-command); (define-Key C-mode-base-map [(Tab)] 'hippie-expand) (define-key C-mode-base-map [(Tab)] 'My-indent-or-complete) (define-key C-mode-base-map [(meta? /)] 'Semantic-Ia-complete-symbol-menu)
Note that the last two lines above are the shortcut keys automatically completed by the Code. I will mention code auto-completion later.
; Preprocessing settings (setq C-macro-shrink-window-flag t) (setq C-macro-Preprocessor "CPP") (setq C-macro-cppflags "") (setq C-macro-prompt-flag t) (setq HS-minor-mode t) (setq abbrev-mode t )) (add-hook 'C-mode-common-hook 'My-C-mode-common-hook );;;; my c ++ language editing Policy (defun my-C ++-mode-hook () (setq tab-width 4 indent-Tabs-mode nil) (C-set-style "stroustrup"); (define-key C ++-mode-map [F3] 'replace-Regexp ))
Auto-completion
I used hippie-expand for a long time. However, sometimes you may feel that this auto-completion is a bit silly, and you will often make up something unrelated, because hippie-expand is determined based on the words you have typed and kill-ring, it does not analyze the program syntax.
Therefore, you need to install a code analysis tool and add it to the extension policy of hippie-expand. We can use semantic. In fact, hippie-expand + semantic is the best choice I have found. If you have better options, please let me know :)
Semantic is cedet
A tool in, cedet is the abbreviation of collection of Emacs Development Environment tools. It contains several tools, which are quite good. Unfortunately, I only use two of them.
You can configure semantic in. emacs. The following is my. emacs configuration, which is for reference only:
Import cedet:
(load-file "~/lib/emacs-lisp/cedet-1.0pre3/common/cedet.el")
Configure the search range of semantic:
(setq semanticdb-project-roots (list (expand-file-name "/")))
Custom Auto-completion command. This part is copied from hhuu. If it is filled in the middle of the word, it is a tab.
(defun my-indent-or-complete () (interactive) (if (looking-at "//>") (hippie-expand nil) (indent-for-tab-command)) )(global-set-key [(control tab)] 'my-indent-or-complete)
The Automatic completion policy of hippie gives priority to the analysis result of the Senator:
(autoload 'senator-try-expand-semantic "senator")(setq hippie-expand-try-functions-list '(senator-try-expand-semantictry-expand-dabbrevtry-expand-dabbrev-visibletry-expand-dabbrev-all-bufferstry-expand-dabbrev-from-killtry-expand-listtry-expand-list-all-bufferstry-expand-line try-expand-line-all-buffers try-complete-file-name-partially try-complete-file-name try-expand-whole-kill ))
Note that the following two lines exist in the CC-mode configuration:
(define-key c-mode-base-map [(tab)] 'my-indent-or-complete) (define-key c-mode-base-map [(meta ?/)] 'semantic-ia-complete-symbol-menu)
In this way, we can call custom hippie completion in CC-mode. The shortcut key is tab.
In addition, I also bound the shortcut key "Alt +/" to the semantic-Ia-complete-symbol-menu command. This is the semantic command, which will pop up the completed menu based on the analysis results, effect display:
Another good tool in cedet is speedbar. You can use it to quickly switch between multiple files. In my. emacs configuration file, I associated the speedbar with F5:
(global-set-key [(f5)] 'speedbar)
In this way, you can use F5 to call up the speedbar. The effect is as follows:
But to be honest, I seldom use speedbar myself. I usually use Dired with bookmark :)
Compile and Debug Programs
Based on the above configuration, after writing the program and MAKEFILE file, you can compile it in the Emacs source code window by pressing F7. Because in the my-C-mode-common-hook () function, there is such a line:
(define-key c-mode-base-map [(f7)] 'compile)
By default, the compile command of Emacs calls make-K and I changed it to make. You can also change it to another one, such as GCC. Just change "make.
'(compile-command "make")
Emacs divides a pane to display the compiled message. After compilation, emacs automatically associates the compiler output with the program to tell you the program in the first line has a problem. Press enter on the wrong row number to jump to the corresponding row of the corresponding file. In fact, I usually use the middle mouse to click the wrong line number :)
After the compilation error is completed, it is necessary to fight against the logical error. In fact, for a simple program, printing intermediate results to the terminal is the easiest and easy-to-use debugging method. However, a slightly complicated program will get dizzy, in this case, we need to use GDB to track the program process.
You can use the following command to start GDB.
M-x gdb
Generally, I like to enter the GDB-embedded-Windows mode, which divides a frame into five panes and displays the gdb command window, current local variables, and program text, call Stack and breakpoint.
The GDB command is not mentioned here, and its documentation is almost everywhere. Emacs binds gdb commands and shortcut keys. For Common commands, it is easier to enter shortcut keys. For example, c-N is next line, and c-S is step in. In fact, the most used shortcut keys are these two.
Below is my GDB:
Read code
There are usually three tools to read code in Emacs. The simplest tool is etags. the most complicated tool is ECB (Emacs code browser), which is located in the center of csflood.
Etags is the same as ctags, except that the former is used for Emacs and the latter is used for VI. I personally think that the etags feature is not enough. Of course, it may be that I am not using it well :) welcome to the guidance of Daniel.
Before using tags, you must first create a tags file for source code analysis and run: etags-R in the directory where the code is located.
I usually use these commands and shortcut keys:
M-x visit-tags-Table <RET> file <RET> select the tags file M -. [tag] <RET> access label M-* returns c-u m -. find the next definition of a tag
ECB is said to be powerful, but it is too complicated. Who can teach me? :) The following is a ECB.
Csflood is a suitable tool for me. It is actually an independent software and can be used completely out of VI and Emacs. However, with the powerful features of Emacs, cssag is
More convenient. By default, GNU Emacs supports cscope. Before using the SDK, cssag also needs to index the code. You can do this in Emacs:
C-c s a sets the initialization Directory, which is generally the root directory of your code C-S I.
After creating an index, you can use csflood to wander around the code. Common commands are as follows:
C-c s order to find the symbol C-c s g to find a global definition C-c s c to see which functions call the specified function C-c s c to see if the specified function is called which functions C-C s e look for the regular expression C-C s f look for the file C-c s I to see which files are included in the specified file?
In fact, I often don't remember the above shortcut keys. It doesn't matter. Look up at the top menu bar, and there is a column of csflood. These commands all contain :)
Paste a cscope:
Finished. I hope this article will be useful to you. If you have any questions or suggestions, contact me.
Contact.
Updated: 2006-12-26 |
Index |
|