CMake & QT5 - QT5_WRAP_UI not generating ui header

2019-01-27 15:54发布

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

3条回答
倾城 Initia
2楼-- · 2019-01-27 16:37
  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.

查看更多
我只想做你的唯一
3楼-- · 2019-01-27 16:37

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?

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-01-27 16:38

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.

查看更多
登录 后发表回答