现在的位置: 首页 > 综合 > 正文

最详细的Linux下C编程

2013年09月14日 ⁄ 综合 ⁄ 共 4284字 ⁄ 字号 评论关闭

 

gcc

目 录

1.      gcc 

1.      makefile写法 

2.      gcc_egcs使用 

3.      gdb使用 

4.      gcc常用选项对代码的影响 

1.      一般情况 

2.      -O 编译选项 

3.      -O2 编译选项 

4.      -fomit-frame-pointer 编译选项 

5.      -fomit-frame-pointer && -O2 

6.      -fPIC 编译选项 

7.      -static 编译选项 

5.      AT&T的汇编格式 

6.      x86内联汇编 

1.      简述 

2.      内联汇编 

3.      程序模板 

4.      操作数 

5.      修饰寄存器列表 

6.      操作数约束 

7.      示例 

1.      寄存器约束 

2.      匹配约束 

3.      内存操作数约束 

4.      修饰寄存器 

7.      不同的CPU下最佳编译参数 

8.      代码维护 

1.      简单cvs 

2.      automake 

3.      diff 

4.      rcs 

5.      内核重编译常见故障 

6.      cvs 

7.      共享库工具 

8.      代码优化 

9.      GNU 编码标准 

10.   书籍





gcc

 

 





makefile写法



作 者: 许明彦

Abstract:

    Unix 上写程式的人大概都碰过 Makefile,尤其是用 C 来开发程式的人。用 make来开发和编译程式的确很方便,可是要写出一个 Makefile就不简单了。偏偏介绍 Makefile 的文件不多,GNU Make 那份印出来要几百页的文件,光看完 Overview 就快阵亡了,难怪许多
人闻 Unix 色变。

    本文将介绍如何利用 GNU Autoconf Automake 这两套软体来协助我们『自动』产生 Makefile 档,并且让开发出来的软体可以像 Apache, MySQL 和常见的 GNU 软体一样,只要会 ``./configure'', ``make'', ``make install'' 就可以把程式安装到系统中。如果您有
心开发 Open Source 的软体,或只是想在 Unix 系统下写写程式。希望这份介绍文件能帮助您轻松地进入 Unix Programming 的殿堂。

1. 简介

    Makefile 基本上就是『目标』(target), 『关连』(dependencies) 和『动作』三者所组成的一连串规则。而 make 就会根据 Makefile 的规则来决定如何编译 (compile) 和连结 (link) 程式。实际上,make 可做的不只是编译和连结程式,例如 FreeBSD port collect
ion
中, Makefile 还可以做到自动下载原始程式套件,解压缩 (extract) ,修补 (patch),设定,然後编译,安装至系统中。

    Makefile 基本构造虽然简单,但是妥善运用这些规则就也可以变出许多不同的花招。却也因此,许多刚开始学习写 Makefile 时会感到没有规范可循,每个人写出来的 Makefile 长得都不太一样,不知道从何下手,而且常常会受限於自己的开发环境,只要环境变数不同或路
径改一下,可能Makefile 就得跟着修改。虽然有 GNU Makefile Conventions (GNU Makefile 惯例) 订出一些使用 GNU 程式设计时撰写 Makefile 的一些标准和规范,但是内容很长而且很复杂, 并且经常做些调整,为了减轻程式设计师维护 Makefile 的负担,因此有了Automake

    程式设计师只需写一些预先定义好的巨集 (macro),交给 Automake 处理後会产生一个可供Autoconf 使用的 Makefile.in 档。再配合利用Autoconf 产生的自动设定档 configure即可产生一份符合 GNU Makefile惯例的 Makeifle 了。

2. 上路之前

    在开始试着用 Automake 之前,请先确认你的系统已经安装以下的软体:
1. GNU Automake
2. GNU Autoconf
3. GNU m4
4. perl
5. GNU Libtool (
如果你需要产生 shared library)

    我会建议你最好也使用 GNU C/C++ 编译器 、GNU Make 以及其它 GNU 的工具程式来做为开发的环境,这些工具都是属於 Open Source Software不仅免费而且功能强大。如果你是使用Red Hat Linux 可以找到所有上述软体的 rpm 档,FreeBSD 也有现成的 package 可以直
接安装,或着你也可以自行下载这些软体的原始档回来 DIY。以下的范例是在 Red Hat Linux 5.2 + CLE2 的环境下所完成的。

3. 一个简单的例子

    Automake 所产生的 Makefile 除了可以做到程式的编译和连结,也已经把如何产生程式文件( manual page, info 档及 dvi ) 的动作,还有把原始程式包装起来以供散 的动作都考虑进去了,所以原始程式所存放的目录架构最好符合 GNU 的标准惯例,接下来我拿hello.c 来做为例子。

    在工作目录下建立一个新的子目录 ``devel'',再在 devel 下建立一个``hello'' 的子目录,这个目录将作为我们存放 hello 这个程式及其相关档案的地方:

% mkdir devel
% cd devel
% mkdir hello
% cd hello

用编辑器写个 hello.c 档,
#include stdio.h
int main(int argc, char** argv)
{
printf(``Hello, GNU! '');
return 0;
}

    接下来就要用 Autoconf Automake 来帮我们产生 Makefile 档了,

1. autoscan 产生一个 configure.in 的雏型,执行 autoscan 後会产生一个configure.scan 的档案,我们可以用它做为configure.in档的蓝本。

% autoscan
% ls
configure.scan hello.c

2. 编辑 configure.scan 档,如下所示,并且把它的档名改成configure.in
dnl Process this file with autoconf to produce a con figure script.
AC_INIT(hello.c)
AM_INIT_AUTOMAKE(hello, 1.0)
dnl Checks for programs.
AC_PROG_CC
dnl Checks for libraries.
dnl Checks for header files.
dnl Checks for typedefs, structures, and compiler ch aracteristics.
dnl Checks for library functions.
AC_OUTPUT(Makefile)

3. 执行 aclocal autoconf ,分别会产生 aclocal.m4 configure 两个档案
% aclocal
% autoconf
% ls
aclocal.m4 configure configure.in hello.c

4. 编辑 Makefile.am 档,内容如下
AUTOMAKE_OPTIONS= foreign
bin_PROGRAMS= hello
hello_SOURCES= hello.c

5. 执行 automake --add-missing Automake 会根据 Makefile.am 档产生一些档案,包含最重要的 Makefile.in
% automake --add-missing
automake: configure.in: installing `./install-sh'
automake: configure.in: installing `./mkinstalldirs'
automake: configure.in: installing `./missing'

6. 最後执行 ./configure
% ./configure
creating cache ./config.cache
checking for a BSD compatible install... /usr/bin/in stall -c
checking whether build environment is sane... yes
checking whether make sets ${MAKE}... yes
checking for working aclocal... found
checking for working autoconf... found
checking for working automake... found
checking for working autoheader... found
checking for working makeinfo... found
checking for gcc... gcc
checking whether the C compiler (gcc ) works... yes
checking whether the C compiler (gcc ) is a cross-co mpiler... no
checking whether we are using GNU C... yes
checking whether gcc accepts -g... yes
updating cache ./config.cache
creating ./config.status
creating Makefile

    现在你的目录下已经产生了一个 Makefile 档,下个 ``make'' 指令就可以开始编译 hello.c 成执行档,执行 ./hello GNU 打声招呼吧!

% make
gcc -DPACKAGE="hello" -DVERSION="1.0" -I. - I. -g -O2 -c he llo.c
gcc -g -O2 -o hello hello.o
% ./hello
Hello! GNU!

    你还可以试试 ``make clean''''make install''''make dist'' 看看会有什麽结果。你也可以把产生出来的 Makefile 秀给你的老板,让他从此对你刮目相看 :-)

4. 一探究竟

    上述产生 Makefile 的过程和以往自行编写的方式非常不一样,舍弃传统自行定义 make 的规则,使用 Automake 只需用到一些已经定义好的巨集即可。我们把巨集及目标 (target)写在 Makefile.am 档内,Automake读入 Makefile.am 档後会把这一串已经定义好的巨集展
开并且产生对应的Makefile.in 档, 然後再由

抱歉!评论已关闭.