14

I have a simple CMakeLists.txt that looks like this:

CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

PROJECT(calculator)

FIND_PACKAGE(Qt5Core)
FIND_PACKAGE(Qt5Gui)
FIND_PACKAGE(Qt5Widgets)

SET(CMAKE_AUTOMOC ON)
SET(CMAKE_INCLUDE_CURRENT_DIR ON)

SET(calculator_SOURCES main.cpp mainwindow.cpp)
SET(calculator_HEADERS mainwindow.h)
SET(calculator_FORMS mainwindow.ui)

QT5_WRAP_CPP(calculator_HEADERS_MOC ${calculator_HEADERS})
QT5_WRAP_UI(calculator_FORMS_HEADERS ${calculator_FORMS})

ADD_LIBRARY(calculator_CONFIG ${calculator_HEADERS_MOC} ${calculator_FORMS_HEADERS})
QT5_USE_MODULES(calculator_CONFIG Widgets)

ADD_EXECUTABLE(calculator ${calculator_SOURCES} ${calculator_CONFIG})
QT5_USE_MODULES(calculator Core Gui Widgets)

And when I try to build the project using cmake -G "Unix Makefiles" and subsequently make, the console says that ui_mainwindow.h is not found. What is the problem? Is it my cmake file?


Full Error Output:

[ 22%] Building CXX object CMakeFiles/calculator.dir/mainwindow.cpp.o
/home/centurion/Code/cpp/calculator/mainwindow.cpp:2:27: fatal error: ui_mainwindow.h: No such file or directory
 #include "ui_mainwindow.h"
                           ^
compilation terminated.
make[2]: *** [CMakeFiles/calculator.dir/mainwindow.cpp.o] Error 1
make[1]: *** [CMakeFiles/calculator.dir/all] Error 2
make: *** [all] Error 2
RAM
  • 2,257
  • 2
  • 19
  • 41
Kenny Worden
  • 4,335
  • 11
  • 35
  • 62

3 Answers3

20

I was running in the same issue with cmake 3.2.2. Try using

SET(CMAKE_AUTOUIC ON)  

if the ui files are not generated. Maybe the default behaviour changed recently?

Goldfishslayer
  • 430
  • 4
  • 10
2
  1. Use lower-case CMake commands. That has been the sane convention for years.

  2. Why are you using both AUTOMOC and qt5_wrap_cpp? AUTOMOC is designed to replace the macro. http://www.cmake.org/cmake/help/v3.0/manual/cmake-qt.7.html#automoc

  3. If using CMake 2.8.11 or later, then don't use qt5_use_modules. I wrote that as a stop-gap hack until CMake 2.8.11 was released. The target_link_libraries command does what qt5_use_modules does, but better and more-generically. http://doc-snapshot.qt-project.org/qt5-5.3/cmake-manual.html

  4. The library has no sources of its own and is not used. You're clearly 'doing it wrong' here. Move the ${calculator_FORMS_HEADERS} variable usage to the executables sources. Then after addressing point 2, remove the library.

Kevin
  • 16,549
  • 8
  • 60
  • 74
steveire
  • 10,694
  • 1
  • 37
  • 48
  • 2
    Don't ask me; I followed tutorials to get me to this point. Thanks for your answer. – Kenny Worden Sep 17 '14 at 02:25
  • 17
    This answer sounds a little condescending; I hope I'm just misreading (I didn't -1). CMake itself is absurdly hard to reason about, and the special Qt magic doesn't make it any easier. – weberc2 Apr 03 '15 at 21:57
  • 1
    I arrived at this question looking for something else but #2 intrigued me so I removed the qt5_wrap_cpp from my CMakeLists.txt and it broke with unresolved external symbol errors. Added it back and it works fine. I have CMAKE_AUTOMOC set to ON. I did end up remove the automoc setting as it seems to do nothing. – Chuck Claunch May 11 '15 at 22:15
  • The doc link in 3. is dead. – Stefan Fabian Oct 03 '17 at 12:40
  • I landed here becuase of a similar issue, but none of this not "sane" stuff was in use, so I must have found more recent examples/tutorials than the poor soul that asked this question. As someone new to CMake, but not new to modern software and build procs and tooling, it is not easy to understand. On top of that, we all know no one seems to scrub the net of obsolete "guides" etc. – Thomas Carlisle Jan 24 '19 at 19:35
1

I have encountered the same issue on Mac OS X. Ui form header file is not generated.

I have solved my problem by generating manually .h file with QtDesigner. When changes are made on ui form, header file is well generated.

Note: if I add some others ui forms, headers are generated automatically without needed to generate header file manually for these others ui.

EDIT: header file is well generated at first build only if it isn't used into cpp code.

Clément
  • 11
  • 3