<div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, May 5, 2018 at 1:33 AM, Francis Giraldeau <span dir="ltr"><<a href="mailto:francis.giraldeau@gmail.com" target="_blank">francis.giraldeau@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>> Hi,</div><div>> </div><div>> I am fetching and building SDL2 using FetchContent and then using</div><div>> target_link_libraries(MyExe SDL2) in the hopes that the required include</div><div>> directories and libraries will be added populated properly. The example</div><div>> project can be found here:</div><div>> </div><div>> <a href="https://github.com/samaursa/cmake_fetch_content_SDL2" target="_blank">https://github.com/samaursa/<wbr>cmake_fetch_content_SDL2</a></div><div>> </div><div>> Unfortunately, it seems that I have to manually specify the include</div><div>> directories. Although target_link_directories(...) does take care of the</div><div>> linking against the correct libraries. When it comes to my own CMake</div><div>> enabled libraries, I do not have to specify the include directories.</div><div><br></div><div>The SDL add_library() only specify the include directory for install target: </div><div><br></div><div><div> target_link_libraries(SDL2 ${EXTRA_LIBS} ${EXTRA_LDFLAGS})</div><div> target_include_directories(<wbr>SDL2 PUBLIC $<INSTALL_INTERFACE:include/<wbr>SDL2>)</div></div><div><br></div><div>They themselves include directories using the old global includes instead of target specific includes:</div><div><br></div><div> include_directories(${SDL2_<wbr>BINARY_DIR}/include ${SDL2_SOURCE_DIR}/include)</div><div><br></div><div>There should be a target_include_directories(<wbr>SDL2 PUBLIC <$BUILD_INTERFACE:${SDL2_<wbr>SOURCE_DIR}/include), but that must be in the same CMakeLists that defines the library, which require changing the SDL's CMakeLists. </div></div></blockquote><div><br></div><div><br></div><div>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 <a href="https://gitlab.kitware.com/cmake/cmake/issues/17943#note_405237">recently agreed</a> 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.</div><div><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>So, one solution could be to create an imported target to attach this path and l ink to it instead:</div><div><br></div><div><div> add_library(SDL2x INTERFACE IMPORTED)</div><div> set_target_properties(SDL2x PROPERTIES INTERFACE_INCLUDE_DIRECTORIES "${sdl2_SOURCE_DIR}/include")</div><div> set_target_properties(SDL2x PROPERTIES INTERFACE_LINK_LIBRARIES SDL2)</div></div><div><br></div><div>Tested with CMake 3.11. However, to make it work, I had to add OpenGL:</div><div><br></div><div><div> find_package(OpenGL REQUIRED)</div></div><div><div> target_link_libraries(testExe SDL2x OpenGL::GL) </div></div></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>Install fails, but that's another issue (the symlink is created in the wrong directory) and this should be reported to SDL I guess.</div><div><div><br></div><div>CMake Error at _deps/sdl2-build/cmake_<wbr>install.cmake:188 (file):</div><div> file INSTALL cannot find</div><div> "[...]/build/_deps/sdl2-build/<wbr>libSDL2.so".</div><div>Call Stack (most recent call first):</div><div> cmake_install.cmake:42 (include)</div></div><span class="HOEnZb"><font color="#888888"><div><br></div></font></span></div></blockquote></div><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr">Craig Scott<br><div>Melbourne, Australia</div><div><a href="https://crascit.com" target="_blank">https://crascit.com</a><br></div></div></div></div></div></div></div>
</div></div>