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

Why, when I want to compile the following multi-thread merge sort, I get this error:

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

      

+3


source to share


2 answers


Neither <iostream>

, nor <iostream.h>

are standard C header files. Your code must be C ++, where <iostream>

is a valid header. Use g++

(and file extension .cpp

) for C ++ code.



However, this program uses mostly constructs available in C anyway. It's easy enough to convert the whole program to compile using the C compiler. Just remove #include <iostream>

and using namespace std;

and replace cout << endl;

with putchar('\n');

... I suggest compiling using C99 (eg. gcc -std=c99

)

+17


source


It looks like you posted a new question after realizing that you are dealing with a simpler problem related to size_t

. I'm glad you did it.

You have a source file anyway .c

, and most of the code looks like according to the C standards, except that #include <iostream>

andusing namespace std;

The C equivalent for built-in functions of the C ++ standard #include<iostream>

can be used via#include<stdio.h>



  • Replace #include <iostream>

    with #include <stdio.h>

    , removeusing namespace std;

  • When disabled, #include <iostream>

    you will need the standard C alternative for cout << endl;

    which can be accomplished with printf("\n");

    or putchar('\n');


    The two printf("\n");

    is faster than I've observed.

    When used printf("\n");

    in the code above, instead ofcout<<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 above code instead ofcout<<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 version gcc (GCC) 4.8.3

. the results are averaged over 10 samples. (It took me 15 minutes)

+2


source







All Articles