转载自   http://condor.depaul.edu/glancast/374class/docs/csapp_compile_guide.html

Compiling with the CSAPP library


The csapp collection of useful auxilliary functions are declared in the file csapp.h and defined in the csapp.c file.

These functions include the utility functions for Unix file i/o, sockets, signals, threads and semaphores.

The threads (and semphores) functions require linking with libraries other than the standard C library.

The threads functions require the pthread library.

On some systems (not ctilinux1), the semaphores also require the rt (run time) library.

Example 1

Goal: Compile a program sample.c that includes "csapp.h"

Assumptions: csapp.h and csapp.c are in the same directory as sample.c

      gcc -O2 -lpthread -o sample sample.c csapp.c
    

On systems that also need to explicitly use the run time library, the command would just add another -l option:

      gcc -O2 -lpthread -lrt -o sample sample.c csapp.c
    

Example 2

Goal: Same as Example 1, but avoid recompiling csapp.c every time.

In Example 1, the file csapp.c is recompiled every time you compile sample.c or any program that uses the csapp lib even though csapp.c doesn't change.

So you could compile (but not link) csapp.c just once to get an object file csapp.o:

      gcc -O2 -c csapp.c
    

Then to compile sample.c

      gcc -O2 -lpthread -o sample sample.c csapp.o
    

Example 3

Goal: Same as Example 2, but avoid having to type so much.

To reduce the amount of typing required, we could create a make file. The make command looks for files named makefile or Makefile.

We want the make command to read the makefile and automatically create the executable file sample from the files it depends on: sample.c and csapp.o.

But csapp.o depends on csapp.c.

So the makefile can have 2 targets: sample and csapp.o.

For each target we need to provide the command(s) to create the target. Each command line should begin with a tab!

      sample: sample.c csapp.o
gcc -O2 -lpthread -o sample sample.c csapp.o csapp.o: csapp.c
gcc -O2 -c csapp.c

To build sample all we need to type is:

      make
    

Example 4

Goal: Compile any one of a fixed set of files that uses the csapp library without having to type too much.

Now suppose we have 3 separate programs: sample1.c, sample2.c, and sample3.c that use the csapp library.

We could write a makefile that has a target and rule for each one:

      sample1: sample1.c csapp.o
gcc -O2 -lpthread -o sample1 sample1.c csapp.o sample2: sample2.c csapp.o
gcc -O2 -lpthread -o sample2 sample2.c csapp.o sample3: sample3.c csapp.o
gcc -O2 -lpthread -o sample3 sample3.c csapp.o csapp.o: csapp.c
gcc -O2 -c csapp.c

To compile, say sample2, just type:

      make sample2
    

Example 5

Goal: Compile any file that uses the csapp library without having to modify the makefile.

Instead of having to write an entry for each of the 3 programs in Example 4, we can also write a "rule" in the makefile that handles all 3 cases (and even an named xxx.c that uses csapp if it is added later):

      .c:
gcc -O2 -lpthread -o $* $< csapp.o csapp.o: csapp.c
gcc -O2 -c csapp.c

With this makefile, we can now compile and link any one of the .c files that uses csapp. For example to compile and link sample2.c:

      make sample2
    

If csapp.c has not yet been compiled, this make command will automatically execute two commands:

      gcc -O2 -c csapp.c
gcc -O2 -lpthread -o sample2 sample2.c csapp.o

Now sample2 can be executed and csapp.c has been also been compiled to produce csapp.o for further use.

So if we now want to compile sample3, we again just type

      make sample3
    

Since csapp.o exists and is "up to date", only one command will be executed (automatically):

      gcc -O2 -lpthread -o sample3 sample3.c csapp.o
    

Example 6

Goal: Same as Example 5, but also using only one copy of csapp.h and csapp.c that are located in one fixed separate directory.

All the preceding examples assume the csapp.h and csapp.c files are in the same directory as the file that will use them.

There is no reason to keep making copies of these files if they are not changing.

So suppose these files are in the subdirectory, say cslib, of your login directory: ~/cslib. That is, suppose the paths to the two files csapp.h and csapp.c are:

    ~/cslib/csapp.h
~/cslib/csapp.c

In some other directory, say ~/examples/threads in your account you have files sample1.c, sample2.c, etc. that will use the csapp library.

There is no need to copy the csapp.h or csapp.c files to the ~/examples/threads directory.

Instead, create a make file (named makefile) in the ~/examples/threads directory:

      .c:
cd ~/cslib; make csapp.o
gcc -O2 -I ~/cslib -lpthread -o $* $<

This makefile has 2 commands to make the target.

The first command (temporarily) changes to the cslib directory and makes the csapp.o. Note that if csapp.o already exists, it will not be recompiled.

The second command (gcc ...) has an -I ~/cslib option. This tells the compiler to look in the ~/cslib directory for include files (csapp.h). It will also look there for any missing object files (csapp.o).

csapp 深入理解计算机系统 csapp.h csapp.c文件配置的更多相关文章

  1. CSAPP深入理解计算机系统(第二版)第三章家庭作业答案

    <深入理解计算机系统(第二版)>CSAPP 第三章 家庭作业 这一章介绍了AT&T的汇编指令 比较重要 本人完成了<深入理解计算机系统(第二版)>(以下简称CSAPP) ...

  2. 《深入理解计算机系统》(CSAPP)读书笔记 —— 第一章 计算机系统漫游

    本章通过跟踪hello程序的生命周期来开始对计算机系统进行学习.一个源程序从它被程序员创建开始,到在系统上运行,输出简单的消息,然后终止.我们将沿着这个程序的生命周期,简要地介绍一些逐步出现的关键概念 ...

  3. 《深入理解计算机系统》实验一 —Data Lab

    本文是CSAPP第二章的配套实验,通过使用有限的运算符来实现正数,负数,浮点数的位级表示.通过完成这13个函数,可以使我们更好的理解计算机中数据的编码方式. 准备工作   首先去官网Lab Assig ...

  4. 深入理解计算机中的 csapp.h和csapp.c

    csapp.h其实就是一堆头文件的打包,在http://csapp.cs.cmu.edu/public/code.html 这里可以下载.这是<深入理解计算机系统>配套网站. 在头文件的# ...

  5. 《深入理解计算机系统》学习笔记整理(CSAPP 学习笔记)

    简介 本笔记目前已包含 CSAPP 中除第四章(处理器部分)外的其他各章节,但部分章节的笔记尚未整理完全.未整理完成的部分包括:ch3.ch11.ch12 的后面几小节:ch5 的大部分. 我在整理笔 ...

  6. CSAPP(深入理解计算机系统)读后感

    9月到10月8号,包括国庆七天,大概每天5小时以上的时间,把Computer System: A Programmer Perspective 2rd version(深入理解计算机系统)的英文版啃完 ...

  7. <深入理解计算机系统> CSAPP Tiny web 服务器

    本文是我学习<深入理解计算机系统>中网络编程部分的学习笔记. 1. Web基础       web客户端和服务器之间的交互使用的是一个基于文本的应用级协议HTTP(超文本传输协议).一个w ...

  8. 六星经典CSAPP笔记(1)计算机系统巡游

    CSAPP即<Computer System: A Programmer Perspective>的简称,中文名为<深入理解计算机系统>.相信很多程序员都拜读过,之前买的旧版没 ...

  9. CSAPP - Ch 1 - 计算机系统漫游

    目录 0 序言及摘要 1 信息就是位+上下文 2 程序被其他程序翻译成不同的格式 3 了解编译系统如何工作是大有益处的 0 序言及摘要 (1) 序言: CS:APP -- Computer Syste ...

随机推荐

  1. Redis单机数据库

    单机数据库 ·Redis服务器的所有数据库都保存在redisServer.db数组中,而数据库的数量则由redisServer.dbnum属性保存. ·客户端通过修改目标数据库指针,让它指向redis ...

  2. IOS UISwitch控件的基本使用

    * UISwitch继承自UIControl,因此也能像UIButton一样监听一些事件,比如状态改变事件* UISwitch可以通过拖线监听状态改变* UISwitch可以通过addTarget:. ...

  3. UVA Live Archive 4015 Cave (树形dp,分组背包)

    和Heroes Of Might And Magic 相似,题目的询问是dp的一个副产物. 距离是不好表示成状态的,但是可以换一个角度想,如果知道了从一个点向子树走k个结点的最短距离, 那么就可以回答 ...

  4. PAT (Basic Level) Practise (中文)- 1006. 换个格式输出整数 (15)

    http://www.patest.cn/contests/pat-b-practise/1006 让我们用字母B来表示“百”.字母S表示“十”,用“12...n”来表示个位数字n(<10),换 ...

  5. 非负随机变量X满足:(1-F(x)) 在 (0,+∞)积分为= E[X]

    机器学习作业的第一题最后一问卡住了,要证明 非负随机变量X满足 1 - F(X) 在 (0,+∞)上的积分是E(X);  关键的地方是积分换序,看原来的答案真的很难理解,画个图一下就懂了,码个链接,便 ...

  6. oracle一些常用的数据类型

    字符数据类型 char数据类型 当需要固定长度时,使用char数据类型,此数据类型长度可以使1-2000字节.若是不指定大小默认占1字节,如果长度有空余时会以空格进行填充,如果大于设定长度 数据库则会 ...

  7. Gym - 101981D Country Meow(模拟退火)

    题意 三维空间有\(n\)个点,找到另外一个点,离所有点的最大距离最小.求这个距离. 题解 \(1\).最小球覆盖,要找的点为球心. \(2\).模拟退火. 还是补一下模拟退火的介绍吧. 模拟退火有一 ...

  8. AngularJS 之1-初识

    摘要:本文主要记录第一次接触AngularJS的笔记,现在在我面前就是一张白纸+一点简单的html知识. 1.首先在<head>中加 <script src="一个网址(具 ...

  9. Eclipse主题更换方法

    1.打开Eclipse的Help->Eclipse Marketplace 2.在Find里搜索Eclipse Color Theme,点击Install按钮 3.打开Window->Pr ...

  10. Python虚拟机类机制之填充tp_dict(二)

    填充tp_dict 在Python虚拟机类机制之对象模型(一)这一章中,我们介绍了Python的内置类型type如果要完成到class对象的转变,有一个重要的步骤就是填充tp_dict对象,这是一个极 ...