# Question about yaml

**URL:** https://discourse.cmake.org/t/question-about-yaml/12698
**Category:** Code
**Tags:** os:windows
**Created:** [October 7, 2024, 4:31am UTC](https://discourse.cmake.org/t/question-about-yaml/12698 "2024-10-07T04:31:24Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Sea\_Sea](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sea_sea/32/4025_2.png) [@Sea\_Sea](https://discourse.cmake.org/u/Sea_Sea)
#### Post date: [October 7, 2024, 4:31am UTC](https://discourse.cmake.org/t/question-about-yaml/12698/1 "2024-10-07T04:31:24Z")

</div>

> **[GitHub - jbeder/yaml-cpp: A YAML parser and emitter in C++](https://github.com/jbeder/yaml-cpp?tab=readme-ov-file#how-to-integrate-it-within-your-project-using-cmake)**
>
> A YAML parser and emitter in C++

```auto
include(FetchContent)

FetchContent_Declare(
  yaml-cpp
  GIT_REPOSITORY https://github.com/jbeder/yaml-cpp.git
  GIT_TAG <tag_name> # Can be a tag (yaml-cpp-x.x.x), a commit hash, or a branch name (master)
)
FetchContent_MakeAvailable(yaml-cpp)

target_link_libraries(YOUR_LIBRARY PUBLIC yaml-cpp::yaml-cpp) # The library or executable that require yaml-cpp library

```

Q1. `YAML_CPP_INCLUDE_DIR` and `YAML_CPP_LIBRARIES` are “”?

Q2. Why is it unnecessary to use target\_include\_directories(test ${YAML\_CPP\_INCLUDE\_DIR})?  
only use target\_link\_libraries

Q3. I write a library with opencv and yaml-cpp and set this

```auto
# lib cmake
target_link_libraries(YOUR_LIBRARY PRIVATE ${OpenCV_LIBS} yaml-cpp::yaml-cpp)

```

Now I want to use another program to call YOUR\_LIBRARY

```auto
# new project cmake
target_link_libraries(test PRIVATE YOUR_LIBRARY::YOUR_LIBRARY)

```

It show the

```auto
# on new project error
'yaml-cpp/yaml.h': No such file or directory

```

Until I `only` change yaml-cpp::yaml-cpp from PRIVATE to PUBLIC, it build successly.

```auto
# lib cmake
target_link_libraries(YOUR_LIBRARY PRIVATE ${OpenCV_LIBS} 
PUBLIC yaml-cpp::yaml-cpp)

```

Why does it must be PUBLIC for yaml? But the opencv can PRIVATE.

---

<div class="post-metadata">

### Author: ![erk](https://discourse.cmake.org/user_avatar/discourse.cmake.org/erk/32/114_2.png) [@erk](https://discourse.cmake.org/u/erk)
#### Post date: [October 7, 2024, 6:08am UTC](https://discourse.cmake.org/t/question-about-yaml/12698/2 "2024-10-07T06:08:04Z")

</div>

> [@Sea\_Sea](#):
>
> Q1. `YAML_CPP_INCLUDE_DIR` and `YAML_CPP_LIBRARIES` are “”?
> 
> Q2. Why is it unnecessary to use target\_include\_directories(test ${YAML\_CPP\_INCLUDE\_DIR})?  
> only use target\_link\_libraries
> 
> Q3. I write a library with opencv and yaml-cpp and set this

May be you can ask thoses questions to yaml-cpp author?

Q1. I do not understand the question.  
Q2. Because the imported library `yaml-cpp::yaml-cpp` already convey the information and yo ulink to it.

Q3. Because it is the way it works for PRIVATE linking. See: [https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#target-command-scope](https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#target-command-scope)

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [October 7, 2024, 6:18am UTC](https://discourse.cmake.org/t/question-about-yaml/12698/3 "2024-10-07T06:18:01Z")

</div>

Q1: It seems that this library produces a proper [CMake config](https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html), so those variables aren’t needed and thus aren’t set, as the library’s target is discovered in `CONFIG` mode.

Q2 and Q3: Already answered by Eric here.

I would only add that if you, as the maintainer of `YOUR_LIBRARY`, know for sure that consuming projects will use some of its dependencies, then you might want to use the `PUBLIC` scope on linking to those dependencies (_and also consider adding the corresponding [find\_dependency()](https://cmake.org/cmake/help/latest/module/CMakeFindDependencyMacro.html) to your library’s CMake config, which in most cases you should do anyway, no matter the linking scope_).

---

<div class="post-metadata">

### Author: ![Sea\_Sea](https://discourse.cmake.org/user_avatar/discourse.cmake.org/sea_sea/32/4025_2.png) [@Sea\_Sea](https://discourse.cmake.org/u/Sea_Sea)
#### Post date: [October 8, 2024, 5:12am UTC](https://discourse.cmake.org/t/question-about-yaml/12698/4 "2024-10-08T05:12:33Z")

</div>

Thanks for your help

But I still can not understand the Q3.  
I found the key place.

Because YOUR\_LIBRARY use the yaml-cpp lib and define the yaml header file on YOUR\_LIBRARY.hpp .

When I chane the defination that define the yaml header file on YOUR\_LIBRARY.cpp, and It success.

```auto
# lib cmake
target_link_libraries(YOUR_LIBRARY PRIVATE ${OpenCV_LIBS} yaml-cpp::yaml-cpp)

# lib .h build fail
#include <yaml-cpp/yaml.h>

# lib.cpp build success
#include <yaml-cpp/yaml.h>

```

---

<div class="post-metadata">

### Author: ![retif](https://discourse.cmake.org/user_avatar/discourse.cmake.org/retif/32/1776_2.png) [@retif](https://discourse.cmake.org/u/retif)
#### Post date: [October 8, 2024, 7:30am UTC](https://discourse.cmake.org/t/question-about-yaml/12698/5 "2024-10-08T07:30:15Z")

</div>

When you are linking to a dependency with `PRIVATE` scope, that dependency’s public headers won’t be “exposed” to other projects, so only `YOUR_LIBRARY` will “see” them. Since those other projects don’t(?) need to do anything with your `lib.cpp`, you can include that dependency header there, and the build should go just fine.

But if you include that header in your `lib.h`, which I assume in turn is included in those other projects, then it is only natural that the build will fail, which is why if you intend to include the depedendency’s public header in your library’s public header (_do you actually need to do that?_), then you should probably link to that dependency with a `PUBLIC` scope (_or explicitly link to that dependency in those other projects too_).

If you’d be interested to know some more, then here is [some documentation](https://cmake.org/cmake/help/latest/command/target_link_libraries.html#id4) and here is a [much more detailed post](https://cmake.org/pipermail/cmake/2016-May/063400.html) from the mailing list. There is also [this article](https://decovar.dev/blog/2023/07/22/cmake-target-link-libraries-scopes/), but this one is not part of the official documentation and does not originate from CMake maintainers.
