Include header file in different directory in c++

前端 未结 3 1238
谎友^
谎友^ 2021-01-17 18:37

I\'ve been learning c++ and encountered the following question: I have a directory structure like:

 - current directory

  - Makefile

  - include

     - he         


        
相关标签:
3条回答
  • 2021-01-17 18:47

    You can either add a -I option to the command line to tell the compiler to look there for header files. If you have header files in include/ directory, then this command should work for you.

    gcc -Iinclude/
    

    Since, you are using makefile, you can include this option in CFLAGS macro in your makefile.

    CFLAGS = -Iinclude/ -c -Wall
    

    OR

    You can include header files using #include "../include/header.h".

    0 讨论(0)
  • 2021-01-17 19:03

    Perhaps change your include line:

    #include "include/header.h"
    

    Assuming that's where your header exists - I'm making that assumption from your makefile

    0 讨论(0)
  • 2021-01-17 19:11

    You told the Makefile that include/header.h must be present, and you told the C++ source file that it needs header.h … but you did not tell the compiler where such headers live (i.e. in the "include" directory).

    Do this:

    CFLAGS = -c -Wall -Iinclude
    
    0 讨论(0)
提交回复
热议问题