Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从cmake为QT5生成的MOC文件中出错_Qt_Cmake_Qt5_Moc - Fatal编程技术网

从cmake为QT5生成的MOC文件中出错

从cmake为QT5生成的MOC文件中出错,qt,cmake,qt5,moc,Qt,Cmake,Qt5,Moc,我使用 set (CMAKE_AUTOMOC ON) set(CMAKE_INCLUDE_CURRENT_DIR ON) 然后我使用 SET(SRC src/main.cpp src/video_widget_surface.cpp src/video_widget.cpp src/video_player.cpp #moc files moc/moc_video_player.cpp moc/moc_video_widget.cpp moc/moc_video_w

我使用

set (CMAKE_AUTOMOC ON)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
然后我使用

SET(SRC
  src/main.cpp
  src/video_widget_surface.cpp
  src/video_widget.cpp
  src/video_player.cpp
#moc files
  moc/moc_video_player.cpp
  moc/moc_video_widget.cpp
  moc/moc_video_widget_surface.cpp
最后,我使用

add_executable(somegui ${SRC})
但我在moc文件中发现错误,如:

/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:54:6: error: 'VideoWidget' has not been declared
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:62:19: error: 'VideoWidget' has not been declared
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:68:20: error: 'VideoWidget' has not been declared
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:68:46: error: non-member function 'const QMetaObject* metaObject()' cannot have cv-qualifier
/other/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h: In function 'const QMetaObject* metaObject()':
/other/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33: error: 'QScopedPointer<QObjectData> QObject::d_ptr' is protected
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:70:21: error: within this context
/other/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33: error: invalid use of non-static data member 'QObject::d_ptr'
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:70:21: error: from this location
/other/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33: error: 'QScopedPointer<QObjectData> QObject::d_ptr' is protected
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:70:50: error: within this context
/other/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33: error: invalid use of non-static data member 'QObject::d_ptr'
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:70:50: error: from this location
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: At global scope:
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:73:7: error: 'VideoWidget' has not been declared
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: In function 'void* qt_metacast(const char*)':
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:47: error: expected type-specifier before 'VideoWidget'
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:47: error: expected '>' before 'VideoWidget'
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:47: error: expected '(' before 'VideoWidget'
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:47: error: 'VideoWidget' was not declared in this scope
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:59: error: expected primary-expression before '>' token
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:61: error: invalid use of 'this' in non-member function
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:67: error: expected ')' before ';' token
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:78:40: error: cannot call member function 'virtual void* QWidget::qt_metacast(const char*)' without object
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: At global scope:
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:81:5: error: 'VideoWidget' has not been declared
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: In function 'int qt_metacall(QMetaObject::Call, int, void**)':
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:83:43: error: cannot call member function 'virtual int QWidget::qt_metacall(QMetaObject::Call, int, void**)' without object
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: In function 'void* qt_metacast(const char*)':
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:79:1: warning: control reaches end of non-void function [-Wreturn-type]
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp: In function 'const QMetaObject* metaObject()':
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:71:1: warning: control reaches end of non-void function [-Wreturn-type]
make[2]: *** [CMakeFiles/somestuff.dir/moc/moc_video_widget.cpp.o] Error 1
make[1]: *** [CMakeFiles/somestuff.dir/all] Error 2
make: *** [all] Error 2
/other/workspace/perception/somestuff/moc/moc\u video\u widget.cpp:54:6:错误:“VideoWidget”尚未声明
/其他/workspace/perception/something/moc/moc_video_widget.cpp:62:19:错误:“VideoWidget”尚未声明
/其他/workspace/perception/something/moc/moc_video_widget.cpp:68:20:错误:“VideoWidget”尚未声明
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:68:46:错误:非成员函数“const QMetaObject*metaObject()”不能有cv限定符
/其他/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:在函数“const QMetaObject*metaObject()”中:
/其他/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33:错误:“QScopedPointer qobject::d_ptr”受保护
/other/workspace/perception/something/moc/moc_video_widget.cpp:70:21:错误:在此上下文中
/其他/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33:错误:无效使用非静态数据成员'qobject::d_ptr'
/其他/workspace/perception/something/moc/moc_video_widget.cpp:70:21:错误:来自此位置
/其他/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33:错误:“QScopedPointer qobject::d_ptr”受保护
/other/workspace/perception/something/moc/moc_video_widget.cpp:70:50:错误:在此上下文中
/其他/Qt5.0.1/5.0.1/gcc_64/include/QtCore/qobject.h:401:33:错误:无效使用非静态数据成员'qobject::d_ptr'
/其他/workspace/perception/something/moc/moc_video_widget.cpp:70:50:错误:来自此位置
/其他/workspace/perception/something/moc/moc_video_widget.cpp:在全球范围内:
/其他/workspace/perception/something/moc/moc_video_widget.cpp:73:7:错误:“VideoWidget”尚未声明
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:在函数“void*qt_metacast(const char*)”中:
/其他/workspace/perception/something/moc/moc_video_widget.cpp:77:47:错误:在“VideoWidget”之前需要类型说明符
/其他/workspace/perception/something/moc/moc_video_widget.cpp:77:47:错误:在“VideoWidget”之前应为“>”
/其他/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:47:错误:应为“(”在“VideoWidget”之前)
/其他/workspace/perception/something/moc/moc_video_widget.cpp:77:47:错误:“VideoWidget”未在此范围内声明
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:59:错误:在“>”标记之前应该有主表达式
/其他/workspace/perception/something/moc/moc_video_widget.cpp:77:61:错误:在非成员函数中无效使用“this”
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:77:67:error:expected')before';'代币
/other/workspace/perception/something/moc/moc_video_widget.cpp:78:40:错误:无法在没有对象的情况下调用成员函数“virtual void*QWidget::qt_metacast(const char*)”
/其他/workspace/perception/something/moc/moc_video_widget.cpp:在全球范围内:
/其他/workspace/perception/something/moc/moc_video_widget.cpp:81:5:错误:“VideoWidget”尚未声明
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:在函数“int qt_metacall(QMetaObject::Call,int,void**)”中:
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:83:43:错误:无法在没有对象的情况下调用成员函数“virtual int QWidget::qt_metacall(QMetaObject::call,int,void**)”
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:在函数“void*qt_metacast(const char*)”中:
/其他/workspace/perception/something/moc/moc_video_widget.cpp:79:1:警告:控件到达非无效函数的末尾[-Wreturn类型]
/other/workspace/perception/something/moc/moc_video_widget.cpp:在函数“const QMetaObject*metaObject()”中:
/other/workspace/perception/somestuff/moc/moc_video_widget.cpp:71:1:警告:控件到达非无效函数的末尾[-Wreturn类型]
make[2]:***[CMakeFiles/somestuff.dir/moc/moc\u video\u widget.cpp.o]错误1
make[1]:***[CMakeFiles/somestuff.dir/all]错误2
make:**[全部]错误2

我的理解是,创建的moc文件中存在一些错误。但我无法控制它是如何产生的。现在我该如何解决这个bug呢?

CMake文档并没有那么糟糕,请不要忽视阅读它。您误解了的概念:

AUTOMOC是一个布尔值,指定CMake是否自动处理Qt
moc
预处理器,即不必使用
QT4\u WRAP\u CPP()
宏。目前支持Qt4。当此属性设置为
TRUE
时,CMake将在生成时扫描源文件,并相应地调用
moc
。如果找到像
#include“moc_foo.cpp”
这样的
#include
语句,则头文件中应包含
Q#u对象
类声明,并且在头文件上运行
moc
。如果找到像
#include“foo.moc”
这样的
#include
语句,则在当前源文件中需要一个
Q#u对象
,并在文件本身上运行
moc
。此外,所有头文件都会被解析为
Q_OBJECT
宏,如果找到,还会对这些文件执行
moc

因此,首先,您应该not将生成的
moc
文件显式添加到源代码中,并将其推入可执行编译。换句话说,您只推送您的来源:

SET(SRC
  src/main.cpp
  src/video_widget_surface.cpp
  src/video_widget.cpp
  src/video_player.cpp)
moc
则由CMake自动处理

第二,如文件所述:

  • 如果
    Q_对象
    foo.h
    中(即
    QObject
    在头文件中声明), 然后在相应的
    foo.cpp
    不要忘记添加
    #最好在末尾添加“moc_foo.cpp”
    档案的完整性

  • 如果
    Q_对象
    foo.cpp
    中(即
    QO