关于《Operating Systems: Three Easy Pieces》书中的 #include“common.h“

本文介绍了如何在阅读《OperatingSystems:ThreeEasyPieces》一书中遇到#include"common.h"时,通过创建common.h文件、添加相关代码并编译执行来解决问题。详细步骤包括新建项目、添加源文件、编写common.h内容及正确引用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前在读 《Operating Systems: Three Easy Pieces》这本书的时候里面有代码引用 #include"common.h"

 原书的代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<sys/time.h>
#include<assert.h>
#include"common.h" 

int main(int argc, char *argv[]){
	if (argc!=2){
		fprintf(stderr,"usage:cpu<string>\n");
		exit(1);
	}
	char *str = argv[1];
	while(1){
		Spin(1);
		printf("%s\n", str);
	}
	return 0;
} 

 图1

当遇到引用 #include"common.h"     解决办法是

1.打开devc++ >文件>新建>项目

2.将图1 中的 .c 的文件代码添加到项目中

3.在项目中新建一个common.h 的文件

4.common.h文件中的代码如下 可以直接复制使用:

#ifndef __common_h__
#define __common_h__
#include <sys/time.h>
#include <sys/stat.h>
#include <assert.h>

double GetTime() {
    struct timeval t;
    int rc = gettimeofday(&t, NULL);
    assert(rc == 0);
    return (double) t.tv_sec + (double) t.tv_usec/1e6;
}

void Spin(int howlong) {
    double t = GetTime();
    while ((GetTime() - t) < (double) howlong)
	; // do nothing in loop
}

#endif // __common_h__

5.编译执行  就ok  了

我是白水小琪七ya , 希望这个对你们有帮助!!!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值