<div dir="ltr">When using target_sources(), if you specify the source file(s) with a relative path, CMake does not convert that to an absolute path when storing it in the target's SOURCES property (that's my interpretation anyway). The end result is that the source you specify is going to be interpreted as being relative to the source directory the dependent target is defined in, not the directory where you called target_sources().  You can prevent the problem you described by ensuring the path for the source file(s) is always an absolute one. For example:<div><br></div><div>target_sources(<span style="font-size:13px">foo INTERFACE "${CMAKE_CURRENT_LIST_DIR}/foo.h")</span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px">You may find the following blog article useful (it discusses the above problem and also other related material):</span></div><div><span style="font-size:13px"><br></span></div><div><span style="font-size:13px"><a href="https://crascit.com/2016/01/31/enhanced-source-file-handling-with-target_sources/">https://crascit.com/2016/01/31/enhanced-source-file-handling-with-target_sources/</a><br></span></div><div><br></div><div><br></div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jul 10, 2016 at 9:03 AM, Robert Dailey <span dir="ltr"><<a href="mailto:rcdailey.lists@gmail.com" target="_blank">rcdailey.lists@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I have the following:<br>
<br>
cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR)<br>
project(foo)<br>
add_library(foo INTERFACE)<br>
target_sources(foo INTERFACE foo.h)<br>
add_subdirectory(subdir)<br>
<br>
And inside subdir, I specify add_executable() and then<br>
target_link_libraries(myexe foo).<br>
<br>
I get an error:<br>
<br>
Cannot find source file: foo.h<br>
<br>
I'm using CMake 3.6. What am I doing wrong here?<br>
<br>
My goal is to generate a header-only project in Visual Studio.<br>
According to the documentation, what I'm trying to do won't give me<br>
that. Instead, foo.h would exist in every project that links to it as<br>
a dependency (that's what it seems like anyhow).<br>
<br>
Advice is appreciated.<br>
--<br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="http://public.kitware.com/mailman/listinfo/cmake" rel="noreferrer" target="_blank">http://public.kitware.com/mailman/listinfo/cmake</a><br>
</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">Craig Scott<br><div>Melbourne, Australia</div><div><a href="http://crascit.com" target="_blank">http://crascit.com</a><br></div></div></div></div></div>
</div>