[CMake] How to configure CMake to add current path to include directive.

Steve Lorimer steve.lorimer at gmail.com
Thu Oct 20 10:10:21 EDT 2016


Hi Dvir

Thanks for the suggestion. I tried CMAKE_INCLUDE_CURRENT_DIR
<https://cmake.org/cmake/help/latest/variable/CMAKE_INCLUDE_CURRENT_DIR.html>,
but I couldn't get it to work using that.

In the end I looped over the sources added to my target, extracted the
directories using get_filename_component, and then added them with
target_include_directories.

set(DIR_LIST)
foreach(FILE ${ARG_SRCS})
get_filename_component(ABS_FILE ${FILE}     ABSOLUTE)
get_filename_component(DIR      ${ABS_FILE} DIRECTORY)
add_unique(${DIR} DIR_LIST)                              # does a
list(FIND...) and if not found, list(APPEND...)
endforeach()

target_include_directories(${ARG_NAME} PRIVATE ${DIR_LIST})

I can't believe it's necessary to do this, and there must be a better way,
but it worked for me, so I moved on.

Thanks
Steve


On 20 October 2016 at 03:20, Dvir Yitzchaki <Dvir.Yitzchaki at ceva-dsp.com>
wrote:

> What about CMAKE_INCLUDE_CURRENT_DIR
> <https://cmake.org/cmake/help/latest/variable/CMAKE_INCLUDE_CURRENT_DIR.html>
> ?
>
>
>
> Regards,
>
>
>
> Dvir
>
> *From:* CMake [mailto:cmake-bounces at cmake.org] *On Behalf Of *Steve
> Lorimer
> *Sent:* Monday, October 17, 2016 5:04 AM
> *To:* Craig Scott <craig.scott at crascit.com>
> *Cc:* CMake Mailing List <cmake at cmake.org>
> *Subject:* Re: [CMake] How to configure CMake to add current path to
> include directive.
>
>
>
> Thanks Craig
>
>
>
> Looks like it did what you intended, but cmake won't allow it
>
>
>
> CMake Error in foo/CMakeLists.txt:
>
>   Found relative path while evaluating include directories of "foo":
>
>
>
>     "."
>
>
>
> Bummer! :(
>
>
>
>
>
>
>
> On 16 October 2016 at 18:35, Craig Scott <craig.scott at crascit.com> wrote:
>
> Something like the following was what I had in mind (untested):
>
>
>
> get_target_property(incDirs someTarget INCLUDE_DIRECTORIES)
>
> list(APPEND incDirs .)
>
> set_target_properties(someTarget PROPERTIES INCLUDE_DIRECTORIES
> "${incDirs}")
>
>
>
>
>
> On Mon, Oct 17, 2016 at 10:31 AM, Steve Lorimer <steve.lorimer at gmail.com>
> wrote:
>
> Thanks Craig
>
>
>
> Not sure if I'm doing it right, but I couldn't get that to work.
>
>
>
> Would you be able to give me an example please?
>
>
>
> Thanks
>
> Steve
>
>
>
>
>
>
>
> On 16 October 2016 at 17:51, Craig Scott <craig.scott at crascit.com> wrote:
>
> I think if you manipulate the target property INCLUDE_DIRECTORIES
> <https://cmake.org/cmake/help/latest/prop_tgt/INCLUDE_DIRECTORIES.html>
> directly, you should be able to force a "." in there without having it
> substituted for an absolute path. By this I mean use set_property() or
> set_target_properties() rather than target_include_directories() or
> include_directories(). Note, however, that the documentation for the
> INCLUDE_DIRECTORIES property explicitly recommends against adding relative
> paths to the property like this (but in your case it sounds like you really
> want a relative path, so maybe this is a valid exception to that advice).
>
>
>
>
>
>
>
> On Mon, Oct 17, 2016 at 9:46 AM, Steve Lorimer <steve.lorimer at gmail.com>
> wrote:
>
> In makefile parlance, I'm trying to add -I. (ie: the current directory) to
> my include paths
>
>
>
> I tried the following:
>
>
>
> include_directories(".")
>
>
>
> This doesn't work unfortunately, as relative paths are interpreted as
> relative to the current source directory
> <https://cmake.org/cmake/help/v3.0/command/include_directories.html>
>
>
>
> That is, if this statement is in my top level CMakeLists.txt, located in
> ~/src/project, the include directive added will be "-I ~/src/project/.",
> whereas I'm trying to add "-I ."
>
>
>
> The reason for my wanting this is that we have sources for a single
> library in several subdirs.
>
>
>
> include_directories(${CMAKE_CURRENT_SOURCE_DIR})
>
>
>
> add_library(
>
> foo
>
> STATIC
>
> foo.cpp
>
> bar/bar.cpp
>
> )
>
>
>
> In bar/bar.cpp, I have a relative include
>
>
>
> #include "../foo.h"
>
>
>
> With the above setup, this will fail to compile as only ~/src/project/foo
> will be added to the include paths.
>
>
>
> I'm aware that if I explicitly added the path it would work
>
>
>
> include_directories(${CMAKE_CURRENT_SOURCE_DIR}/bar)
>
>
>
> However, if there is a way to get -I. added to my include path, that's
> what I'm after.
>
>
>
> TIA
>
> Steve
>
>
>
>
>
> --
>
> 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
>
> https://crascit.com
>
>
>
>
>
>
>
> --
>
> Craig Scott
>
> Melbourne, Australia
>
> https://crascit.com
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20161020/3684a754/attachment.html>


More information about the CMake mailing list