IT이야기

다른 디렉토리의 헤더 파일 포함

cyworld 2022. 5. 7. 09:38
반응형

다른 디렉토리의 헤더 파일 포함

나는 메인 디렉토리를 가지고 있다.A두 개의 하위 디렉토리가 있는B그리고C.

디렉토리B헤더 파일 포함structures.c:

#ifndef __STRUCTURES_H
#define __STRUCTURES_H
typedef struct __stud_ent__
{
    char name[20];
    int roll_num;
}stud;
#endif

디렉토리C포함하다main.c코드:

#include<stdio.h>
#include<stdlib.h>
#include <structures.h>
int main()
{
    stud *value;
    value = malloc(sizeof(stud));
    free (value);
    printf("working \n");
    return 0;
}

하지만 오류가 생겼어:

main.c:3:24: error: structures.h: No such file or directory
main.c: In function ‘main’:
main.c:6: error: ‘stud’ undeclared (first use in this function)
main.c:6: error: (Each undeclared identifier is reported only once
main.c:6: error: for each function it appears in.)
main.c:6: error: ‘value’ undeclared (first use in this function)

다음 항목을 포함하는 올바른 방법은?structures.h에 줄지어 들어가다.main.c?

c 파일과 관련된 헤더 파일을 참조할 때 다음을 사용하십시오.#include "path/to/header.h"

양식#include <someheader.h>내부 헤더 또는 명시적으로 추가된 디렉토리에만 사용됨(gcc:-I옵션).

글씨를 쓰다

#include "../b/structure.h"

대신에

#include <structures.h>

그리고 나서 c의 디렉토리에 들어가서 main.c를 컴파일하다

gcc main.c

Makefile 프로젝트에서 작업하거나 명령줄에서 코드를 실행하는 경우

gcc -IC main.c

어디에-I옵션이 추가됨C디렉터리에서 헤더 파일을 검색할 디렉터리 목록으로 이동하여#include "structures.h"당신의 프로젝트 어디든.

명령줄 인수를 사용하려면gcc -idirafter ../b/ main.c

프로그램 안에서 아무 것도 할 필요가 없어

참조URL: https://stackoverflow.com/questions/7581408/including-a-header-file-from-another-directory

반응형