0

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.

13
  • Does the path to the header files start with a "/"? Your pseudo code looks like that. Are you on a platform where such a path indicates an absolut path? Is that on purpose? Or do you instead want a relative path which starts with "path/to..."? Commented Oct 12, 2021 at 4:57
  • @Yunnosch I have updated the question with more information. the header is provided by a third party lib and in their header code they have a hardcoded path for the other header files. Commented Oct 12, 2021 at 5:19
  • Your pseudo code is riskily omitting relevant info. Does the "hardcoded path for the other header files" absolute"? Does it end in "/lib"? Consider doing less of "anonymisation" by creating a minimal reproducible example with precise but non-personal information. If necessary reinstall to change too personal paths. Please edit to collect all info in the question itself instead of splitting it between question body and comments down here. Consider making a minimal reproducible example with the same problem but without the library, to simplify debugging. I.e. simulate a similar library. Probably header-only is sufficient. Commented Oct 12, 2021 at 5:26
  • @Yunnosch I've updated the question. I thought it would be easier to understand without the extra information but apparently not. Please let me know if the updated question is clearer Commented Oct 12, 2021 at 5:37
  • Are you sure that the headers are inside "yaml-cpp/include" and the include lines list them as #include "yaml-cpp/parser.h", i.e. do NOT look for them inside a "include" folder? I really recommend to create a minimal reproducible example with a self-created header-only-library. It would allow you to experiment with different installation file structures, include lines ... It would also make reproducing your problem easier for potential answerers. Apart from the fact that making a MRE is simply a good way for yourself to trace the relevant detials of your problem. Commented Oct 12, 2021 at 5:40

1 Answer 1

4

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.