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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30543286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:47:30  来源:igfitidea点击:

Fatal error: iostream: No such file or directory in compiling C program using GCC

c++cgccg++

提问by Abraham

Why when I wan to compile the following multi thread merge sorting C program, I receive this error:

为什么当我想编译以下多线程合并排序 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.

My program:

我的程序:

#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;
}

回答by autistic

Neither <iostream>nor <iostream.h>are standard C header files. Your code is meant to be C++, where <iostream>is a valid header. Use g++(and a .cppfile extension) for C++ code.

既不是标准 C 头文件,<iostream>也不<iostream.h>是标准 C 头文件。您的代码应该是 C++,其中<iostream>是有效的标头。将g++(和.cpp文件扩展名)用于 C++ 代码。

Alternatively, this program uses mostly constructs that are available in C anyway. It's easy enough to convert the entire program to compile using a C compiler. Simply remove #include <iostream>and using namespace std;, and replace cout << endl;with putchar('\n');... I advise compiling using C99 (eg. gcc -std=c99)

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

回答by WedaPashi

Seems like you posted a new question after you realized that you were dealing with a simpler problem related to size_t. I am glad that you did.

似乎您在意识到您正在处理与size_t. 我很高兴你做到了。

Anyways, You have a .csource file, and most of the code looks as per C standards, except that #include <iostream>and using namespace std;

无论如何,您有一个.c源文件,并且大部分代码看起来都符合 C 标准,除了#include <iostream>using namespace std;

C equivalent for the built-in functions of C++ standard #include<iostream>can be availed through #include<stdio.h>

C++标准内置函数的C等价物#include<iostream>可以通过#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
    
  1. 替换#include <iostream>#include <stdio.h>,删除using namespace std;
  2. 随着#include <iostream>带下,你会需要一个C标准的替代cout << endl;,它可以这样做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
    

Compiled with Cygwin gcc (GCC) 4.8.3version. results averaged over 10 samples. (Took me 15 mins)

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