<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">2017-12-09 1:37 GMT+01:00 Oleksii Vilchanskyi <span dir="ltr"><<a href="mailto:oleksii.vilchanskyi@gmail.com" target="_blank">oleksii.vilchanskyi@gmail.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hello Eric,<br>
<span class="gmail-"><br>
> Why should I do that?<br>
</span>> Is this the expected behavior / way to create header only library?<br>
<br>
Yes, this is the expected behaviour. CMake interprets a relative path to<br>
the file according to the target's add_library() or add_executable()<br>
call directory. I will provide a minimal, reproducible example:<br></blockquote><div><br></div><div>Hi Oleksii,</div><div><br></div><div>Thanks you for the minimal reproducible example.</div><div>I'm bot that sure that this is the "expected" behavior</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Listfile under `lib`:<br>
<br>
> add_library(lib INTERFACE)<br>
> target_include_directories(lib INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")<br>
> target_sources(lib INTERFACE "lib.h")<br></blockquote><div><br></div><div>With the previous one would expect that any target linking to this interface target</div><div>will be provided with the include directories provided in its interface.</div><div>In your case (and in mine too) this is given with **full path**</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
Listfile under `main`:<br>
<br>
> add_executable(main "main.c")<br>
> target_link_libraries(main PRIVATE lib)<br>
<br>
Now, let's look at the diagnostics:<br>
<br>
> CMake Error at main/CMakeLists.txt:1 (add_executable):<br>
>   Cannot find source file:<br>
><br>
>     lib.h<br>
<br>
So, the problem occurs when CMake cannot find `lib.h` relatively to<br>
`add_executable(main "main.c")` call. Whether it's the expected<br>
behaviour is debatable (I personally would prefer CMake to search<br>
relatively to `add_library(lib INTERFACE)`), and the documentation is<br>
silent on this caveat either, so there's that.<br></blockquote><div><br></div><div>Yes precisely. This seems to work OK for imported targets but not for</div><div>header-only in-project lib.</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
One way to solve this is to provide a path to `lib.h` relatively to<br>
`main` directory, another way is to provide a full path.<br></blockquote><div><br></div><div>We (you and me) already provide </div><div> target_include_directories</div><div>with full path.</div><div><br></div><div>I cannot provide the file themselves with relative path since they may be used</div><div>from different place.</div><div><br></div><div>I guess I will open an issue unless someone explain me the rational behind that behavior.</div><div><br></div></div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Eric<br></div></div></div></div></div>
</div></div>