Implementing Qt project through CMake
Your script has several errors, also a few things can be improved. After changes it will be looks like:
cmake_minimum_required(VERSION 3.0.2)
project(MyProject)
find_package(Qt5Widgets)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
add_library(mainwindow mainwindow.cpp)
target_link_libraries (mainwindow Qt5::Widgets)
add_executable(MyProject main.cpp)
target_link_libraries (MyProject mainwindow)
Errors:
Wrong
add_executable
directive. You try to add a library, but for this purpose you need to usetarget_link_libraries
. So instead of:add_executable(Cmake main.cpp mainwindow)
You need:
add_executable(Cmake main.cpp)
target_link_libraries (Cmake mainwindow)And one more mistake is missing
*.cpp
files in theadd_library
directive:add_library(mainwindow mainwindow.cpp ${Cmake_hdr_moc} ${Cmake_form_hdr})
Recommendations:
Also setting version of
CMake
would be appropriate. If you useCMAKE_AUTOMOC
you need a version not less than 2.8.6, and if you useCMAKE_AUTOUIC
you need a version not less than 3.0.2:cmake_minimum_required(VERSION 3.0.2)
Using
qt5_wrap_cpp
withCMAKE_AUTOMOC
isn't necessary.When you use
CMAKE_AUTOMOC
usageCMAKE_AUTOUIC
instead ofqt5_wrap_ui
will be more appropriate.This script is correct for the project with the following structure in the file system:
Project
├── CMakeLists.txt
├── main.cpp
├── mainwindow.cpp
├── mainwindow.h
└── mainwindow.uiIf you have another structure you should use
include_directories
as was mentioned by @steveire.(UPD) Since, I've written this answer, I suggested it several times for beginners who try to meet with
Qt
throughCMake
. They complain of an inappropriate name of the project - "Cmake". For beginners who just meet withCMake
is difficult to realize where cmake - is just part of the project name (and isn't mandatory) and where cmake is part of a directive (and is mandatory). So I'd like to replace the name of the project from "Cmake" to "MyProject". This reduces connection between question and answer, but on the other hand this makes the answer more friendly for beginners.(UPD) As was mentioned by @Erik Sjölund
qt5_use_modules
is obsolete andtarget_link_libraries
should be used instead.
Note: Personally I have had unsuccessful experience with CMAKE_AUTOMOC
; it's good for a simple project with plain structure. I've had problems with a case when my include files were stored into a separate directory:
.
├── include
│ └── QtClass.h
└── src
└── QtClass.cpp
And when files with the same name were into different subdirectories:
.
├── NamespaceA
│ ├── QtClass.cpp
│ └── QtClass.h
└── NamespaceB
├── QtClass.cpp
└── QtClass.h
Finally:
This is a suggestion based on my personal opinion, but I'd like to propose more explicit version of the script without usage of CMAKE_AUTOMOC
and CMAKE_AUTOUIC
, it's more verbose but in other hand you have more control:
cmake_minimum_required (VERSION 2.8.12)
project (MyProject)
find_package (Qt5Widgets)
set (MyProjectLib_src ${PROJECT_SOURCE_DIR}/mainwindow.cpp)
set (MyProjectLib_hdr ${PROJECT_SOURCE_DIR}/mainwindow.h)
set (MyProjectLib_ui ${PROJECT_SOURCE_DIR}/mainwindow.ui)
set (MyProjectBin_src ${PROJECT_SOURCE_DIR}/main.cpp)
qt5_wrap_cpp(MyProjectLib_hdr_moc ${MyProjectLib_hdr})
qt5_wrap_ui (MyProjectLib_ui_moc ${MyProjectLib_ui})
include_directories (${PROJECT_SOURCE_DIR})
include_directories (${PROJECT_BINARY_DIR})
add_library (MyProjectLib SHARED
${MyProjectLib_src}
${MyProjectLib_hdr_moc}
${MyProjectLib_ui_moc}
)
target_link_libraries (MyProjectLib Qt5::Widgets)
add_executable(MyProject ${MyProjectBin_src})
target_link_libraries (MyProject MyProjectLib)
The complete version of the projects source code is available at GitLab.
Importing a CMake project in QtCreator
Normally you just do "Open Project" and then select the CMakeLists.txt as the project file. QtCreator will then ask you to run CMake once (to generate a CodeBlocks build file, which is then interpreted by QtCreator). After that you have native support for your CMake project.
It even works with rather complicated CMake scripts.
If you observe hangs etc., you should probably file a bug report with the QtCreator project.
Using Cmake with Qt Creator
You can add files using glob expression in your CMakeLists.txt, like this:
file(GLOB SRC . *.cpp)
add_executable (your_exe_name ${SRC})
Cmake will pick your new cpp files next time you run it and QtCreator will show them in the project browser.
Update
This solution may be useful but as noted in comments - this is not a good practice. Every time somebody add new source file and commit changes, you need to rerun cmake to build all the sources. Usually I just touch
one of the CMakeLists.txt files if my build is broken after I pool recent changes from repository. After that make
will run cmake
automatically and I didn't need to run it by hands. Despite of that I think that explicit source lists in CMakeLists.txt is a good thing, they called thing CMake Lists for a reason.
Related Topics
Why Is the C++ Stl Is So Heavily Based on Templates? (And Not on *Interfaces*)
Polymorphic_Allocator: When and Why Should I Use It
Restrict C++ Template Parameter to Subclass
Why Can't "Transform(S.Begin(),S.End(),S.Begin(),Tolower)" Be Complied Successfully
How to Use an Array as Map Value
Simulating Key Press Events in MAC Os X
C++ How to Generate All the Permutations of Function Overloads
How to Print the Full Value of a Long String in Gdb
Converting Float Values from Big Endian to Little Endian
Error: Member Access into Incomplete Type:Forward Declaration Of