您的位置:首页 > 运维架构 > Linux

构建基本的Linux C 编程环境

2007-08-18 21:06 417 查看
本文基于debian-40r0(内核2.6.18)介绍一下如何在一个标准系统中构建Linux C的基本编程开发环境。
一个完整的开发环境主要包括四个部分:标准C库、头文件、工具链、编辑器、帮助文档,下面依次介绍。

标准C库glibc

glibc是gnu发布的libc库,即c运行库。glibc是linux系统中内核之上最底层的api,几乎其它任何的运行库都会倚赖于glibc。glibc除了封装linux操作系统所提供的系统服务外,它本身也提供了许多其它一些必要功能服务的实现,主要的如下:
(1)string,字符串处理
(2)signal,信号处理
(3)dlfcn,管理共享库的动态加载
(4)direct,文件目录操作
(5)elf,共享库的动态加载器,即interpreter
(6)iconv,不同字符集的编码转换
(7)inet,socket接口的实现
(8)intl,国际化,即gettext的实现
(9)io,基本IO操作
(10)linuxthreads,线程
(11)locale,本地化
(12)login,虚拟终端设备的管理,及系统的安全访问
(13)malloc,动态内存的分配与管理
(14)nis
(15)stdlib,其它基本功能
基于glibc库的重要性,已经装好的linux系统,基本上都会有glibc库。
可以通过如下命令查看系统目前已经安装的glibc库及相关套件


dpkg -l | grep 'GNU C Library'

头文件

许多新手选择自己定制软件包来安装linux,往往会忘记安装头文件等相关的软件包,导致编译时无法通过,报类似下面的错误:


error: stdio.h: No such file or directory

可以通过下面的命令安装头文件以及开发库


apt-get install libc6-dev

工具链

从理论上说编译一个程序依次需要下面几个工具:C预处理器-->词法分析器-->代码生成器-->优化器-->汇编程序-->链接器。linux下有两个软件包binutils、gcc包括了上面的所有工具。
1. binutils工具
Binutils 是一组很重要的开发工具,包括链接器(ld)、汇编器(as)、反汇编器(objdump)和其他用于目标文件和档案的工具(ar),也是gcc的依赖项。
可以通过下面的命令安装


apt-get install binutils

2. gcc(gnu collect compiler)
gcc完成了从"C预处理器"到"优化器"的工作,并提供了与编译器紧密相关的运行库的支持,如libgcc_s.so、libstdc++.so等。
其重要性不必再说,没它什么都干不了 ... ...


apt-get install gcc

3. make
跟gcc一样大名鼎鼎,也一样重要


apt-get install make

其实上面三项可以通过一个软件包全部安装了,命令是


apt-get install build-essential (此命令还会将g++等工具也安装进来)

4. auto工具
make相关工具,也非常有必要


apt-get install autoconf automake

5. 调试器gdb
又是一个必备工具


apt-get install gdb

6. 其他实用工具

针对源代码
indent C程序美化器
ctags 创建标签文件,供vi编辑器使用。可以加快检查源文件的速度
lint C程序检查器

针对可执行文件
dis 反汇编工具
ldd 打印文件所需的动态连接信息
nm 打印目标文件中的符号表
strings 查看签入二进制文件中字符串
sum 打印文件的校验和与程序块计数
size 打印可执行文件中的各个段
readelf 分析elf格式的可执行文件(包括在binutils中)
strip 去除调试信息和符号(包括在binutils中)

帮助调试工具
strace 打印可执行文件中的系统调用
ps 显示进程信息
file 检测文件格式

性能优化辅助工具
gprof
prof
time

编辑器

选择一个好的编辑器将会大大提高效率,vi是个不错的选择
如下是一个.vimrc文件的例子(从网上摘抄的 可以做个参考):


..

1  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
2  " Stuff I have decided I don't like
3  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
4  "set ignorecase -- turns out, I like case sensitivity
5  "set list " turns out, I don't like listchars -- show chars on end of line, whitespace, etc
6  "autocmd GUIEnter * :simalt ~x -- having it auto maximize the screen is annoying
7  "autocmd BufEnter * :lcd %:p:h -- switch to current dir (breaks some scripts)
8
9  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
10  " General
11  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
12  set nocompatible " get out of horrible vi-compatible mode
13  filetype on " detect the type of file
14  set history=1000 " How many lines of history to remember
15  set cf " enable error files and error jumping
16  set clipboard+=unnamed " turns out I do like is sharing windows clipboard
17  set ffs=dos,unix,mac " support all three, in this order
18  filetype plugin on " load filetype plugins
19  set viminfo+=! " make sure it can save viminfo
20  set isk+=_,$,@,%,#,- " none of these should be word dividers, so make them not be
21
22  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
23  " Theme/Colors
24  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
25  set background=dark " we are using a dark background
26  syntax on " syntax highlighting on
27  colorscheme metacosm " my theme
28
29  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
30  " Files/Backups
31  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
32  set backup " make backup file
33  set backupdir=$VIM/vimfiles/backup " where to put backup file
34  set directory=$VIM/vimfiles/temp " directory is the directory for temp file
35  set makeef=error.err " When using make, where should it dump the file
36
37  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
38  " Vim UI
39  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
40  set lsp=0 " space it out a little more (easier to read)
41  set wildmenu " turn on wild menu
42  set ruler " Always show current positions along the bottom
43  set cmdheight=2 " the command bar is 2 high
44  set number " turn on line numbers
45  set lz " do not redraw while running macros (much faster) (LazyRedraw)
46  set hid " you can change buffer without saving
47  set backspace=2 " make backspace work normal
48  set whichwrap+=<,>,h,l  " backspace and cursor keys wrap to
49  set mouse=a " use mouse everywhere
50  set shortmess=atI " shortens messages to avoid 'press a key' prompt
51  set report=0 " tell us when anything is changed via :...
52  set noerrorbells " don't make noise
53  " make the splitters between windows be blank
54  set fillchars=vert:/ ,stl:/ ,stlnc:/
55
56  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
57  " Visual Cues
58  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
59  set showmatch " show matching brackets
60  set mat=5 " how many tenths of a second to blink matching brackets for
61  set nohlsearch " do not highlight searched for phrases
62  set incsearch " BUT do highlight as you type you search phrase
63  set listchars=tab:/|/ ,trail:.,extends:>,precedes:<,eol:$ " what to show when I hit :set list
64  set lines=80 " 80 lines tall
65  set columns=160 " 160 cols wide
66  set so=10 " Keep 10 lines (top/bottom) for scope
67  set novisualbell " don't blink
68  set noerrorbells " no noises
69  set statusline=%F%m%r%h%w/ [FORMAT=%{&ff}]/ [TYPE=%Y]/ [ASCII=/%03.3b]/ [HEX=/%02.2B]/ [POS=%04l,%04v][%p%%]/ [LEN=%L]
70  set laststatus=2 " always show the status line
71
72  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
73  " Text Formatting/Layout
74  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
75  set fo=tcrqn " See Help (complex)
76  set ai " autoindent
77  set si " smartindent
78  set cindent " do c-style indenting
79  set tabstop=8 " tab spacing (settings below are just to unify it)
80  set softtabstop=8 " unify
81  set shiftwidth=8 " unify
82  set noexpandtab " real tabs please!
83  set nowrap " do not wrap lines
84  set smarttab " use tabs at the start of a line, spaces elsewhere
85
86  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
87  " Folding
88  "    Enable folding, but by default make it act like folding is off, because folding is annoying in anything but a few rare cases
89  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
90  set foldenable " Turn on folding
91  set foldmethod=indent " Make folding indent sensitive
92  set foldlevel=100 " Don't autofold anything (but I can still fold manually)
93  set foldopen-=search " don't open folds when you search into them
94  set foldopen-=undo " don't open folds when you undo stuff
95
96  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
97  " File Explorer
98  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
99  let g:explVertical=1 " should I split verticially
100  let g:explWinSize=35 " width of 35 pixels
101
102  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
103  " Win Manager
104  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
105  let g:winManagerWidth=35 " How wide should it be( pixels)
106  let g:winManagerWindowLayout = 'FileExplorer,TagsExplorer|BufExplorer' " What windows should it
107
108  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
109  " CTags
110  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
111  let Tlist_Ctags_Cmd = $VIM.'/ctags.exe' " Location of ctags
112  let Tlist_Sort_Type = "name" " order by
113  let Tlist_Use_Right_Window = 1 " split to the right side of the screen
114  let Tlist_Compart_Format = 1 " show small meny
115  let Tlist_Exist_OnlyWindow = 1 " if you are the last, kill yourself
116  let Tlist_File_Fold_Auto_Close = 0 " Do not close tags for other files
117  let Tlist_Enable_Fold_Column = 0 " Do not show folding tree
118
119  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
120  " Minibuf
121  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
122  let g:miniBufExplTabWrap = 1 " make tabs show complete (no broken on two lines)
123  let g:miniBufExplModSelTarget = 1
124
125  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
126  " Matchit
127  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
128  let b:match_ignorecase = 1
129
130  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
131  " Perl
132  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
133  let perl_extended_vars=1 " highlight advanced perl vars inside strings
134
135  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
136  " Custom Functions
137  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
138  " Select range, then hit :SuperRetab($width) - by p0g and FallingCow
139  function! SuperRetab(width) range
140          silent! exe a:firstline . ',' . a:lastline . 's//v%(^ *)@<= {'. a:width .'}//t/g'
141  endfunction
142
143  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
144  " Mappings
145  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
146  map <right> <ESC>:MBEbn<RETURN> " right arrow (normal mode) switches buffers  (excluding minibuf)
147  map <left> <ESC>:MBEbp<RETURN> " left arrow (normal mode) switches buffers (excluding minibuf)
148  map <up> <ESC>:Sex<RETURN><ESC><C-W><C-W> " up arrow (normal mode) brings up a file list
149  map <down> <ESC>:Tlist<RETURN> " down arrow  (normal mode) brings up the tag list
150  map <A-i> i <ESC>r " alt-i (normal mode) inserts a single char, and then switches back to normal
151  map <F2> <ESC>ggVG:call SuperRetab()<left>
152  map <F12> ggVGg? " encypt the file (toggle)
153
154  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
155  " Autocommands
156  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
157  autocmd BufEnter * :syntax sync fromstart " ensure every file does syntax highlighting (full)
158  au BufNewFile,BufRead *.asp :set ft=aspjscript " all my .asp files ARE jscript
159  au BufNewFile,BufRead *.tpl :set ft=html " all my .tpl files ARE html
160  au BufNewFile,BufRead *.hta :set ft=html " all my .tpl files ARE html
161
162  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
163  " Useful abbrevs
164  """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
165  iab xasp <%@language=jscript%><CR><%<CR><TAB><CR><BS>%><ESC><<O<TAB>
166  iab xdate <c-r>=strftime("%d/%m/%y %H:%M:%S")<cr>


给一个不错的vi中文帮助文档
http://vcd.gro.clinux.org/doc/

帮助文档

帮助文档非常有用,强烈建议大家装上

安装C/C++man帮助手册


apt-get install manpages-dev

之后就可以查看函式的原型、具体参数说明、include头文件说明等
例如: man 3 printf查看printf函数的介绍

其他文档

apt-get install binutils-doc gcc-doc glibc-doc
apt-get install cpp-doc stl-manual cpp-4.0-doc libstdc++6-4.0-doc

最后

浅浅的一篇文章
若能给大家一些帮助,则感到不胜欣慰!
欢迎批评指正.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: