前言
最近在看《Operating Systems - Three Easy Pieces》
发现自己C编程能力是真的弱啊,甚至读不懂。
一 代码段内容:
cpu.c
文件
#include <stdio.h>
#include <stdlib.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) {
printf("%s\n", str);
Spin(1);
}
return 0;
}
一开始报错 #include "common.h"
行。
- 系统自带的头文件用尖括号括起来,这样编译器会在系统文件目录下查找。
#include <xxx.h> - 用户自定义的文件用双引号括起来,编译器首先会在用户目录下查找,然后在到C++安装目录(比如VC中可以指定和修改库文件查找路径,Unix和Linux中可以通过环境变量来设定)中查找,最后在系统文件中查找。
在本书的github
厂库中找到了官方给出的代码,里面有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