AUTOMOC set to true makes fail cmake build - c++

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?

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)

Related

How to find (configure) Qt for cmake project of iOS?

I install Qt via online installer on my macOS (Qt for iOS and macOS).
And qmake project works just fine, now I need compile with cmake Qt project:
cmake_minimum_required(VERSION 3.5)
project(demo LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(Qt5 COMPONENTS Core Quick REQUIRED)
And I can not find Qt.
Direct build configuration:
cmake .. -GXcode -DQt5_DIR=/Users/user/Qt/5.14.1/clang_64/lib/cmake/Qt5
...
-- Configuring done
-- Generating done
-- Build files have been written to:
Cross-compiling for iOS:
cmake .. -GXcode -DCMAKE_SYSTEM_NAME=iOS -DQt5_DIR=/Users/user/Qt/5.14.1/ios/lib/cmake/Qt5
...
CMake Error at /Users/user/Qt/5.14.1/ios/lib/cmake/Qt5/Qt5Config.cmake:28 (find_package):
Could not find a package configuration file provided by "Qt5Core" with any
of the following names:
Qt5CoreConfig.cmake
qt5core-config.cmake
Add the installation prefix of "Qt5Core" to CMAKE_PREFIX_PATH or set
"Qt5Core_DIR" to a directory containing one of the above files. If
"Qt5Core" provides a separate development package or SDK, be sure it has
been installed.
Obviously Qt5CoreConfig.cmake is just fine and it is in relatively the same place as for "direct" compilation, but for some reason cmake can not find it
$ find /Users/user/Qt/5.14.1/ios/lib/cmake/ -name Qt5CoreConfig.cmake
/Users/user/Qt/5.14.1/ios/lib/cmake//Qt5Core/Qt5CoreConfig.cmake
How can I fix this?
When building for iOS CMAKE_SYSROOT is set to iPhoneOS.sdk path.
find_package:
The CMAKE_SYSROOT variable can also be used to specify exactly one directory to use as a prefix. Setting CMAKE_SYSROOT also has other effects. See the documentation for that variable for more.
These variables are especially useful when cross-compiling to point to the root directory of the target environment and CMake will search there too. By default at first the directories listed in CMAKE_FIND_ROOT_PATH are searched, then the CMAKE_SYSROOT directory is searched, and then the non-rooted directories will be searched.
So, adding the option -DCMAKE_FIND_ROOT_PATH=/Users/user/Qt/5.14.1/ios will help.

Specifying Build Options for External CMake Dependency

I am in the process of writing my project's CMakeLists.txt file. My project relies on an external dependency, GLFW, luckily for me GLFW also uses CMake for its build system. I am using find_package() and git submodules to handle dependency management, currently this is how my CMakeLists.txt file looks like:
# CMake version
cmake_minimum_required(VERSION 3.0)
# project name
project(CHIP-8)
# dependency management
find_package(OpenGL REQUIRED)
find_package(glfw)
if(NOT glfw)
message("Unable to locate glfw.")
message("Cloning the repository.")
execute_process(
COMMAND
git submodule update --init -- libs
WORKING_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR}
)
message("Building glfw.")
add_subdirectory(libs/glfw)
endif()
From my limited understanding of CMake the command add_subdirectory(libs/glfw) runs CMake on the recently cloned glfw sub-module. The problem is a lot of unnecessary features are part of the default build for GLFW such as examples, unit tests, and documentation.
The documentation for compiling GLFW lists the CMake options for disabling these features GLFW_BUILD_EXAMPLES, GLFW_BUILD_TESTS, and GLFW_BUILD_DOCS.
I have two questions, the first being how can I specify these options, and the second is the argument I pass for find_package accurate (I do have GLFW installed on my system yet every time I build it still attempts to clone and build the repository).
Found my answer in a similar question.
All I had to do was add:
option(GLFW_BUILD_EXAMPLES OFF)
option(GLFW_BUILD_TESTS OFF)
option(GLFW_BUILD_DOCS OFF)
right before add_subdirectory(libs/glfw).

How do I use cmake to build external libraries as well as my own application?

I am trying to build a cross platform OpenGL application, which means building and including multiple libraries (glfw, glbinding, glm, etc.) Because my application is cross platform, it makes sense to use cmake to generate all the build scripts and not have to muck about with them myself. I am attempting to target Windows and Linux specifically.
A main feature that I need is that the libraries I need are not installed on the host system. Furthermore, they cannot be installed (due to administrative reasons). What I need is to build these libraries and then build my application.
I am mostly working on Windows using Visual Studio 2017, which has cmake support included. Currently, I have attempted to build these libraries myself, however I am having many issues getting find_package to do the right thing.
My directory structure looks like this:
project/
|-src/
|- my sources for my application
|-include/
|- my header files
|-external/
|-glfw-3.2.1/
|-glbinding-2.1.4/
|-glfw-build/
|-glbinidng-build/
So I am attempting to build the external libraries and use them in my application. I am also attempting to follow cmake best practices. My CMakeLists.txt currently looks like this:
cmake_minimum_required(VERSION 3.5)
project(glTestProj)
set(CMAKE_PREFIX_PATH "external/")
find_package(glfw3 3.2 REQUIRED)
find_package(glbinding REQUIRED)
add_executable(glTest src/main.cpp)
target_compile_features(glTest PRIVATE cxx_std_17)
target_compile_options(glTest PRIVATE -Wall -Wextra)
target_link_libraries(
glTest
glfw
glbinding::glbinding
)
The libraries in question (glfw and glbinding) both have instructions on including them via cmake, however I am running into this issue:
CMake Error at CMakeLists.txt:6 (find_package):
By not providing "Findglfw3.cmake" in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by "glfw3", but
CMake did not find one.
Could not find a package configuration file provided by "glfw3" (requested
version 3.2) with any of the following names:
glfw3Config.cmake
glfw3-config.cmake
Add the installation prefix of "glfw3" to CMAKE_PREFIX_PATH or set
"glfw3_DIR" to a directory containing one of the above files. If "glfw3"
provides a separate development package or SDK, be sure it has been
installed.

CMake file cannot locate Qt charts module

I'm trying to start writing my Qt project inside JetBrains' Clion but I need to link some libraries in my Cmake file first. There's no problem when trying to find packages like Qt5Core, Qt5Widgets, Qt5Gui but when it come to finding Qt5Charts an error is thrown:
By not providing "FindQt5Charts.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file
provided by "Qt5Charts", but CMake did not find one.
Could not find a package configuration file provided by "Qt5Charts"
with any of the following names:
Qt5ChartsConfig.cmake
qt5charts-config.cmake
Add the installation prefix of "Qt5Charts" to CMAKE_PREFIX_PATH or
set "Qt5Charts_DIR" to a directory containing one of the above
files. If "Qt5Charts" provides a separate development package or
SDK, be sure it has been installed.
This is my CMake file right now.
All packages are installed via the Qt's Linux(ubuntu) maintanence tool.
Any ideas how to help Cmake find the Charts module ?
Using the following and see if it helps:
sudo apt install libqt5charts5-dev
Src: https://stackoverflow.com/a/46765025
Typically when including Qt5 in a project I use the follow basic script for CMake, though I should note I haven't tested this on Linux.
cmake_minimum_required(VERSION 3.10.0 FATAL_ERROR)
project(<YOUR_PROJECT_NAME>)
find_package(Qt5 REQUIRED COMPONENTS Core Gui Widgets Charts)
# set your project sources and headers
set(project_sources src/blah.cpp)
set(project_headers include/headers/blah.h)
# wrap your qt based classes with qmoc
qt5_wrap_cpp(project_source_moc ${project_headers})
# add your build target
add_executable(${PROJECT_NAME} ${project_sources} ${project_headers} ${project_source_moc})
# link to Qt5
target_link_libraries(${PROJECT_NAME}
PUBLIC
Qt5::Core
Qt5::Gui
Qt5::Widgets
Qt5::Charts)
# require C++ 14
target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_14)
When configuring your project via cmake, you just need to pass in the path to you qt5 installation directory (cmake variable name is Qt5_DIR) that contains the Qt5Config.cmake file and then cmake should be able to find the rest of the components that you request.
Also double check that Qt5Charts was installed, not sure if it's installed by default.
Maybe try this?
sudo apt install libqt5charts5-dev

Updated Qt library; cannot build anymore

I have a previously working Qt5/cmake project which built fine. I then updated Qt from 5.6 to 5.8. Now cmake cannot find Qt anymore.
This is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.0)
project(TEST)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_PREFIX_PATH "/opt/Qt/Qt5.8.0")
set(QT_QMAKE_EXECUTABLE ${CMAKE_PREFIX_PATH}/5.8/clang_64/bin/qmake)
find_package(Qt5Widgets)
...
This is the error message I get:
CMake Warning at CMakeLists.txt:56 (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.
I followed what written in Qt documentation, so I do not understand what is wrong.
Version details:
- cmake 3.9.0
- Qt 5.8.0
- QMake 3.1
- Mac OS X 10.9.5
Did you check CMake version that is compatible with QT? Also did you check that everything is ok with qmake? Because this is what causes the problems most of the times. Check QT_QMAKE_EXECUTABLE to be pointing in the new QT version, i get problems with new versions of QT all the time.
Fixed setting the following system variables:
QTDIR=/opt/Qt/Qt5.8.0/5.8/clang_64
PATH=$PATH:/opt/Qt/Qt5.8.0/5.8/clang_64/bin
and removing:
set(CMAKE_PREFIX_PATH "/opt/Qt/Qt5.8.0")
from CMakeLists.txt.