4

I have a library which uses Qt5. I build it with this CMakeLists.txt file:

cmake_minimum_required (VERSION 3.0)

project (testLib LANGUAGES CXX) 

set(CMAKE_AUTOMOC ON)  
set(CMAKE_AUTOUIC ON)     
set(CMAKE_INCLUDE_CURRENT_DIR ON) 

find_package(Qt5 REQUIRED Widgets)  

add_library(testLib SHARED
    src/TestClass.cpp
    src/TestClass.h
)

target_include_directories(testLib PUBLIC 
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)    

target_link_libraries(testLib PUBLIC Qt5::Widgets)

export(TARGETS testLib FILE testLib-exports.cmake)

Now I try to link an executable against this library in the build path. This is what I've tried so far:

cmake_minimum_required (VERSION 3.0)

project(TestProject)

add_executable(myexec src/main.cpp )
include(/path/to/testLib-exports.cmake)

target_link_libraries(myexec testLib)

I am getting this error:

Target "myexec" links to target "Qt5::Widgets" but the target was not found. Perhaps a find_package() call is missing for an IMPORTED target, or an ALIAS target is missing?

I don't want to explicitly use find_package() in myexec's cmake file, but want it to be transitive. How should I link against testLib then? If the answer is not in the build path it is fine.


EDIT: I changed the export line to:

export(TARGETS testLib FILE testLib-exports.cmake EXPORT_LINK_INTERFACE_LIBRARIES)

This seems to be exactly what I need, but the generated files with and without EXPORT_LINK_INTERFACE_LIBRARIES are identical. Both if them say

This file does not depend on other imported targets which have been exported from the same project but in a separate export set.

As far as I understand, it should use testLib's target LINK_INTERFACE_LIBRARIES property, but I noticed that it is NOTFOUND. maybe this is the problem? However, INTERFACE_LINK_LIBRARIES has Qt5::Widgets.

Utka
  • 49
  • 6
  • 1
    You may consider using Qt's `qmake`, or even simple use Qt Creator to handle the compilation. – Tom de Geus Apr 06 '17 at 16:38
  • @TomdeGeus The dependancy on Qt is optional, so I want to stick to cmake. I think the problem is - it doesn't understand targets imported to testLib, such as Qt5::widgets, if I include testLib this way. – Utka Apr 06 '17 at 16:45
  • Have you tried to export `QT5::Widgets` too? – Tsyvarev Apr 06 '17 at 21:42
  • @Tsyvarev how shall I export it? – Utka Apr 07 '17 at 09:28
  • Within your `export(TARGETS)` call: `export(TARGETS testLib QT5::Widgets FILE testLib-exports.cmake)`. – Tsyvarev Apr 07 '17 at 09:52
  • @Tsyvarev I added `EXPORT_LINK_INTERFACE_LIBRARIES` to the export command, but it didn't change anything. Inside testLib-exports.cmake there are lines: # This file does not depend on other imported targets which have # been exported from the same project but in a separate export set. – Utka Apr 07 '17 at 10:15
  • @Tsyvarev I did `export(TARGETS testLib QT5::Widgets FILE testLib-exports.cmake)`, it says `CMake Error (export): export given target "QT5::Widgets" which is not built by this project` – Utka Apr 07 '17 at 10:17
  • @Tsyvarev I don't know if this is related, but generated testLib-exports.cmake has a line `cmake_policy(VERSION 2.6)`. Is this normal? Why would the 3.0 version generate 2.6? – Utka Apr 07 '17 at 10:22
  • 1
    Related: http://stackoverflow.com/questions/30987311/exporting-an-imported-library (similar error when trying to export imported library), https://public.kitware.com/Bug/view.php?id=14311 (installation of imported library). – Tsyvarev Apr 07 '17 at 11:38
  • Related: https://cmake.org/pipermail/cmake/2016-June/063773.html Did someone found a solution for this without adding lots of boilerplate CMake code? I've run into the exact same problem using both external (from 3rd-party vendors) and internal CMake package configs. – Florian Wolters Jun 27 '17 at 15:01

0 Answers0