【问题标题】:fatal error: 'common.h' file not found under mac osx 10.10.5致命错误:在 mac osx 10.10.5 下找不到“common.h”文件
【发布时间】:2015-09-29 07:08:35
【问题描述】:

我按照书操作系统:三个简单的部分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;
}

当我尝试gcc -o cpu cpu.c -Wall

错误出来了:fatal error: 'common.h' file not found,

我试过从this link下载common.h,把这个文件和cpu.c放在一起,但是不行,错误信息:

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.

如何修复错误?谢谢。

【问题讨论】:

  • @shami 谢谢,我明白了。
  • 我添加为答案 :)

标签: c++ c macos


【解决方案1】:

此问题的 "common.h" 标头在第 1 章旁边的课程目录中链接为 tgz 包:http://pages.cs.wisc.edu/~remzi/OSTEP/

将它放在源文件旁边,然后再次尝试编译。

【讨论】:

  • 为什么不直接添加链接http://pages.cs.wisc.edu/~remzi/OSTEP/Code/code.intro.tgz
  • @color-blind 我的课程经验表明,我链接的目录结构下面的目录结构被重新设计了很多。我希望父级能够找到该文件,即使目录结构发生了变化。
【解决方案2】:

这就是您要查找的内容。所有其他答案都不明白您是在使用操作系统书籍。请参考网站上提供的链接。

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__

【讨论】:

    【解决方案3】:

    您的 main 函数声明应遵循 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;
    }
    

    并使用以下代码编译您的代码:

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

    【讨论】:

      【解决方案4】:

      common.h 不是要添加的标准标头。如果你想编译和运行这段代码,无论是找到包含 Spin() 的 common.h 还是在没有 Spin() 和 common 的情况下编译和运行你的代码。省略 Spin() 和 common.h 不会影响对书中讨论的理解

      【讨论】:

      • 你如何找到common.h?
      • 见下文 - 我找到了您丢失的 common.h 文件。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 2020-10-02
      • 2015-01-20
      • 2014-12-21
      • 2013-12-10
      相关资源
      最近更新 更多