Re: Why isn't target_link_libraries not enough in some cases?

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Why isn't target_link_libraries not enough in some cases?

Francis Giraldeau
> Hi,
> I am fetching and building SDL2 using FetchContent and then using
> target_link_libraries(MyExe SDL2) in the hopes that the required include
> directories and libraries will be added populated properly. The example
> project can be found here:
> Unfortunately, it seems that I have to manually specify the include
> directories. Although target_link_directories(...) does take care of the
> linking against the correct libraries. When it comes to my own CMake
> enabled libraries, I do not have to specify the include directories.

The SDL add_library() only specify the include directory for install target: 

  target_link_libraries(SDL2 ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
  target_include_directories(SDL2 PUBLIC $<INSTALL_INTERFACE:include/SDL2>)

They themselves include directories using the old global includes instead of target specific includes:

  include_directories(${SDL2_BINARY_DIR}/include ${SDL2_SOURCE_DIR}/include)

There should be a target_include_directories(SDL2 PUBLIC <$BUILD_INTERFACE:${SDL2_SOURCE_DIR}/include), but that must be in the same CMakeLists that defines the library, which require changing the SDL's CMakeLists. 

So, one solution could be to create an imported target to attach this path and l ink to it instead:

  add_library(SDL2x INTERFACE IMPORTED)
  set_target_properties(SDL2x PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${sdl2_SOURCE_DIR}/include")
  set_target_properties(SDL2x PROPERTIES INTERFACE_LINK_LIBRARIES SDL2)

Tested with CMake 3.11. However, to make it work, I had to add OpenGL:

  find_package(OpenGL REQUIRED)
  target_link_libraries(testExe SDL2x OpenGL::GL)

Install fails, but that's another issue (the symlink is created in the wrong directory) and this should be reported to SDL I guess.

CMake Error at _deps/sdl2-build/cmake_install.cmake:188 (file):
  file INSTALL cannot find
  "[...]/build/_deps/sdl2-build/libSDL2.so".
Call Stack (most recent call first):
  cmake_install.cmake:42 (include)

Francis
--
Francis Giraldeau

--

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:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: Why isn't target_link_libraries not enough in some cases?

Craig Scott-3


On Sat, May 5, 2018 at 1:33 AM, Francis Giraldeau <[hidden email]> wrote:
> Hi,
> I am fetching and building SDL2 using FetchContent and then using
> target_link_libraries(MyExe SDL2) in the hopes that the required include
> directories and libraries will be added populated properly. The example
> project can be found here:
> Unfortunately, it seems that I have to manually specify the include
> directories. Although target_link_directories(...) does take care of the
> linking against the correct libraries. When it comes to my own CMake
> enabled libraries, I do not have to specify the include directories.

The SDL add_library() only specify the include directory for install target: 

  target_link_libraries(SDL2 ${EXTRA_LIBS} ${EXTRA_LDFLAGS})
  target_include_directories(SDL2 PUBLIC $<INSTALL_INTERFACE:include/SDL2>)

They themselves include directories using the old global includes instead of target specific includes:

  include_directories(${SDL2_BINARY_DIR}/include ${SDL2_SOURCE_DIR}/include)

There should be a target_include_directories(SDL2 PUBLIC <$BUILD_INTERFACE:${SDL2_SOURCE_DIR}/include), but that must be in the same CMakeLists that defines the library, which require changing the SDL's CMakeLists. 


That's not actually true. You can call target_include_directories() on any target, not just those created in the current scope. In fact, all of the target_...() commands except target_link_libraries() can do this (and it was recently agreed that we should remove the restriction on target_link_libraries() too). This means if a project like SDL fails to set INTERFACE properties for header search paths, compiler defines, etc. you can add them yourself from outside the project. Obviously it's better if the project itself can be updated to do it, but in the meantime your own project can add the missing things.

 

So, one solution could be to create an imported target to attach this path and l ink to it instead:

  add_library(SDL2x INTERFACE IMPORTED)
  set_target_properties(SDL2x PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${sdl2_SOURCE_DIR}/include")
  set_target_properties(SDL2x PROPERTIES INTERFACE_LINK_LIBRARIES SDL2)

Tested with CMake 3.11. However, to make it work, I had to add OpenGL:

  find_package(OpenGL REQUIRED)
  target_link_libraries(testExe SDL2x OpenGL::GL) 

Install fails, but that's another issue (the symlink is created in the wrong directory) and this should be reported to SDL I guess.

CMake Error at _deps/sdl2-build/cmake_install.cmake:188 (file):
  file INSTALL cannot find
  "[...]/build/_deps/sdl2-build/libSDL2.so".
Call Stack (most recent call first):
  cmake_install.cmake:42 (include)


--
Craig Scott
Melbourne, Australia

--

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:
https://cmake.org/mailman/listinfo/cmake