转载自   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. HDU 2191 悼念汶川地震(多重背包)

    思路: 多重背包转成01背包,怎么转?把一种大米看成一堆单个的物品,每件物品要么装入,要么不装.复杂度比01背包要大.时间复杂度为O(vns)(这里S是所有物品的数量s之和).这个做法太粗糙了,但就是 ...

  2. GetIPAddress——获得本地IP地址信息

    1.gethostname()需要初始化套接字库 加载#pragma comment(lib,"ws2_32.lib"),和WSAStartup(MAKEWORD(2,2),&am ...

  3. jQuery中常用的元素查找方法总结

    $("#myELement") 选择id值等于myElement的元素,id值不能重复在文档中只能有一个id值是myElement所以得到的是唯一的元素 $("div&q ...

  4. co-dialog弹出框组件-版本v2.0.0

    co-dialog theme 访问git:co-dialog 版本v2.0.0 主题2 coog.app('.theme2').use({ title: 'JUST CHECKING.', mess ...

  5. VS打包方法(安装和部署简介)——内含大量图片,密症慎入!

    友情提示:内含大量文字.图片,密集恐惧症者慎入! 主要记述一下利用微软集成开发环境VS打包的方法和详细步骤. 1.新建打包工程 打开VS,文件->添加项目->新建项目(如图1),在添加新项 ...

  6. hdu1863 畅通工程---MST&连通

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1863 题目大意: 中文题,求MST权值,如果不连通,输出? 解题思路: 这道题帮我找出了之前模板中的 ...

  7. python_52_函数返回值2

    def test1(x,y): print(x,y) test1(1,2)#位置参数调用,按顺序来,与形参一一对应 test1(y=1,x=2)#输出为2 1,不是1 2.关键字参数调用按关键字,不按 ...

  8. 【计数】cf938E. Max History

    发现有一种奇怪的方法不能快速预处理? 复习一下常见的凑组合数的套路 You are given an array a of length n. We define fa the following w ...

  9. SpringSecurity项目报错

    启动时,提示: Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory be ...

  10. 1 > 2 and 3 < 4 or 4 > 5 and 2 > 1 or 9 < 8

    a,b,c,d,e,f,g=1,2,3,4,5,8,9 m = a > b and c < d or c > e n = b > a or g < f x = m and ...