attolee
attolee

Reputation: 622

fatal error: 'common.h' file not found under mac osx 10.10.5

I follow the book Operating Systems: Three Easy Pieces, the code in introduction chapter,

#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;
}

When I try gcc -o cpu cpu.c -Wall,

The error came out: fatal error: 'common.h' file not found,

I have tried download common.h from this link, and put this file with cpu.c, but it doesn't work, error message:

cpu.c:8:1: error: conflicting types for 'main'
main(int argc, char *argv[])
^
./common.h:86:13: note: previous declaration is here
extern int              main(int, char **, char **);
                        ^
cpu.c:18:5: warning: implicit declaration of function 'Spin' is invalid in C99 [-Wimplicit-function-declaration]
    Spin(1);
    ^
1 warning and 1 error generated.

How to fix the error? Thanks.

Upvotes: 7

Views: 7454

Answers (5)

Ocean
Ocean

Reputation: 19

Spin(1) is the same as sleep(1), so I will just use sleep()

Upvotes: 0

ChannelJuanNews
ChannelJuanNews

Reputation: 425

Here is what you are looking for. All the other answers don't understand that you were working from an OS book. Please refer to the link provided on the site.

http://pages.cs.wisc.edu/~remzi/OSTEP/Code/code.intro.tgz

#ifndef __common_h__
#define __common_h__

#include <sys/time.h>
#include <assert.h>
#include <pthread.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
}

void Pthread_create(pthread_t *t, const pthread_attr_t *attr,  
    void *(*start_routine)(void *), void *arg) {
    int rc = pthread_create(t, attr, start_routine, arg);
    assert(rc == 0);
}

void Pthread_join(pthread_t thread, void **value_ptr) {
    int rc = pthread_join(thread, value_ptr);
    assert(rc == 0);
}

void Pthread_mutex_lock(pthread_mutex_t *mutex) {
    int rc = pthread_mutex_lock(mutex);
    assert(rc == 0);
}

void Pthread_mutex_unlock(pthread_mutex_t *mutex) {
    int rc = pthread_mutex_unlock(mutex);
    assert(rc == 0);
}

void Pthread_mutex_init(pthread_mutex_t *mutex, pthread_mutexattr_t *attr) {
    int rc = pthread_mutex_init(mutex, attr);
    assert(rc == 0);
}


#endif // __common_h__

Upvotes: 5

Erik
Erik

Reputation: 610

The "common.h" header for this problem is linked as a tgz bundle in the table of contents for the course next to chapter 1: http://pages.cs.wisc.edu/~remzi/OSTEP/

Drop it next to your source file and try compiling again.

Upvotes: 8

incompetent
incompetent

Reputation: 1822

common.h is not standard header to be added. If you want to compile and run this piece of code whether find common.h which contains Spin() or compile and run your code with out Spin() and common. Omitting Spin() and common.h will not have effect on the understanding of discussion provided in the book

Upvotes: -1

parachvte
parachvte

Reputation: 56

Your declaration of main function should follow that declared in common.h:

#include <stdio.h>
#include "common.h"

// not this:
//int main(int argc, char *argv[]) {
// but:
int main(int argc, char *argv[], char *argv2[]) {
    return 0;
}

and compile your code with something like:

gcc cpu.c common.h -Wall -o cpu

Upvotes: 0

Related Questions