12

I'm trying to use Clion IDE to compile a simple program using Qt library, but I can't figure out how to configure CMakeLists.txt file. (I'm not familiar with cmake and toolchain) this is my current CMakeLists.txt file:

cmake_minimum_required(VERSION 3.2)
project(MyTest)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES main.cpp)
add_executable(MyTest ${SOURCE_FILES})

 # Define sources and executable
set(EXECUTABLE_NAME "MySFML")
add_executable(${EXECUTABLE_NAME} main.cpp)



# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
    include_directories(${SFML_INCLUDE_DIR})
    target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()

It's configured to use SFML library with a "FindSFML.cmake" file and it works fine. (I have copied these files from some tutorial) now I want some help regarding proper CMakeLists.txt configuration to compile programs that are using Qt library (it's more helpful if the files and explanations are provided).


P.S: my current OS is manjaro 0.8.13 and all I could find was explaining configurations in windows environment so I was unable to implement those tutorials.

user123
  • 1,060
  • 3
  • 13
  • 29
Kian Ahrabian
  • 911
  • 2
  • 8
  • 20
  • @coincoin what do you mean by calling cmake .. ?? I use Clion to build and run my program not CLI command (if it's what you mean) – Kian Ahrabian Jul 01 '15 at 08:05
  • @coincoin this is my code: [link](http://paste.ubuntu.com/11803793/) and this is clion's output: [link](http://paste.ubuntu.com/11803799/) – Kian Ahrabian Jul 01 '15 at 10:12

2 Answers2

13

In addition to this answer, you can use a simpler syntax:

find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui)

Then, you don't call qt5_use_modules, but instead use the standard command to link:

target_link_libraries(MyTest Qt5::Core Qt5::Widgets Qt5::Gui)
user16217248
  • 3,119
  • 19
  • 19
  • 37
Jean-Michaël Celerier
  • 7,412
  • 3
  • 54
  • 75
9

Your CMake project file is missing the Qt packages. You have to add:

find_package( Qt5Core REQUIRED )
find_package( Qt5Widgets REQUIRED )
find_package( Qt5Gui REQUIRED )

and then

qt5_use_modules( MyTest Core Widgets Gui )
tomvodi
  • 5,577
  • 2
  • 27
  • 38
  • thank you for your answer, finally it works fine!! now my 'CmakeLists.txt' looks like this: [link](http://paste.ubuntu.com/11804067/) – Kian Ahrabian Jul 01 '15 at 11:33
  • @thomas_b correct me if I'm wrong, but if cmake replaces .pro files, how do I add resources to my project? – Michael Sep 14 '15 at 02:08
  • As the CMake manual for Qt (http://doc.qt.io/qt-5/cmake-manual.html) describes, this is done with the `qt5_add_resources` command. Extending the source code of the question, the call would look like this: `qt5_add_resources(SOURCE_FILES my_resources.qrc)` – tomvodi Sep 14 '15 at 06:30