Skip to content
Advertisement

Compiler cannot find header file within header file in C++

I have a header file provided by yaml-cpp library, yaml.h

yaml.h:

#include "yaml-cpp/parser.h"
#include "yaml-cpp/emitter.h"
#include "yaml-cpp/emitterstyle.h"
#include "yaml-cpp/stlemitter.h"
#include "yaml-cpp/exceptions.h"

#include "yaml-cpp/node/node.h"
#include "yaml-cpp/node/impl.h"
#include "yaml-cpp/node/convert.h"
#include "yaml-cpp/node/iterator.h"
#include "yaml-cpp/node/detail/impl.h"
#include "yaml-cpp/node/parse.h"
#include "yaml-cpp/node/emit.h"

main.cpp

#include "./lib/yaml-cpp/include/yaml.h"

int main()
{
    YAML::Node config = YAML::LoadFile("config.yaml");
    return 0;
    
}

All the header files are in the same directory (/home/user/application/libs/yaml-cpp/include), but the compiler is unable to find parser.h and all the other includes. Why is this so and how do I fix it?

I have tried using g++ -I/home/user/application/libs/yaml-cpp/include main.cpp but that did not work.

I am on a linux environment. Everything works fine when the header files are kept in /usr/lib64, but I am not allowed to do that for this project.

Advertisement

Answer

When you have a file yaml.h that itself includes other files like this:

#include "yaml-cpp/parser.h"

Then the expected directory layout is as follows:

somewhere/
  |
  +-- yaml.h
  |
  +-- yaml-cpp/
        |
        +-- parser.h

You are expected to pass -Isomewhere to your compiler and use the header file yaml.h like this in your own source code:

#include <yaml.h>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement