C0002.Clion运行Qt项目时,报错提示undefined reference to `__imp__ZN15QSerialPortInfo14availablePortsEv‘

报错提示信息

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库;

按如上操作后,就可以解决上面的问题了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值