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:
target_sources(foo INTERFACE "${CMAKE_CURRENT_LIST_DIR}/foo.h") You may find the following blog article useful (it discusses the above problem and also other related material): https://crascit.com/2016/01/31/enhanced-source-file-handling-with-target_sources/ On Sun, Jul 10, 2016 at 9:03 AM, Robert Dailey <rcdailey.li...@gmail.com> wrote: > I have the following: > > cmake_minimum_required(VERSION 3.1.0 FATAL_ERROR) > project(foo) > add_library(foo INTERFACE) > target_sources(foo INTERFACE foo.h) > add_subdirectory(subdir) > > And inside subdir, I specify add_executable() and then > target_link_libraries(myexe foo). > > I get an error: > > Cannot find source file: foo.h > > I'm using CMake 3.6. What am I doing wrong here? > > My goal is to generate a header-only project in Visual Studio. > According to the documentation, what I'm trying to do won't give me > that. Instead, foo.h would exist in every project that links to it as > a dependency (that's what it seems like anyhow). > > Advice is appreciated. > -- > > Powered by www.kitware.com > > Please keep messages on-topic and check the CMake FAQ at: > http://www.cmake.org/Wiki/CMake_FAQ > > Kitware offers various services to support the CMake community. For more > information on each offering, please visit: > > CMake Support: http://cmake.org/cmake/help/support.html > CMake Consulting: http://cmake.org/cmake/help/consulting.html > CMake Training Courses: http://cmake.org/cmake/help/training.html > > Visit other Kitware open-source projects at > http://www.kitware.com/opensource/opensource.html > > Follow this link to subscribe/unsubscribe: > http://public.kitware.com/mailman/listinfo/cmake > -- Craig Scott Melbourne, Australia http://crascit.com
-- Powered by www.kitware.com Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ Kitware offers various services to support the CMake community. For more information on each offering, please visit: CMake Support: http://cmake.org/cmake/help/support.html CMake Consulting: http://cmake.org/cmake/help/consulting.html CMake Training Courses: http://cmake.org/cmake/help/training.html Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html Follow this link to subscribe/unsubscribe: http://public.kitware.com/mailman/listinfo/cmake