[CMake] Unable to find source files in different directory using target_sources(INTERFACE)

Robert Dailey rcdailey.lists at gmail.com
Sat Jul 9 21:37:10 EDT 2016


Thanks, I just verified that doing absolute path works. I find it
frustrating though that relative paths aren't calculated to absolute
at the time the function is called, though. I feel like CMake should
be doing this for me.

On Sat, Jul 9, 2016 at 7:23 PM, Craig Scott <craig.scott at crascit.com> wrote:
> Sorry, hit the send button prematurely. To achieve your desired goal of
> creating a header only library as a target in Visual Studio, it somewhat
> depends on what you want to do with that library. If you just want it to be
> there so you can see the header in the IDE, then simply defining a dummy
> custom target is one approach I've seen people use. E.g.
>
>   add_custom_target(foo SOURCES foo.h)
>
> If you want to actually link to the target so it can provide header search
> path dependencies, then the approach you tried is probably the closest (with
> the use of absolute paths as described in my previous email). As you
> discovered though, it adds the header to the sources list of each project
> linking against it, which it sounds like you want to avoid. An interface
> library doesn't create build output on its own, so it doesn't make sense for
> it to have its own list of sources. The only way sources make sense for it
> is for those sources to be added to anything linking against it, which is
> the behaviour you are seeing.
>
>
>
>
>
> On Sun, Jul 10, 2016 at 10:04 AM, Craig Scott <craig.scott at crascit.com>
> wrote:
>>
>> 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.lists at 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
>
>
>
>
> --
> Craig Scott
> Melbourne, Australia
> http://crascit.com


More information about the CMake mailing list