之前在读 《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 , 希望这个对你们有帮助!!!