csapp 深入理解计算机系统 csapp.h csapp.c文件配置
转载自 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文件配置的更多相关文章
- CSAPP深入理解计算机系统(第二版)第三章家庭作业答案
<深入理解计算机系统(第二版)>CSAPP 第三章 家庭作业 这一章介绍了AT&T的汇编指令 比较重要 本人完成了<深入理解计算机系统(第二版)>(以下简称CSAPP) ...
- 《深入理解计算机系统》(CSAPP)读书笔记 —— 第一章 计算机系统漫游
本章通过跟踪hello程序的生命周期来开始对计算机系统进行学习.一个源程序从它被程序员创建开始,到在系统上运行,输出简单的消息,然后终止.我们将沿着这个程序的生命周期,简要地介绍一些逐步出现的关键概念 ...
- 《深入理解计算机系统》实验一 —Data Lab
本文是CSAPP第二章的配套实验,通过使用有限的运算符来实现正数,负数,浮点数的位级表示.通过完成这13个函数,可以使我们更好的理解计算机中数据的编码方式. 准备工作 首先去官网Lab Assig ...
- 深入理解计算机中的 csapp.h和csapp.c
csapp.h其实就是一堆头文件的打包,在http://csapp.cs.cmu.edu/public/code.html 这里可以下载.这是<深入理解计算机系统>配套网站. 在头文件的# ...
- 《深入理解计算机系统》学习笔记整理(CSAPP 学习笔记)
简介 本笔记目前已包含 CSAPP 中除第四章(处理器部分)外的其他各章节,但部分章节的笔记尚未整理完全.未整理完成的部分包括:ch3.ch11.ch12 的后面几小节:ch5 的大部分. 我在整理笔 ...
- CSAPP(深入理解计算机系统)读后感
9月到10月8号,包括国庆七天,大概每天5小时以上的时间,把Computer System: A Programmer Perspective 2rd version(深入理解计算机系统)的英文版啃完 ...
- <深入理解计算机系统> CSAPP Tiny web 服务器
本文是我学习<深入理解计算机系统>中网络编程部分的学习笔记. 1. Web基础 web客户端和服务器之间的交互使用的是一个基于文本的应用级协议HTTP(超文本传输协议).一个w ...
- 六星经典CSAPP笔记(1)计算机系统巡游
CSAPP即<Computer System: A Programmer Perspective>的简称,中文名为<深入理解计算机系统>.相信很多程序员都拜读过,之前买的旧版没 ...
- CSAPP - Ch 1 - 计算机系统漫游
目录 0 序言及摘要 1 信息就是位+上下文 2 程序被其他程序翻译成不同的格式 3 了解编译系统如何工作是大有益处的 0 序言及摘要 (1) 序言: CS:APP -- Computer Syste ...
随机推荐
- app再次进入数据不加载问题
问题原因:触发点击事件在加载页面之前完成. 1.调整了一下页面加载顺序 2.增加了settime的时间
- HTML5新特性 video '►'
var play = document.createElement('button') play.setAttribute('title','play') play.innerHTML = '►' 创 ...
- Zend Studio 12.5.1原版安装破解
安装官方Zend Studio 12.5.1原版,关闭zend studio,然后将破解补丁com.zend.verifier_12.5.1.v20150514-2003.jar覆盖到 安装目录\pl ...
- 解决Postgresql服务启动又关闭的问题
查看日志发现如下错误消息:%t LOG: could not receive data from client: An operation was attempted on something tha ...
- CUDA:Supercomputing for the Masses (用于大量数据的超级计算)-第十节
原文链接 第十节:CUDPP, 强大的数据平行CUDA库Rob Farber 是西北太平洋国家实验室(Pacific Northwest National Laboratory)的高级科研人员.他在多 ...
- 借鉴一些关于js框架的东西
八款Js框架介绍及比较,Dojo .Scriptaculous .Prototype .yui-ext .Jquery .Mochikit.mootools .moo.fx,componentartu ...
- github不能加载css、js解决办法
很奇怪,上午在公司还能正常访问github,晚点访问却有问题,页面样式明显错乱. 在FireFox下用F12开发者工具一看,有2条css和2条js 404 了,猜想应该是github的DNS被GFW污 ...
- java内存模型原理阅读总结
Java内存模型可以理解为在特定操作协议下,对特定的内存或高速缓存进行读写访问的过程抽象.不同架构的物理计算机可以有不一样的内存模型,java虚拟机也有自己的内存模型,java虚拟机规范中试图定义一种 ...
- SpringBoot学习4:springboot整合listener
整合方式一:通过注解扫描完成 Listener 组件的注册 1.编写listener package com.bjsxt.listener; import javax.servlet.ServletC ...
- Drop it-freecodecamp算法题目
Drop it 1.要求 丢弃数组(arr)的元素,从左边开始,直到回调函数return true就停止. 第二个参数,func,是一个函数.用来测试数组的第一个元素,如果返回fasle,就从数组中抛 ...