在Qt项目中,添加新的c++文件后,使用该类时,编译报错。
网上对于这种报错,说法各异。
其实,原因很简单。
就是需要在CMakeLists.txt中,添加头文件和实现文件的配置项即可。
【编译报错】:
"VideoRender::qt_metacall(QMetaObject::Call, int, void**)", referenced from:
vtable for QQmlPrivate::QQmlElement<VideoRender> in main.cpp.o
"VideoRender::qt_metacast(char const*)", referenced from:
vtable for QQmlPrivate::QQmlElement<VideoRender> in main.cpp.o
"VideoRender::staticMetaObject", referenced from:
int qmlRegisterType<VideoRender>(char const*, int, int, char const*) in main.cpp.o
QtPrivate::MetaObjectForType<VideoRender*, void>::value() in main.cpp.o
QMetaTypeIdQObject<VideoRender*, 8>::qt_metatype_id() in main.cpp.o
"VideoRender::VideoRender(QObject*)", referenced from:
QQmlPrivate::QQmlElement<VideoRender>::QQmlElement() in main.cpp.o
"VideoRender::~VideoRender()", referenced from:
QQmlPrivate::QQmlElement<VideoRender>::~QQmlElement() in main.cpp.o
"VideoRender::metaObject() const", referenced from:
vtable for QQmlPrivate::QQmlElement<VideoRender> in main.cpp.o
"typeinfo for VideoRender", referenced from:
typeinfo for QQmlPrivate::QQmlElement<VideoRender> in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [FrtcMeeting.app/Contents/MacOS/FrtcMeeting] Error 1
make[1]: *** [CMakeFiles/FrtcMeeting.dir/all] Error 2
make: *** [all] Error 2
【根源】:
添加新文件后,没有在配置文件中添加新文件的路径,导致编译报错。
【解决方法】:
在CMakeLists.txt中,添加头文件和实现文件的路径即可。
# 1.Add header file include directories.
include_directories(
./Video/VideoRender/
)
# 2. Get all .cpp/.c/.cc files.
aux_source_directory(/Video/VideoRender/. VIDEO_RENDER_DIR_SRC)
# 3.Target
add_executable(FrtcMeeting
MACOSX_BUNDLE
$(VIDEO_RENDER_DIR_SRC)
...
main.cpp
qml.qrc
)