How to create a cmake header-only library that depends on external header files? is close but different.
I have a single-header library, MyHeaderLib
. In MyHeaderLib/MyHeader.h
I have #include <QString>
, so anyone doing #include "MyHeaderLib/MyHeader.h"
had better have QString
in their path (i.e., Qt5Core to CMake, I think(?)) and it they'll need to link to Qt5Core.
What belongs in my CMakeLists.txt
for MyHeaderLib
? I have
cmake_minimum_required(VERSION 3.12)
add_library(MyHeaderLib INTERFACE)
target_include_directories(MyHeaderLib include/)
# (^ Where include/ contains MyHeaderLib/MyHeader.h)
Anything I try with target_link_libraries(MyHeaderLib
requires INTERFACE
and if I do target_link_libraries(MyHeaderLib INTERFACE Qt5Core)
that doesn't suffice.
Ultimately I got it to work as follows, but I don't understand what is going on:
cmake_minimum_required(VERSION 3.12)
find_package(Qt5Core REQUIRED) # <- Can't be Qt5::Core
add_library(MyHeaderLib INTERFACE)
target_include_directories(MyHeaderLib include/)
# (^ Where include/ contains MyHeaderLib/MyHeader.h)
target_link_libraries(MyHeaderLibrary
INTERFACE
Qt5::Core # <- Can't be Qt5Core
)
I gather the targets with ::
in them are aliases, but I'm perplexed why it needs to be exactly like this. Furthermore, I can't find add_library(Qt5::Core ALIAS Qt5Core)
anywhere. What is going on? Why do I have to find_package(Qt5Core REQUIRED)
and not find_package(Qt5::Core REQUIRED)
and why can't target_link_libraries
take Qt5Core
?