0

I'm at the very first day of Qt + Cmake and Conan, trying to make things work. I'm not using qmake because I'll integrate everything into a bigger project using cmake.

By following QT's tutorial, I figured out that I need to compile QT macros, and for that there's a useful AUTOMOC CMake property, as suggested here.

The point is that it's making me fail cmake builds.

My conanfile.txt:

[requires]
qt/5.15.2

[generators]
cmake

My CMakeLists.txt:

cmake_minimum_required(VERSION 3.20)
project(qttest)

set(CMAKE_CXX_STANDARD 20)
set_target_properties(${PROJECT_NAME} PROPERTIES AUTOMOC TRUE)


set (PROJECT_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)

include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_executable(${PROJECT_NAME} src/main.cpp)

target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})

with the following output:

CMake Warning (dev) in CMakeLists.txt:
  AUTOGEN: No valid Qt version found for target qttest.  AUTOMOC disabled.
  Consider adding:

    find_package(Qt<QTVERSION> COMPONENTS Core)

  to your CMakeLists.txt file.
This warning is for project developers.  Use -Wno-dev to suppress it.

ouch, but adding the find doesn't make things better:

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

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

    Qt5Config.cmake
    qt5-config.cmake

Actually the project compiles, Qt is there in its conan dir:

matteo@MacBook-Pro-de-matteo 96a68a791abfc7a246f2bc28aa2f6fc210be0f9f % cd ~/.conan/data/qt
matteo@MacBook-Pro-de-matteo qt % ls
5.15.2  6.2.2
matteo@MacBook-Pro-de-matteo qt %

how could I enable it, or make things easier to compile it along with cmake?

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Bertuz
  • 2,390
  • 3
  • 25
  • 50
  • You can set_target_properties() only on a valid target, qttest is not. Either set [CMAKE_AUTOMOC](https://cmake.org/cmake/help/latest/prop_tgt/AUTOMOC.html) globally or on a valid target – chehrlic Jan 13 '22 at 06:03

1 Answers1

0

You need to tell CMake, where to find Qt. So, as CMake suggests by itself:

find_package(Qt5 COMPONENTS Core)

for the most basic stuff, you might want to add some of the other components later.

Depending on the system you are working on and your Qt installation, you need to tell CMake where to search for the package configuration files (second error message). CMake has some default directories, where it looks for these files, but obviously, there is none. On Linux, this can be solved by installing Qt with a package manager (this will install the CMake config files to one of the Qt default locations). If you are on Windows or if you installed Qt to a different location, this can be solved by providing the path with the PREFIX_PATH-variable.

cmake -B $BUILD_DIR -S $SOURCE_DIR -DCMAKE_PREFIX_PATH=$QT_INSTALL_PATH/5.15.2/$ARCHITECTURE $OTHER_OPTIONS

(You can have different versions installed in the same installation path, that's why Qt adds an other folder with the version number. And you can have different compilers/architectures. On Windows for example, you might have a mingw73_32 and a msvc2017 folder to choose.)

As already mentioned in the comments, a project is no CMake target. CMake targets are either libraries (add_library), executables (add_executable) or custom targets (add_custom_target); the project is not. If you want to set the AUTOMOC property target wise, that's ok and even suggested by CMake, but you can also set it globally by using:

set(CMAKE_AUTOMOC ON)