- 前言
- 问题
- 解决方法
- 总结
前言
Qt Remote Objects 是Qt官方推出的一个用于进程间通信(IPC)的模块,而且支持网络通讯。
详细用法网络上很多,Qt官方手册也讲的很清楚,要学习用的话强烈建议去看官方手册,看手册完全可以搞明白。
目前我这里还是使用的qt5.15.2版本,而且项目管理已由qmake转为cmake,但在cmake中遇到无法生成rep_***_source.h 的情况,下面记录问题产生的过程和解决方法。
解决方法以下以commoninterface.rep 文件为例
在qmake中,生成 rep_commoninterface_source.h
REPC_REPLICA +=
../Reps/commoninterface.rep
qmake,然后构建 后就可以生成 rep_commoninterface_source.h 文件
但在cmake中,需使用qt5_generate_repc(),注意Qt6中又有不同的函数 ,注意看手册
https://doc-snapshots.qt.io/qt6-dev/qtremoteobjects-repc.html
https://doc-snapshots.qt.io/qt6-dev/qtremoteobjects-cmake-qt-add-repc-sources.html
这里只说Qt5中的用法:
官方手册说明:https://doc.qt.io/qt-5/qtremoteobjects-cmake-qt5-generate-repc.html
最初我的cmake写法:
cmake_minimum_required(VERSION 3.16)
project(RoClient VERSION 1.0 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Set up AUTOMOC and some sensible defaults for runtime execution
# When using Qt 6.3, you can replace the code block below with
# qt_standard_project_setup()
set(CMAKE_AUTOMOC ON)
include(GNUInstallDirs)
find_package(QT NAMES Qt5 Qt6 REQUIRED COMPONENTS Core)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS RemoteObjects)
add_executable(RoClient
main.cpp
# rep_commoninterface_merged.h
)
qt5_generate_repc(RoClient Reps/commoninterface.rep MERGED)
target_link_libraries(RoClient PRIVATE
Qt${QT_VERSION_MAJOR}::Core
Qt${QT_VERSION_MAJOR}::RemoteObjects
)
install(TARGETS RoClient
BUNDLE DESTINATION .
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
)
但执行后,不管是cmake还是构建,都无法生成rep_commoninterface_merged.h
直到后来有人说,需要将rep_commoninterface_merged.h 加入到目标列表中才可以,具体是因为qt5_generate_repc里面使用到了add_custom_command这个命令,目标文件必须要在target列表里,才可以正常工作。
所以
add_executable(RoClient
main.cpp
# rep_commoninterface_merged.h
)
改为
add_executable(RoClient
main.cpp
rep_commoninterface_merged.h
)
即可正常工作。
总结其实官方手册里也写是这么用的,
find_package(Qt5 COMPONENTS RemoteObjects REQUIRED)
set(SOURCES
main.cpp
simpleswitch.cpp
)
qt5_generate_repc(SOURCES simpleswitch.rep SOURCE)
add_executable(directconnectserver ${SOURCES})
target_link_libraries(directconnectserver Qt5::RemoteObjects)
只是没有注意到这个问题



