반응형
다른 디렉토리의 헤더 파일 포함
나는 메인 디렉토리를 가지고 있다.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
반응형
'IT이야기' 카테고리의 다른 글
로거의 다른 레벨을 다른 대상에 기록하도록 Logback을 구성하는 방법 (0) | 2022.05.07 |
---|---|
Laravel Axios Vue를 사용하여 상태 코드 419로 실패 (0) | 2022.05.07 |
VS 코드의 Vuex 스토어에 대한 인텔리센스 (0) | 2022.05.07 |
오류: 렌더 함수에서 반환된 여러 루트 노드.v-dv-group 출신 (0) | 2022.05.07 |
nuxt js의 vuetify를 플러그인으로 사용하는 방법? (0) | 2022.05.07 |