转载自   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. map 容器(copy)

    Map是c++的一个标准容器,她提供了很好一对一的关系,在一些程序中建立一个map可以起到事半功倍的效果,总结了一些map基本简单实用的操作!1. map最基本的构造函数:   map<stri ...

  2. IOS 监听slider滑动

    // 监听slider滑动 - (IBAction)valueChnage:(UISlider *)sender; @property (weak, nonatomic) IBOutlet NJVie ...

  3. POJ 2385 Apple Catching(01背包)

    01背包的基础上增加一个维度表示当前在的树的哪一边. #include<cstdio> #include<iostream> #include<string> #i ...

  4. 【51nod1443】路径和树(堆优化dijkstra乱搞)

    点此看题面 大致题意:给你一个无向联通图,要求你求出这张图中从u开始的权值和最小的最短路径树的权值之和. 什么是最短路径树? 从\(u\)开始到任意点的最短路径与在原图中相比不变. 题解 既然要求最短 ...

  5. flash + php对称密钥加密的交互

    这几天研究了下php和flash中的对称密钥加密的交互问题,经过研究以后决定,在项目中使用aes加密.问题也就来了,在flash中的加密数据如何与php的amf进行数据交互,最终决定使用base64编 ...

  6. 多线程中使用HttpContext.Current为null的解决办法

    HttpContext.Current.Server.MapPath(logFile)   这个是得到具体路径的方法  正常情况下是可以的 多线程情况下就为null 下边的代码原本的作用是把网站的异常 ...

  7. 问题004:如何在windows中打开命令行,有几种方法?

    第一种方法:按快捷键 Win+R (run),然后运行框中输入cmd. 第二种方法:开始菜单-->运行-->然后运行框中输入cmd. 第三种方法:在附件当中,找命令行选项即可.

  8. oracle 多行数据合并一行数据

    在工作中遇见的oracle知识,多行合并成一行,记录一下 1.取出需要的数据,代码: (SELECT to_char(m.f_meetdate, 'yyyy-MM-dd'), decode(nvl(m ...

  9. React报错 :browserHistory doesn't exist in react-router

    由于版本问题,React中history不可用 import { hashHistory } from 'react-router' 首先应该导入react-router-dom包: import { ...

  10. 项目实战8.1—tomcat企业级Web应用服务器配置与会话保持

    分类: Linux架构篇   tomcat企业级Web应用服务器配置与实战 环境背景:公司业务经过长期发展,有了很大突破,已经实现盈利,现公司要求加强技术架构应用功能和安全性以及开始向企业应用.移动A ...