转载自   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.ogcc -O2 -lpthread -o sample sample.c csapp.ocsapp.o: csapp.cgcc -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.ogcc -O2 -lpthread -o sample1 sample1.c csapp.osample2: sample2.c csapp.ogcc -O2 -lpthread -o sample2 sample2.c csapp.osample3: sample3.c csapp.ogcc -O2 -lpthread -o sample3 sample3.c csapp.ocsapp.o: csapp.cgcc -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.ocsapp.o: csapp.cgcc -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.cgcc -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.ogcc -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).

转载于:https://www.cnblogs.com/li-daphne/p/5418330.html

csapp 深入理解计算机系统 csapp.h csapp.c文件配置相关推荐

  1. 【收藏】CSAPP深入理解计算机系统三万字长文解析

    CSAPP深入理解计算机系统 文章目录 CSAPP深入理解计算机系统 信息 信息存储 字长 字节序 位运算 位运算符 逻辑运算 移位运算 整数 补码规则 表示范围 整数运算 浮点数 编码 规格化数 阶 ...

  2. 《深入理解计算机系统》(CSAPP)读书笔记 —— 第三章 程序的机器级表示

    本章主要介绍了计算机中的机器代码--汇编语言.当我们使用高级语言(C.Java等)编程时,代码会屏蔽机器级的细节,我们无法了解到机器级的代码实现.既然有了高级语言,我们为什么还需要学习汇编语言呢?学习 ...

  3. csapp深入理解计算机系统实验

    文章目录 实验0 环境配置 虚拟机下载 ubuntu系统 下载gcc gdb 和 vim 下载csapp实验包 实验1 datalab-handout 实验0 环境配置 配置了一下午加一晚上的环境,遇 ...

  4. 《深入理解计算机系统》(CSAPP)实验三 —— Buf Lab

    这是CSAPP的第三个实验,主要让我们熟悉GDB的使用,理解程序栈帧的结构和缓冲区溢出的原理. 实验目的   本实验的目的在于加深对IA-32函数调用规则和栈结构的具体理解.实验的主要内容是对一个可执 ...

  5. 《深入理解计算机系统》(CSAPP)实验七 —— Malloc Lab

    文章目录 隐式空闲链表 分离的空闲链表 显示空闲链表 1. 实验目的 2. 背景知识 3. Implicit list mm_init extend_heap mm_malloc find_fit p ...

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

    前言 自己这段时间上了微机原理,想起来这本书也看完了,就一同综合做个笔记.因而有部分是只属于MIPS的,我会标注出来,如果不需要应付考试的话我是不推荐读里面相关段落的一个字的,而为应付考试的话标注属于 ...

  7. 《深入理解计算机系统》(CSAPP)读书笔记 —— 第二章 信息的表示和处理

    本章主要研究了计算机中无符号数,补码,浮点数的编码方式,通过研究数字的实际编码方式,我们能够了解计算机中不同类型的数据可表示的值的范围,不同算术运算的属性,可以知道计算机是如何处理数据溢出的.了解计算 ...

  8. 《深入理解计算机系统》(CSAPP)第二章——信息的表示和处理 知识点总结

    CASPP 第二章 信息的表示与处理 2.1 信息存储 2.1.1 字数据大小 2.1.2 寻址和字节顺序 2.1.3 布尔运算 2.1.4 位移运算 2.2 整数表示 2.2.1 整数类型数据 2. ...

  9. 《深入理解计算机系统》之浅析程序性能优化

    此文已由作者余笑天授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 本文主要是基于我之前学习<深入理解计算机系统>(以下简称CSAPP)这本书第五章优化程序性能内容 ...

最新文章

  1. int _tmain(int argc, _TCHAR* argv[])
  2. Http Message Converters with the Spring Framework--转载
  3. recyclerview item点击无效_Android列表布局专题(一)RecyclerView
  4. centos文件服务器软件,seafile服务器端的centos8的快速部署
  5. Java社区调查结果:74%的开发人员希望减少详细程度
  6. 计算机相关货品类别,工业计算机的种类以及产品供应链
  7. 为什么将表格的method改为post后就无法工作_用Python将Keras深度学习模型部署为Web应用程序...
  8. [导入]基于D3D Effect的引擎模式探讨。
  9. 数学边界(数学萌芽、初等数学、高等数学)
  10. linux+极点五笔输入法,Linux技巧之Ubuntu11.04下安装极点五笔输入法
  11. KISS保持简单:纪念丹尼斯·里奇
  12. thinkphp6事件监听event-listene
  13. 多人如何通过小程序上传照片、视频、素材、文章,然后点赞投票评选?
  14. Stata实验——计算个股“上海机场“的市场模型和CAPM模型的β值
  15. nginx php 设置时区,laravel5.8(二十)解决时区设置差8个小时解决办法
  16. 主流数据库书籍电子版下载汇总贴(84本)
  17. 关于el-dialog弹出层右上角叉号绑定取消按钮功能
  18. 智能合约vote部署
  19. 采坑道路4:vite报错(vue3.0+element-plus )由一个语言包引起的问题
  20. 响应式网站切图实操-姜威-专题视频课程

热门文章

  1. python学习之路-书籍推荐
  2. plsql developer 查看存储过程执行计划_产品简介 | X-Developer一站式研发效能管理平台...
  3. (44)FPGA面试题CMOS和TTL电路区别
  4. Vivado生成bit文件出现error解决
  5. 下标 获取字符_互联网人工智能编程语言Python的下标与切片详解
  6. 视觉检测无脊椎机器人或vipir_深入浅出人工智能前沿技术—机器视觉检测,看清人类智慧工业...
  7. 计算机一级考试复习资料,全国计算机一级考试复习资料
  8. python情感词典计算得分_基于情感词典的情感打分
  9. QCostomPlot 示例注解 2
  10. clickhouse mysql引擎_MySQL 最优秀的替代品系列之五:Clickhouse 视图的神奇用法