Including a header file from another directory

前端 未结 4 1525
别那么骄傲
别那么骄傲 2021-02-03 18:01

I have a main directory A with two sub directories B and C.

Directory B contains a header file structures.c

相关标签:
4条回答
  • 2021-02-03 18:29

    If you work on a Makefile project or simply run your code from command line, use

    gcc -IC main.c

    where -I option adds your C directory to the list of directories to be searched for header files, so you'll be able to use #include "structures.h"anywhere in your project.

    0 讨论(0)
  • 2021-02-03 18:33

    write

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

    in place of

    #include <structures.h>
    

    then go in directory in c & compile your main.c with

    gcc main.c
    
    0 讨论(0)
  • 2021-02-03 18:43

    If you want to use the command line argument then you can give gcc -idirafter ../b/ main.c

    then you don't have to do any thing inside your program.

    0 讨论(0)
  • 2021-02-03 18:54

    When referencing to header files relative to your c file you should use #include "path/to/header.h"

    The form #include <someheader.h> is only used for internal headers or for explicitly added directories (in gcc with the -I option).

    0 讨论(0)
提交回复
热议问题