Search code examples
c++qtcmake

CMake find_package can't find WebSockets


According to https://blog.kitware.com/cmake-finding-qt5-the-right-way/, the method find_package(Qt5 COMPONENTS Core Qml Quick Svg Qt5WebSockets REQUIRED) allows me to load lots of QT5 packages while being able to set the QT root only once by setting the Qt5_DIR variable. I'm trying to compile a minimal CMake project which is listed below, using this technique. You can see that WebSockets isn't found. What is the problem?

PS: My home directory at /home/lz/Qt5.11.2 indeed has a new Qt installation that Qt installer just installed for me, and it indeed has libwebsockets and websocket include files.

My CMakeLists.txt:

find_package(Qt5 COMPONENTS Core Qml Quick Svg Qt5WebSockets REQUIRED)

How do I run it:

cmake -DQt5_DIR=/home/lz/Qt5.11.2 .
-- The C compiler identification is GNU 7.2.0
-- The CXX compiler identification is GNU 7.2.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
CMake Error at /usr/lib/x86_64-linux-gnu/cmake/Qt5/Qt5Config.cmake:28 (find_package):
  Could not find a package configuration file provided by "Qt5Qt5WebSockets"
  with any of the following names:

    Qt5Qt5WebSocketsConfig.cmake
    qt5qt5websockets-config.cmake

  Add the installation prefix of "Qt5Qt5WebSockets" to CMAKE_PREFIX_PATH or
  set "Qt5Qt5WebSockets_DIR" to a directory containing one of the above
  files.  If "Qt5Qt5WebSockets" provides a separate development package or
  SDK, be sure it has been installed.
Call Stack (most recent call first):
  CMakeLists.txt:1 (find_package)


CMake Warning (dev) in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.9)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring incomplete, errors occurred!
See also "/home/lz/c/CMakeFiles/CMakeOutput.log".

PS: without WebSockets cmake 'compiles' just fine, so the WebSockets is the only module not found

UPDATE:

After the help below, I'm still having an error related to WebSocket:

/home/lz/orwell/react-native-desktop-orwell/ReactQt/runtime/src/websocketmodule.cpp:12:10: fatal error: QWebSocket: No such file or directory
 #include <QWebSocket>

and now I'm successfully using my QT, as you can see in the verbose output:

[ 50%] Building CXX object CMakeFiles/m.dir/main.o
/usr/bin/c++  -DQT_CORE_LIB -DQT_GUI_LIB -DQT_NO_DEBUG -DQT_WIDGETS_LIB -isystem /home/lz/Qt5.11.2/5.11.2/gcc_64/include -isystem /home/lz/Qt5.11.2/5.11.2/gcc_64/include/QtWidgets -isystem /home/lz/Qt5.11.2/5.11.2/gcc_64/include/QtGui -isystem /home/lz/Qt5.11.2/5.11.2/gcc_64/include/QtCore -isystem /home/lz/Qt5.11.2/5.11.2/gcc_64/./mkspecs/linux-g++  -fPIC -std=gnu++11 -o CMakeFiles/m.dir/main.o -c /home/lz/c/main.cpp
/home/lz/c/main.cpp:1:10: fatal error: QWebSocket: No such file or directory
 #include <QWebSocket>
          ^~~~~~~~~~~~
compilation terminated.

Solution

  • Change the line in your CMakeLists.txt to

    find_package(Qt5 COMPONENTS Core Qml Quick Svg WebSockets REQUIRED)
    

    You don't need to add "Qt5" prefix to the name of the component using this syntax.