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

hts6caw3  于 2022-11-13  发布在  iOS
关注(0)|答案(4)|浏览(1726)

为什么当我想编译下面的多线程合并排序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;
}
2vuwiymt

2vuwiymt1#

<iostream><iostream.h>都不是标准的C头文件。您的代码应该是C++,其中<iostream>是有效的头文件。请使用C编译器,如clang++g++(以及.cpp文件扩展名)编译C代码。
或者,这个程序使用的大多数结构都是C语言中可用的。将整个程序转换为C编译器编译是很容易的。只需删除#include <iostream>using namespace std;,并用putchar('\n');替换cout << endl; ...我建议使用C99、C11或C18(例如gcc -std=c99clang -std=c18等)编译。

yduiuuwa

yduiuuwa2#

看起来你在意识到你正在处理一个与size_t有关的更简单的问题后发布了一个新的问题。我很高兴你这样做了。
无论如何,您有一个.c源文件,大多数代码看起来都符合C标准,除了#include <iostream>using namespace std;
C++标准#include<iostream>内置函数的C等效函数可通过#include<stdio.h>获得
1.将#include <iostream>替换为#include <stdio.h>,删除using namespace std;
1.取消了#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分钟)

deyfvvtc

deyfvvtc3#

我面临着同样的错误:-

fatal error: iostream: No such file or directory
 #include <iostream>

过了一段时间我发现,我在这里犯了一个非常非常低级的错误。实际上,我运行我的程序文件时只给出了扩展名".c"....:-(所以检查一下你是否用正确的扩展名保存了你的文件...在这种情况下,它将是.cpp的C++程序文件。所以检查一下,也许你会很好去。

14ifxucb

14ifxucb4#

因此,现在首先检查您是否已下载MinGW并将其添加到env变量中
在cmd/终端上检查使用g++ --version
现在,如果仍然出现弯曲的线条,安装c++ Intellisense扩展,您将在c_cpp_properties.json文件中得到错误,因为编译器路径可能不正确
所以你所要做的就是把正确的路径和swiggly线将消失

相关问题