<div dir="ltr">I did file a bug report then:<div><br></div><div><a href="https://gitlab.kitware.com/cmake/cmake/issues/17556">https://gitlab.kitware.com/cmake/cmake/issues/17556</a><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">2017-12-11 15:12 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:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">> I'm not that sure that this is the "expected" behavior<br>
<br>
Yes, now it looks like a bug to me, too. Being written as it was in the<br>
last message,<br>
> target_sources(lib INTERFACE lib.h)<br>
can be treated as the equivalent of<br>
> target_sources(main PRIVATE lib.h)<br>
(and therefore generating an error).<br>
<br>
However,<br>
<span class="">> target_include_directories(lib INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}")<br>
</span>on the command line resolves to<br>
> -I/home/l2y/<...>/lib<br>
and<br>
> target_include_directories(lib INTERFACE ".")<br>
resolves to<br>
> -I/home/l2y/<...>/lib/.<br>
<br>
Also, looking at `<build_dir>/main/CMakeFiles/<wbr>main.dir/depend.internal`:<br>
<br>
> main/CMakeFiles/main.dir/main.<wbr>c.o<br>
>  ../lib/lib.h<br>
>  /home/l2y/<...>/main/main.c<br>
So, all elements in `target_include_directories()` resolve to full<br>
paths, as well as `target_sources()`, while `target_sources(INTERFACE)`<br>
stay relative, which causes trouble.<br>
<span class=""><br>
On 11.12.2017 09:20, Eric Noulard wrote:<br>
><br>
><br>
> 2017-12-09 1:37 GMT+01:00 Oleksii Vilchanskyi<br>
</span>> <<a href="mailto:oleksii.vilchanskyi@gmail.com">oleksii.vilchanskyi@gmail.com</a> <mailto:<a href="mailto:oleksii.vilchanskyi@gmail.com">oleksii.vilchanskyi@<wbr>gmail.com</a>>>:<br>
<div class="HOEnZb"><div class="h5">><br>
>     Hello Eric,<br>
><br>
>     > Why should I do that?<br>
>     > 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>
><br>
><br>
> Hi Oleksii,<br>
><br>
> Thanks you for the minimal reproducible example.<br>
> I'm bot that sure that this is the "expected" behavior<br>
>  <br>
><br>
>     Listfile under `lib`:<br>
><br>
>     > add_library(lib INTERFACE)<br>
>     > target_include_directories(lib INTERFACE<br>
>     "${CMAKE_CURRENT_SOURCE_DIR}")<br>
>     > target_sources(lib INTERFACE "lib.h")<br>
><br>
><br>
> With the previous one would expect that any target linking to this<br>
> interface target<br>
> will be provided with the include directories provided in its interface.<br>
> In your case (and in mine too) this is given with **full path**<br>
>  <br>
><br>
><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>
><br>
><br>
> Yes precisely. This seems to work OK for imported targets but not for<br>
> header-only in-project lib.<br>
>  <br>
><br>
><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>
><br>
><br>
> We (you and me) already provide <br>
>  target_include_directories<br>
> with full path.<br>
><br>
> I cannot provide the file themselves with relative path since they may<br>
> be used<br>
> from different place.<br>
><br>
> I guess I will open an issue unless someone explain me the rational<br>
> behind that behavior.<br>
><br>
><br>
> --<br>
> Eric<br>
<br>
</div></div><div class="HOEnZb"><div class="h5">--<br>
Regards,<br>
Oleksii Vilchanskyi<br>
PGP:<wbr>0x8D3A0E046BDE941F2A53867CE3FD<wbr>952D48C0B338<br>
<br>
</div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div>Eric<br></div></div></div></div></div>
</div>