编译使用GCC的C程序时出现“致命错误:iostream:文件或目录不存在”的错误。

38

为什么我想编译以下的多线程归并排序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;
}

9
<iostream>using namespace std;是用于C++而不是C语言的。 - timrau
5
这看起来像是一份C++程序而不是C程序。最好将它保存为一个.cc文件,并使用g++进行编译,而不是gcc;或者将其改为C99程序。 - Basile Starynkevitch
4个回答

65

<iostream><iostream.h> 都不是标准的 C 头文件。您的代码应该是 C++,其中 <iostream> 是一个有效的头文件。使用像 clang++g++ 这样的 C++ 编译器(以及 .cpp 文件扩展名)来编译 C++ 代码。

此外,这个程序主要使用的构造在 C 中也是可用的。很容易将整个程序转换为使用 C 编译器进行编译。只需删除 #include <iostream>using namespace std;,并用 putchar('\n'); 替换 cout << endl;… 我建议使用 C99、C11 或 C18 进行编译(例如 gcc -std=c99clang -std=c18 等)。


9
似乎你在意识到涉及到 size_t 的简单问题后发布了一个新问题。我很高兴你这么做了。
无论如何,你有一个 .c 源文件,大部分代码符合 C 标准,除了 #include <iostream>using namespace std;
C++ 标准中内置函数的 C 等效代码可以通过 #include<stdio.h> 获取。
  1. Replace #include <iostream> with #include <stdio.h>, delete using namespace std;
  2. With #include <iostream> taken off, you would need a C standard alternative for cout << endl;, which can be done by printf("\n"); or putchar('\n');
    Out of the two options, printf("\n"); works the faster as I observed.

    When used printf("\n"); in the code above in place of cout<<endl;

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

    When used putchar('\n'); in the code above in place of 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分钟)


你的代码在哪里?你使用哪个编译器、标准库和命令来编译代码? - autistic
真的吗?你只打印了一个字符,计时并且尽管由于天知道的任务切换而引起的误差,你认为其中一个比另一个快?这就是你的测试方法学吗?哇,真是太棒了! - autistic
如果您编译为汇编语言(例如使用“gcc -O3 -S your_printf_file.c”命令),您可能会注意到printf调用被转换为putchar调用...有句古话,我相信Google可以为您完成,它的开头是这样的:过早优化是...... - autistic
@undefinedbehaviour: 就我所知,说实话,我不知道它被翻译成putchar,而且一直以为(并仍然相信)它被翻译成write调用。通过strace实用程序验证了这一点。它并不做你所提到的事情。 - WedaPashi
这在我的系统上并不是这样的,但我使用的是gcc 4.5.2。我想,与任务切换相比,优化本身更容易出现较大的误差,这只是另一个表明“过早优化是......”的因素。 - autistic
...一切罪恶的根源。所以,我会让OP决定使用什么。但是,很高兴你建议我编译成汇编语言,这是我从未认真尝试过的。 - WedaPashi

2
我遇到了相同的错误:-
fatal error: iostream: No such file or directory
 #include <iostream>

经过一段时间的研究,我发现我犯了一个非常非常初级的错误。实际上,我只是通过添加扩展名".c"来运行我的程序文件.... :-( 所以请检查您是否正确保存了文件的扩展名... 在这种情况下,C++程序文件的扩展名应该是.cpp。所以请检查一下,也许你就可以顺利运行了。


0

现在首先检查您是否已经下载了MinGW并将其添加到环境变量中

要检查,请在cmd /终端上使用g++ --version

现在,如果仍然出现波浪线,请安装c ++ Intellisense扩展程序,您可能会在c_cpp_properties.json文件中收到错误提示,因为编译器路径可能不正确

所以您需要做的就是放置正确的路径,波浪线就会消失


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接