C and C Complier Quickstart for Unix

    技术2022-05-11  53

    原文下载:http://plan9x.ys168.com- CandCComplierQuickstartforUnix.pdf

    Unix C/C++编译器快速指南

    (Mikel Stous 12/2/1998) (© 1998 University of Missouri-Kansas City)

    C编译器(.c文件)

    /usr/bin/cc

    /usr/local/bin/gcc

    /usr/local/bin/cc (和gcc相同)

    C++编译器(.c, .cc, .cxx文件)

    /usr/bin/cxx

    /usr/local/bin/g++

    /usr/local/bin/c++ (和g++相同)

    命令行参数

    -g -> 为debugger处理生成附加的符号表

    -I directory -> 指定目录以便于搜索头文件

    -L directory -> 指定目录以便于搜索库文件

    -l library -> 指定与当前编译代码链接的库

    -o outfile -> 命名object文件为outfile

    -O -> 使用对象代码优化器 (与-O2等价)

    -c infile.c -> 将infile.c编译为infile.o

    下面所示3条命令均为由源码文件infile.c开始并以可执行文件executable结束。

    1、gcc -c infile.c;gcc infile.o ;mv a.out outfile

    2、gcc infile.c ;mv a.out outfile

    3、gcc infile.c -o outfile

    路径:

    标准头文件 /usr/include #include <math.h>

    标准库文件 /usr/{lib,slib} gcc –o outfile infile.c -lm

    本地头文件 /usr/local/include #include <modsim/modsim.h>

    本地库文件 /usr/local/lib gcc –o outfile –lmodsim infile.c

    项目头文件 项目子目录 #include <myfile.h>

    项目库文件 项目子目录 gcc –Isubdir -c infile.c

    项目库文件 项目子目录 gcc –Lsubdir –llibrary –o outfile infile.c

    项目头文件 当前目录 #include “myfile.h”

    其他工具: (输入命令man获取详细帮助信息)

    make - 使用makefile,编译项目的强有力助手

    lint - C 语法检查器

    prof - profiler, 帮助程序员优化程序

    gdb - Gnu debugger, 允许程序员检测已编译的程序

    ddd - 图形化的debugger, 使用了gdb

    touch - 更新文件的访问修改日期

    emacs - 高度定制的文本编辑器,专业程序员必备

    indent - 格式化及缩进C程序

    a2ps - Anything-to-Postscript 转换工具, 生成格式化的文本文件

    rcs - 版本(修订)控制系统

    make 和 Makefiles: 工具make的目的就是快速的自动的检测出大型程序中那一块需要重编译,然后使用命令去重编译它们。make按照makefile提供的指令定位当前路径并工作。

    make 的命令行参数:

    -C directory -> 执行Makefile前先转至directory目录下

    -d -> 同时打印调试信息

    -f filename -> 使用filename替代makefile作为输入文件

    -n -> 测试模式: 只打印将要执行的

    target -> 只执行至target,否则执行全部

    makefile中,目标文件只当它的目标文件比依赖文件旧时才进行编译或重编译。命令touch可以用来更改makefile依赖文件的日期以达到重编译目标文件的目的。makefile的格式如下所示:

    # 注释

    变量名 = 文本或数字或$(变量名)

    target1 : target2 dependent_file

    <tab>用于创建目标文件的命令,可能需要包含 $(变量名)

    <tab>用于创建目标文件的命令,可能需要包含 $(变量名)

    target2 : dependent_file

    <tab>用于创建目标文件的命令,可能需要包含 $(变量名)

    <tab>用于创建目标文件的命令,可能需要包含 $(变量名)

    RCS: 版本(修订)控制系统,用于管理不同修订版的文件。RCS自动对修订版进行存储,检索,记录日志,标识以及合并。RCS对需要经常修订的文本十分有用,例如程序,文档,图片,论文或是信件。按下面的命令来体验一下RCS吧:

    vi infile.c 编辑文件,或是新建文件然后添加一些内容

    mkdir RCS 建立目录RCS

    ci infile.c checks in infile.c的当前版本

    co –l infile.c checks out infile.c的当前版本并锁住它

    vi infile.c 编辑文件并修改

    ci infile.c 检查infile.c的新版本

    rcsdiff infile.c 列出对infile.c所作的变更

    请阅读下面这些命令的帮助手册: rcsintro, ci, co, ident, rcs, rcsdiff, rcsmerge, rlog

    C 预处理指示符:

    #define #elif #else #endif #error #if

    #ifdef #ifndef #include #line #pragma #undef

    ANSI C 保留关键字:

    auto break case char const continue

    default do double else enum extern

    float for goto if int long

    register return short signed sizeof static

    struct switch typedef union unsigned void

    volatile while

    C 操作符:

    ! != % %= & && &= * *= + ++ +=

    - -- -= -> . / /= < << <= <<= =

    == > >= >> >>= ?: ^ ^= | |= || ~

    ; , ‘ “

    [] () {}

    C 运算符优先级: (从高到低)

    ( ) [ ] -> .

    ! ~ ++ -- - * & (type) sizeof

    * / %

    + -

    << >>

    < <= > >=

    == !=

    &

    ^

    |

    &&

    ||

    ?:

    = += -= *= /=

    ,

     

    最新回复(0)