c++qtcmakeqt5cmake-modules

Modern CMake 3 with QT5 "Qt5::QtCore" but the target was not found


I am having an issue with CMake and a simple QT example. I am updating my CMake configuration to follow the modern way of doing it, meaning supporting CMake > v3.0.

Here is my CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project("test")

set(CMAKE_AUTOMOC ON)
find_package(Qt5 COMPONENTS Core REQUIRED)
message(STATUS "Qt5 version: ${Qt5_VERSION}")
get_target_property(QtCore_location Qt5::Core LOCATION)
message(STATUS "Qt5 version: ${QtCore_location}")

add_executable(Test main.cpp)
target_link_libraries(Test Qt5::QtCore)

My code sample (main.cpp)

#include <QApplication>
#include <QProgressBar>
#include <QSlider>

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

 // Create a container window
 QWidget window;
 window.setFixedSize(200, 80);

 // Create a progress bar
 // with the range between 0 and 100, and a starting value of 0
 QProgressBar *progressBar = new QProgressBar(&window);
 progressBar->setRange(0, 100);
 progressBar->setValue(0);
 progressBar->setGeometry(10, 10, 180, 30);

 // Create a horizontal slider
 // with the range between 0 and 100, and a starting value of 0
 QSlider *slider = new QSlider(&window);
 slider->setOrientation(Qt::Horizontal);
 slider->setRange(0, 100);
 slider->setValue(0);
 slider->setGeometry(10, 40, 180, 30);

 window.show();

 // Connection
 // This connection set the value of the progress bar
 // while the slider's value changes
 QObject::connect(slider, SIGNAL (valueChanged(int)), progressBar, SLOT (setValue(int)));

 return app.exec();
}

But when I run CMake I get the following error :

build# cmake ..
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Qt5 version: 5.12.3
-- Qt5 version: /opt/Qt5.12.3/5.12/gcc_64/lib/libQt5Core.so.5.12.3
-- Configuring done
CMake Error at CMakeLists.txt:16 (add_executable):
  Target "Test" links to target "Qt5::QtCore" but the target was not found.
  Perhaps a find_package() call is missing for an IMPORTED target, or an
  ALIAS target is missing?


-- Generating done
CMake Generate step failed.  Build files cannot be regenerated correctly.

I don't understand what I am missing. I have followed the cmake documentation example

Thanks for your help,

EDIT 1: Thanks for the comment I fixed the typo issue. Which was Qt5::Core instead of Qt5::QTCore. But now I get this build issue :

Scanning dependencies of target Test_autogen
[ 25%] Automatic MOC for target Test
[ 25%] Built target Test_autogen
Scanning dependencies of target Test
[ 50%] Building CXX object CMakeFiles/Test.dir/Test_autogen/mocs_compilation.cpp.o
[ 75%] Building CXX object CMakeFiles/Test.dir/main.cpp.o
/app/build/main.cpp:1:10: fatal error: QApplication: No such file or directory
 #include <QApplication>
          ^~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/Test.dir/build.make:75: recipe for target 'CMakeFiles/Test.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/Test.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:76: recipe for target 'CMakeFiles/Test.dir/all' failed
make[1]: *** [CMakeFiles/Test.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Solution

  • The solution was based on the comments : Thanks for the comment I fixed the typo issue. Which was Qt5::Core instead of Qt5::QTCore. And for the build issue I added the missing Widgets :

    cmake_minimum_required(VERSION 3.16)
    
    project("test")
    
    set(CMAKE_AUTOMOC ON)
    set(CMAKE_AUTORCC ON)
    set(CMAKE_AUTOUIC ON)
    find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
    
    add_executable(Test main.cpp)
    target_link_libraries(Test Qt5::Core Qt5::Widgets)