Fatal error: iostream: No such file or directory in compiling C program using GCC
Fatal error: iostream: No such file or directory in compiling C program using GCC
为什么当我想编译下面的多线程合并排序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 <iostream>
^
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 <iostream.h>
^
compilation terminated.
我的程序:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
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 <= high) {
if (a[left] > 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 < (high-low+1) ; i++) a[low+i] = b[i];
}
void * mergesort(void *a)
{
ArrayIndex *pa = (ArrayIndex *)a;
int mid = (pa->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 < N; i++) pthread_join(thread[i], NULL);
merge(pa->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;
}
<iostream>
和 <iostream.h>
都不是标准的 C header 文件。您的代码应该是 C++,其中 <iostream>
是有效的 header。为 C++ 代码使用 C++ 编译器,例如 clang++
或 g++
(以及 .cpp
文件扩展名)。
或者,该程序主要使用 C 中可用的构造。将整个程序转换为使用 C 编译器进行编译非常容易。只需删除 #include <iostream>
和 using namespace std;
,并将 cout << endl;
替换为 putchar('\n');
... 我建议使用 C99、C11 或 C18 进行编译(例如 gcc -std=c99
、clang -std=c18
等)
您似乎在意识到自己正在处理与 size_t
相关的更简单问题后发布了一个新问题。我很高兴你做到了。
无论如何,您有一个 .c
源文件,大部分代码看起来都符合 C 标准,除了 #include <iostream>
和 using namespace std;
可以通过 #include<stdio.h>
获得 C++ 标准 #include<iostream>
内置函数的 C 等价物
- 将
#include <iostream>
替换为#include <stdio.h>
,删除using namespace std;
取消 #include <iostream>
后,您将需要 cout << endl;
的 C 标准替代方案,可以通过 printf("\n");
或 putchar('\n');
根据我的观察,在这两个选项中,printf("\n");
工作得更快。
当在上面的代码中使用 printf("\n");
代替 cout<<endl;
$ 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<<endl;
$ 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 分钟)
为什么当我想编译下面的多线程合并排序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 <iostream>
^
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 <iostream.h>
^
compilation terminated.
我的程序:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <iostream>
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 <= high) {
if (a[left] > 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 < (high-low+1) ; i++) a[low+i] = b[i];
}
void * mergesort(void *a)
{
ArrayIndex *pa = (ArrayIndex *)a;
int mid = (pa->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 < N; i++) pthread_join(thread[i], NULL);
merge(pa->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;
}
<iostream>
和 <iostream.h>
都不是标准的 C header 文件。您的代码应该是 C++,其中 <iostream>
是有效的 header。为 C++ 代码使用 C++ 编译器,例如 clang++
或 g++
(以及 .cpp
文件扩展名)。
或者,该程序主要使用 C 中可用的构造。将整个程序转换为使用 C 编译器进行编译非常容易。只需删除 #include <iostream>
和 using namespace std;
,并将 cout << endl;
替换为 putchar('\n');
... 我建议使用 C99、C11 或 C18 进行编译(例如 gcc -std=c99
、clang -std=c18
等)
您似乎在意识到自己正在处理与 size_t
相关的更简单问题后发布了一个新问题。我很高兴你做到了。
无论如何,您有一个 .c
源文件,大部分代码看起来都符合 C 标准,除了 #include <iostream>
和 using namespace std;
可以通过 #include<stdio.h>
#include<iostream>
内置函数的 C 等价物
- 将
#include <iostream>
替换为#include <stdio.h>
,删除using namespace std;
取消
#include <iostream>
后,您将需要cout << endl;
的 C 标准替代方案,可以通过printf("\n");
或putchar('\n');
根据我的观察,在这两个选项中,printf("\n");
工作得更快。当在上面的代码中使用
printf("\n");
代替cout<<endl;
$ 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<<endl;
$ 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 分钟)