find_package failed to generate project (in Qt) with CMake

4

I'm trying to generate the build configuration from a simple example in Qt using CMake. The example code is this:

#include <QApplication>
#include <QTextEdit>

int main(int argc, char** argv)
{
    QApplication app(argc, argv);

    QTextEdit textEdit;
    textEdit.setText("Olá mundo!");
    textEdit.show();

    return app.exec();
}

And the CMake configuration file is this:

cmake_minimum_required(VERSION 2.8.11)

project (teste)

# Configuracao do Qt
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
find_package(Qt5Core)
find_package(Qt5Widgets)

# Configuracao especifica para o gcc
if(NOT WIN32)
    set(CMAKE_C_FLAGS "-Wall -g")
endif()

# Adiciona todos os fontes na variavel SRCS
file(GLOB SRCS *.cpp *.h)

# Nome do executavel
if(WIN32)
    add_executable(teste WIN32 ${SRCS})
else()
    add_executable(teste ${SRCS})
endif()

# Usa os modulos do Qt 5
target_link_libraries(teste Qt5::Core)
target_link_libraries(teste Qt5::Widgets)

When I build the configuration in Windows (v.10, 64bits), it works fine. However, when I do the same in Ubuntu (v.14.04, 32bits), CMake produces the following error message:

  

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

     

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.

Some additional information:

  • Qt 5.5.1 is properly installed on Ubuntu (in the local /home/luiz/Qt5.5.1 folder).
  • Running the apt-file search Qt5CoreConfig.cmake command, I get the following path: qtbase5-dev: /usr/lib/i386-linux-gnu/cmake/Qt5Core/Qt5CoreConfig.cmake . So I've already tried to add /usr/lib/i386-linux-gnu/cmake/ to the environment variable CMAKE_PREFIX_PATH (as directed the documentation in the 5th paragraph of Getting Started ), but the error continues.
  • I just did not try to individually define the Qt5<Module>_DIR variables because I believe this approach will only give more work than necessary (and maybe there is some other misconfiguration).

Does anyone know where I might be going wrong?

    
asked by anonymous 24.02.2016 / 15:28

1 answer

2

I was able to solve this help for this other thread . There were two problems:

  • Instead of setting the path in an environment variable CMAKE_PREFIX_PATH , it seems that the "most correct" is to do as a variable within the CMake script.
  • The correct path is the installation of Qt, but including the compiler specification (in the case of Ubuntu, gcc).
  • So, I just added the following excerpt to my CMake script, before calling find_package :

    if(WIN32)
        set (CMAKE_PREFIX_PATH "C:\Qt\Qt5.6.0\5.6\msvc2015\")
    elseif(UNIX)
        set (CMAKE_PREFIX_PATH $ENV{HOME}/Qt5.5.1/5.5/gcc/)
    endif()
    

    The setting of the CMAKE_PREFIX_PATH variable was not required in my Windows environment previously because the Qt path was added to the Path. So, to avoid this specific configuration within the CMake script file, the most appropriate is to add the Qt installation path to the environment variable PATH (both Windows and Linux) and not use the variable CMAKE_PREFIX_PATH .

        
    24.02.2016 / 18:40