sourcetip

치명적 오류: iostream:GCC를 사용하여 C 프로그램을 컴파일할 때 이러한 파일이나 디렉터리가 없습니다.

fileupload 2023. 10. 25. 23:41
반응형

치명적 오류: iostream:GCC를 사용하여 C 프로그램을 컴파일할 때 이러한 파일이나 디렉터리가 없습니다.

다음 다중 스레드 병합 정렬 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 헤더 파일입니다.당신의 코드는 C++가 되어야 합니다.<iostream>는 유효한 헤더입니다.다음과 같은 C++ 컴파일러를 사용합니다.clang++아니면g++(그리고..cpp파일 확장자)를 입력합니다.

또는, 이 프로그램은 C에서 사용할 수 있는 컨스트럭트를 대부분 사용합니다.전체 프로그램을 C 컴파일러로 변환하는 것은 충분히 쉽습니다.간단히 제거#include <iostream>그리고.using namespace std;, 교체합니다.cout << endl;와 함께putchar('\n');... C99, C11 또는 C18(예: C99, C11 또는 C18)을 사용하여 컴파일하는 것을 권장합니다.gcc -std=c99,clang -std=c18등)

당신이 다음과 관련된 더 간단한 문제를 다루고 있다는 것을 깨달은 후에 새로운 질문을 올린 것 같습니다.size_t. 당신이 그렇게 해주어서 기쁩니다.

아무튼 여러분들은..c소스 파일, 그리고 대부분의 코드는 C 표준에 따라 보입니다. 단, 그것은 제외합니다.#include <iostream>그리고.using namespace std;

C++ 표준의 내장 기능에 대한 C 등가#include<iostream>을 통해 이용할 수 있습니다.#include<stdio.h>

  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
    

Cygwin과 함께 컴파일됨gcc (GCC) 4.8.3버전평균 10개 이상의 샘플 결과(15분 소요)

동일한 오류에 직면했습니다 : -

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

시간이 좀 흐른 후에, 저는 여기서 아주 초보적인 실수를 하고 있습니다.사실 나는 내 프로그램 파일을 확장자를 주면서 실행하고 있었습니다.".c"only..... :-( 그러니 파일을 적절한 확장명으로 저장하고 있는지 확인해 보세요...이 경우 C++ 프로그램 파일의 .cpp가 됩니다.그러니 한 번 확인해보시면 괜찮을 겁니다.

그래서 이제 먼저 MinGW를 다운받아서 env 변수에 추가했는지 확인합니다.

사용법을 점검하다g++ --versioncmd/단말기에

만약 아직도 꼬불꼬불한 선들이 나타나면 설치합니다.c++ Intellisenseextension 당신은 대부분 오류를 얻게 될 것입니다.c_cpp_properties.json컴파일러 경로가 올바르지 않을 수 있기 때문에 파일.

그래서 당신이 해야 할 일은 정확한 길을 놓는 것이고 그러면 그 꼬불꼬불한 선들이 사라질 것입니다.

언급URL : https://stackoverflow.com/questions/30543286/fatal-error-iostream-no-such-file-or-directory-in-compiling-c-program-using-gc

반응형