0
votes

Context

Using the below CMakeLists.txt, it build the Qt test project without issues ONLY when included to a parent project like:

RootProject
  +--CMakeLists.txt      // Parent CMake
  +--TestQt
       +--testwidget.cpp 
       +--testwidget.hpp // Empty class, just extends QWidget
       +--CMakeLists.txt // My Test Project CMake

Parent Project just contain:

add_subdirectory( "TestQt" )

As soon as I try to build the "TestQt" project standalone, it just return an error like:

CMake Error at CMakeLists.txt:16 (find_package): By not providing "FindQt5Widgets.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "Qt5Widgets", but CMake did not find one.

Could not find a package configuration file provided by "Qt5Widgets" with any of the following names:

Qt5WidgetsConfig.cmake / qt5widgets-config.cmake

Add the installation prefix of "Qt5Widgets" to CMAKE_PREFIX_PATH or set "Qt5Widgets_DIR" to a directory containing one of the above files. If "Qt5Widgets" provides a separate development package or SDK, be sure it has been installed.

CMAKE_PREFIX_PATH is empty in both cases.

Currently using Debian with a slightly old CMAKE 3.0.2

Question

What is wrong/ missing?

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

set(CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(Qt5Widgets REQUIRED)

include_directories(${Qt5Widgets_INCLUDE_DIRS})
add_definitions(${Qt5Widgets_DEFINITIONS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Qt5Widgets_EXECUTABLE_COMPILE_FLAGS}")


# Project name
project ( "TestQt" )
add_executable( UnitTest_TestQt UnitTest.cpp testwidget.cpp)
target_link_libraries(UnitTest_TestQt Qt5::Widgets)
1
Do a search to find where FindQt5Widgets.cmake is installed, and look to see what are the contents of environment variables CMAKE_PREFIX_PATH and Qt5Widgets_DIRAmadeus
Good catch with Qt5Widgets_DIR: /usr/lib/x86_64-linux-gnu/cmake/Qt5Widgets when in subproject, but Qt5Widgets_DIR-NOTFOUND in standalone. Do you know why is not set in standalone?Adrian Maire
That's confusing: you shouldn't have a project ( "TestQt" ) inside another project.Velkan
As far as I know, 'there can be only one' project. And it must be in the root CMakeLists.txt. If it's not there, it will be added implicitly. Stuff breaks if you call project() in other CMakeLists.txt somewhere down the directory hierarchy.Velkan
I guess there is more in CMakeLlists.txt that you are showing here. The error says that it happens in line 16, but in your CMakeLists.txt, it is on line 6. Try with this small file and look to see if the error continues to happenAmadeus

1 Answers

2
votes

find_package needs project to work properly. Move the line project("TestQt") to the top of the file, right after cmake_minimum_required.