致命错误:iostream:使用GCC编译C程序时没有这样的文件或目录

为什么当我想编译以下multithreading合并排序C程序时,我收到此错误:

ap@sharifvm:~/forTHE04a$ gcc -g -Wall -o mer mer.c -lpthread mer.c:4:20: fatal error: iostream: No such file or directory #include  ^ compilation terminated. ap@sharifvm:~/forTHE04a$ gcc -g -Wall -o mer mer.c -lpthread mer.c:4:22: fatal error: iostream.h: No such file or directory #include  ^ compilation terminated. 

我的节目:

 #include  #include  #include  #include  using namespace std; #define N 2 /* # of thread */ int a[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; /* target array */ /* structure for array index * used to keep low/high end of sub arrays */ typedef struct Arr { int low; int high; } ArrayIndex; void merge(int low, int high) { int mid = (low+high)/2; int left = low; int right = mid+1; int b[high-low+1]; int i, cur = 0; while(left <= mid && right  a[right]) b[cur++] = a[right++]; else b[cur++] = a[right++]; } while(left <= mid) b[cur++] = a[left++]; while(right <= high) b[cur++] = a[left++]; for (i = 0; i low + pa->high)/2; ArrayIndex aIndex[N]; pthread_t thread[N]; aIndex[0].low = pa->low; aIndex[0].high = mid; aIndex[1].low = mid+1; aIndex[1].high = pa->high; if (pa->low >= pa->high) return 0; int i; for(i = 0; i < N; i++) pthread_create(&thread[i], NULL, mergesort, &aIndex[i]); for(i = 0; i low, pa->high); //pthread_exit(NULL); return 0; } int main() { ArrayIndex ai; ai.low = 0; ai.high = sizeof(a)/sizeof(a[0])-1; pthread_t thread; pthread_create(&thread, NULL, mergesort, &ai); pthread_join(thread, NULL); int i; for (i = 0; i < 10; i++) printf ("%d ", a[i]); cout << endl; return 0; } 

都不是标准的C头文件。 您的代码应该是C ++,其中是一个有效的标头。 对C ++代码使用g++ (和.cpp文件扩展名)。

或者,该程序主要使用C中可用的构造。 使用C编译器将整个程序转换为编译很容易。 只需删除#include using namespace std; ,并替换cout << endl;putchar('\n'); ...我建议使用C99进行编译(例如gcc -std=c99

在您意识到您正在处理与size_t相关的更简单问题之后,似乎您发布了一个新问题。 你很高兴。

无论如何,你有一个.c源文件,大多数代码看起来都符合C标准,除了#include using namespace std;

C ++标准#include的内置函数的C等效可以通过#include

  1. #include 替换#include using namespace std;删除using namespace std;
  2. #include 取消之后,你需要一个C标准替代cout << endl; ,可以通过printf("\n");putchar('\n');
    在两个选项中, printf("\n"); 我观察到的工作速度越快。

    使用printf("\n"); 在上面的代码中代替cout<

     $ time ./thread.exe 1 2 3 4 5 6 7 8 9 10 real 0m0.031s user 0m0.030s sys 0m0.030s 

    当使用putchar('\n'); 在上面的代码中代替cout<

     $ time ./thread.exe 1 2 3 4 5 6 7 8 9 10 real 0m0.047s user 0m0.030s sys 0m0.030s 

用Cygwin gcc (GCC) 4.8.3版本编译。 结果平均超过10个样本。 (花了我15分钟)