报错提示信息
CMakeFiles\NaMi.dir/objects.a(link.cpp.obj): In function `Link::listAvailablePorts()':
E:/4.work/Qt_Work/NaMi/link.cpp:57: undefined reference to `__imp__ZN15QSerialPortInfo14availablePortsEv'
E:/4.work/Qt_Work/NaMi/link.cpp:64: undefined reference to `__imp__ZNK15QSerialPortInfo8portNameEv'
CMakeFiles\NaMi.dir/objects.a(link.cpp.obj): In function `QList<QSerialPortInfo>::node_destruct(QList<QSerialPortInfo>::Node*, QList<QSerialPortInfo>::Node*)':
D:/Qt/5.15.2/mingw81_64/include/QtCore/qlist.h:524: undefined reference to `__imp__ZN15QSerialPortInfoD1Ev'
CMakeFiles\NaMi.dir/objects.a(link.cpp.obj): In function `QList<QSerialPortInfo>::node_copy(QList<QSerialPortInfo>::Node*, QList<QSerialPortInfo>::Node*, QList<QSerialPortInfo>::Node*)':
D:/Qt/5.15.2/mingw81_64/include/QtCore/qlist.h:492: undefined reference to `__imp__ZN15QSerialPortInfoC1ERKS_'
D:/Qt/5.15.2/mingw81_64/include/QtCore/qlist.h:498: undefined reference to `__imp__ZN15QSerialPortInfoD1Ev'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\NaMi.dir\build.make:192: NaMi.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:85: CMakeFiles/NaMi.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:92: CMakeFiles/NaMi.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:168: NaMi] Error 2
解决办法
出现上面报错信息的原因是,我们在CmakeLists.txt文件中没有导入对应的SerialPort模块,此时我们只需要在CmakeLists.txt文件中导入该模块即可,具体操作如下:
# 用于查找 Qt5包,并确保找到Widgets、SerialPort模块
find_package(QT NAMES Qt5 REQUIRED COMPONENTS Widgets SerialPort)
find_package(Qt${QT_VERSION_MAJOR} REQUIRED COMPONENTS Widgets SerialPort)
上述代码中表示找到Widgets和SerialPort模块;
# 链接Qt库,这里是链接了Qt下面的Widgets库和SerialPort库
target_link_libraries(NaMi PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::SerialPort)
上述代码中表示链接Widgets库和SerialPort库;
按如上操作后,就可以解决上面的问题了。