2
我添加了代码来使用QT的MOC可执行文件生成一个文件。当我尝试构建它时,会生成该文件,但它不会被编译。CMake生成的源不编译
find_program (QT_UIC uic)
find_program (QT_MOC moc)
set(TARGET_LIBRARY support_qt_${UNICODE_FLAG}${STATIC_FLAG}${BUILD_TYPE_FLAG})
# Enable the build directory as an include path as well, required for
# qt moc generated files.
set(CMAKE_INCLUDE_CURRENT_DIR ON)
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Gui REQUIRED)
include_directories(${Qt5Widgets_INCLUDES})
include_directories("..")
set(SUPPORT_SOURCE
${CMAKE_CURRENT_SOURCE_DIR}/support_qt_dll_api.h
${CMAKE_CURRENT_SOURCE_DIR}/supportlib_qt_namespace.h
${CMAKE_CURRENT_SOURCE_DIR}/dll_main.cpp
)
if (STATIC_BUILD)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBUILD_SUPPORT_QT_STATIC")
else (NOT STATIC_BUILD)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DBUILD_SUPPORT_QT_DLL")
endif()
if (UNICODE_BUILD)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DUNICODE")
endif()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -pedantic -Wextra")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
add_subdirectory (logging)
add_subdirectory (models)
add_subdirectory (window)
#QT5_WRAP_UI(QT_UI_HEADERS ${QT_UI_SOURCES})
# Process each .ui file and create a dependency to the
# the corresponding .h file.
set(GENERATED_SOURCES "")
foreach(ui_file ${SUPPORT_SOURCE})
get_filename_component(ui_ext ${ui_file} EXT)
string(TOLOWER ${ui_ext} ui_ext)
set(ui_dependency "")
string(REPLACE "${CMAKE_SOURCE_DIR}" "${CMAKE_BINARY_DIR}" ui_gen_file ${ui_file})
if(${ui_ext} STREQUAL ".ui")
string(REGEX REPLACE ".ui$" ".h" ui_gen_file "${ui_gen_file}")
add_custom_command(
#PRE_BUILD
OUTPUT "${ui_gen_file}"
COMMAND ${QT_UIC} -g cpp -o "${ui_gen_file}" "${ui_file}"
MAIN_DEPENDENCY "${ui_file}"
COMMENT "Generating UI: ${ui_gen_file}"
)
# A .ui File always has a .moc file with it.
string(REGEX REPLACE ".h$" ".moc" ui_gen_file "${ui_gen_file}")
set(ui_ext ".moc")
endif()
if(${ui_ext} STREQUAL ".moc")
string(REGEX REPLACE ".moc$" ".cpp" ui_gen_file "${ui_gen_file}")
set(GENERATED_SOURCES "${GENERATED_SOURCES} ${ui_gen_file}")
add_custom_command(
#PRE_BUILD
OUTPUT "${ui_gen_file}"
COMMAND ${QT_MOC} -o "${ui_gen_file}" -f "${ui_file}" "${ui_file}"
MAIN_DEPENDENCY "${ui_file}"
COMMENT "Generating MOC: ${ui_gen_file}"
)
set(GENERATED_SOURCE "${GENERATED_SOURCE} ${ui_gen_file}")
#message("Generated: ${ui_gen_file}")
endif()
endforeach()
set_source_files_properties(${GENERATED_SOURCES} PROPERTIES GENERATED TRUE)
add_library(${TARGET_LIBRARY} ${BUILD_TYPE} ${SUPPORT_SOURCE} ${GENERATED_SOURCES})
target_link_libraries(${TARGET_LIBRARY} Qt5::Widgets)
set_target_properties(${TARGET_LIBRARY} PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
)
当我删除从append_library
的${GENERATED_SOURCES}
我得到这个错误:
CMake Error at support_qt/CMakeLists.txt:109 (add_library):
Cannot find source file:
D:/src/c/support_lib/build/support_qt/logging/logging_dialog_box_gui.cpp D:/src/c/support_lib/b
uild/support_qt/logging/logging_dialog_box_gui.cpp
Tried extensions .c .C .c++ .cc .cpp .cxx .m .M .mm .h .hh .h++ .hm .hpp
.hxx .in .txx
CMake Error: CMake can not determine linker language for target: support_qt_ad
CMake Error: Cannot determine link language for target "support_qt_ad".
我曾经见过类似的脚本使用add_custom_command
显示,但是当我这样做,我得到其他帖子:
mingw32-make[2]: *** No rule to make target 'support_qt/ D:/src/c/support_lib/build/support_qt/loggi
ng/logging_dialog_box_gui.cpp D:/src/c/support_lib/build/support_qt/logging/logging_dialog_box_gui.c
pp', needed by 'support_qt/CMakeFiles/support_qt_ad.dir/_D_/src/c/support_lib/build/support_qt/loggi
ng/logging_dialog_box_gui.cpp_D_/src/c/support_lib/build/support_qt/logging/logging_dialog_box_gui.c
pp.obj'. Stop.
CMakeFiles\Makefile2:236: recipe for target 'support_qt/CMakeFiles/support_qt_ad.dir/all' failed
mingw32-make[1]: *** [support_qt/CMakeFiles/support_qt_ad.dir/all] Error 2
makefile:74: recipe for target 'all' failed
mingw32-make: *** [all] Error 2
并且该文件不是仅编译生成的。
更新:
我也试过set(CMAKE_AUTOMOC ON)
和产生的CPP被编译,但只有一次。当修改moc
或包含的ui
文件时,它不更新它。看到一个完整的工作示例将会很有帮助。当我谷歌我只找到片段。 :(
请参阅我的更新。 – Devolus
@Devolus您还需要'qt5_wrap_ui'并将生成的文件作为依赖添加到'add_library'中,请参阅我的更新。 – doqtor
我也试过,但是一样。其实我第一次尝试,但因为它没有工作,我试着用add_custom_command。无论如何,这应该工作,而不是特别与QT有关,但我还没有找到任何完整的工作样本。 – Devolus