C++ Cmake生成测试文件
我正在从事一个由一些组件组成的项目,每个组件都必须与GPS或IMU等传感器进行通信。C++ Cmake生成测试文件,c++,qt,cmake,C++,Qt,Cmake,我正在从事一个由一些组件组成的项目,每个组件都必须与GPS或IMU等传感器进行通信。 文件夹的结构如下所示: / build src errorCodes.h Component.h Component.cpp IMU.h IMU.cpp tests imu main.cpp CMakeLists.txt cmake_minimum_required (VERSION 2.6 FATAL_ERROR) set
文件夹的结构如下所示:
/
build
src
errorCodes.h
Component.h
Component.cpp
IMU.h
IMU.cpp
tests
imu
main.cpp
CMakeLists.txt
cmake_minimum_required (VERSION 2.6 FATAL_ERROR)
set (PROJECT_NAME imu)
set (DUT_DIR ${CMAKE_SOURCE_DIR}/../../../src)
project (${PROJECT_NAME})
find_package (Qt5Core REQUIRED)
set (project_SOURCES main.cpp ${DUT_DIR}/IMU.h ${DUT_DIR}/IMU.cpp ${DUT_DIR}/Component.h ${DUT_DIR}/Component.cpp ${DUT_DIR}/errorCodes.h)
include_directories (${DUT_DIR})
ADD_EXECUTABLE (${PROJECT_NAME} ${project_SOURCES}
${project_FORMS_HEADERS}
${project_HEADERS_MOC})
qt5_use_modules(${PROJECT_NAME} Core SerialPort)
我想做的是编译test目录中的main.cpp
文件,该文件链接src文件中的实现
CMakeLists.txt
文件如下所示:
/
build
src
errorCodes.h
Component.h
Component.cpp
IMU.h
IMU.cpp
tests
imu
main.cpp
CMakeLists.txt
cmake_minimum_required (VERSION 2.6 FATAL_ERROR)
set (PROJECT_NAME imu)
set (DUT_DIR ${CMAKE_SOURCE_DIR}/../../../src)
project (${PROJECT_NAME})
find_package (Qt5Core REQUIRED)
set (project_SOURCES main.cpp ${DUT_DIR}/IMU.h ${DUT_DIR}/IMU.cpp ${DUT_DIR}/Component.h ${DUT_DIR}/Component.cpp ${DUT_DIR}/errorCodes.h)
include_directories (${DUT_DIR})
ADD_EXECUTABLE (${PROJECT_NAME} ${project_SOURCES}
${project_FORMS_HEADERS}
${project_HEADERS_MOC})
qt5_use_modules(${PROJECT_NAME} Core SerialPort)
但是当我运行make
命令时,我收到以下错误:
CMakeFiles/imu.dir/home/thegr8/Desktop/3d-6dof-scanner/src/IMU.cpp.o: In function `IMU::IMU()':
IMU.cpp:(.text+0x25): undefined reference to `vtable for IMU'
CMakeFiles/imu.dir/home/thegr8/Desktop/3d-6dof-scanner/src/IMU.cpp.o: In function `IMU::~IMU()':
IMU.cpp:(.text+0x7c): undefined reference to `vtable for IMU'
CMakeFiles/imu.dir/home/thegr8/Desktop/3d-6dof-scanner/src/Component.cpp.o: In function `Component::Component(QObject*)':
Component.cpp:(.text+0x2a): undefined reference to `vtable for Component'
CMakeFiles/imu.dir/home/thegr8/Desktop/3d-6dof-scanner/src/Component.cpp.o: In function `Component::~Component()':
Component.cpp:(.text+0x65): undefined reference to `vtable for Component'
IMU.cpp:(.text+0x244): undefined reference to `QSerialPortInfo::availablePorts()'
IMU.cpp:(.text+0x265): undefined reference to `QSerialPortInfo::QSerialPortInfo(QSerialPortInfo const&)'
IMU.cpp:(.text+0x28b): undefined reference to `QSerialPort::QSerialPort(QObject*)'
IMU.cpp:(.text+0x2af): undefined reference to `QSerialPortInfo::portName() const'
IMU.cpp:(.text+0x343): undefined reference to `QSerialPortInfo::description() const'
IMU.cpp:(.text+0x3d7): undefined reference to `QSerialPortInfo::manufacturer() const'
IMU.cpp:(.text+0x46b): undefined reference to `QSerialPortInfo::portName() const'
IMU.cpp:(.text+0x488): undefined reference to `QSerialPort::setPortName(QString const&)'
IMU.cpp:(.text+0x560): undefined reference to `QSerialPort::setBaudRate(int, QFlags<QSerialPort::Direction>)'
IMU.cpp:(.text+0xada): undefined reference to `QSerialPort::error() const'
IMU.cpp:(.text+0xb46): undefined reference to `QSerialPortInfo::~QSerialPortInfo()'
IMU.cpp:(.text+0xb91): undefined reference to `QSerialPortInfo::~QSerialPortInfo()'
IMU.cpp:(.text+0xd37): undefined reference to `QSerialPortInfo::~QSerialPortInfo()'
我做错了什么
更新 现在,我在build目录中创建了一个库,并按照注释中的建议从测试文件链接它们 编译测试项目时,我收到以下错误:
CMakeFiles/imu.dir/home/thegr8/Desktop/3d-6dof-scanner/src/IMU.cpp.o: In function `IMU::IMU()':
IMU.cpp:(.text+0x25): undefined reference to `vtable for IMU'
CMakeFiles/imu.dir/home/thegr8/Desktop/3d-6dof-scanner/src/IMU.cpp.o: In function `IMU::~IMU()':
IMU.cpp:(.text+0x7c): undefined reference to `vtable for IMU'
CMakeFiles/imu.dir/home/thegr8/Desktop/3d-6dof-scanner/src/Component.cpp.o: In function `Component::Component(QObject*)':
Component.cpp:(.text+0x2a): undefined reference to `vtable for Component'
CMakeFiles/imu.dir/home/thegr8/Desktop/3d-6dof-scanner/src/Component.cpp.o: In function `Component::~Component()':
Component.cpp:(.text+0x65): undefined reference to `vtable for Component'
IMU.cpp:(.text+0x244): undefined reference to `QSerialPortInfo::availablePorts()'
IMU.cpp:(.text+0x265): undefined reference to `QSerialPortInfo::QSerialPortInfo(QSerialPortInfo const&)'
IMU.cpp:(.text+0x28b): undefined reference to `QSerialPort::QSerialPort(QObject*)'
IMU.cpp:(.text+0x2af): undefined reference to `QSerialPortInfo::portName() const'
IMU.cpp:(.text+0x343): undefined reference to `QSerialPortInfo::description() const'
IMU.cpp:(.text+0x3d7): undefined reference to `QSerialPortInfo::manufacturer() const'
IMU.cpp:(.text+0x46b): undefined reference to `QSerialPortInfo::portName() const'
IMU.cpp:(.text+0x488): undefined reference to `QSerialPort::setPortName(QString const&)'
IMU.cpp:(.text+0x560): undefined reference to `QSerialPort::setBaudRate(int, QFlags<QSerialPort::Direction>)'
IMU.cpp:(.text+0xada): undefined reference to `QSerialPort::error() const'
IMU.cpp:(.text+0xb46): undefined reference to `QSerialPortInfo::~QSerialPortInfo()'
IMU.cpp:(.text+0xb91): undefined reference to `QSerialPortInfo::~QSerialPortInfo()'
IMU.cpp:(.text+0xd37): undefined reference to `QSerialPortInfo::~QSerialPortInfo()'
IMU.cpp:(.text+0x244):对'QSerialPortInfo::availableport()'的未定义引用
IMU.cpp:(.text+0x265):未定义对“QSerialPortInfo::QSerialPortInfo(QSerialPortInfo const&)”的引用
IMU.cpp:(.text+0x28b):对“QSerialPort::QSerialPort(QObject*)”的未定义引用
IMU.cpp:(.text+0x2af):对'QSerialPortInfo::portName()const'的未定义引用
IMU.cpp:(.text+0x343):未定义对“QSerialPortInfo::description()const”的引用
IMU.cpp:(.text+0x3d7):对'QSerialPortInfo::manufacturer()const'的未定义引用
IMU.cpp:(.text+0x46b):对'QSerialPortInfo::portName()const'的未定义引用
IMU.cpp:(.text+0x488):对“QSerialPort::setPortName(QString const&)”的未定义引用
IMU.cpp:(.text+0x560):对“QSerialPort::setBaudRate(int,QFlags)”的未定义引用
IMU.cpp:(.text+0xada):对'QSerialPort::error()const'的未定义引用
IMU.cpp:(.text+0xb46):对'QSerialPortInfo::~QSerialPortInfo()的未定义引用
IMU.cpp:(.text+0xb91):对'QSerialPortInfo::~QSerialPortInfo()的未定义引用
IMU.cpp:(.text+0xd37):对'QSerialPortInfo::~QSerialPortInfo()的未定义引用
这两个项目都是使用
set(AUTOMOC ON)
编译的,IMU和组件是否继承QObject并定义Q_对象?在这种情况下,尝试将<代码> SET(CMACHY-AutoCon)添加到您的CMAKLISTS.TXTBY……考虑将您的SRC/Tobe作为一个库,然后让您的测试链接到它。您也可以使用CMake的ctest
runner(可能还有qtest)来运行测试。@juzzlin-我已经更新了我的问题,现在我收到了相对于QSerialPortInfo
库的未定义参考
您是否链接到测试中的Qt串口库?@juzzlin-是,使用命令qt5\u使用模块(${项目名称}核心串行端口< /COD> DIMU和组件从QObject继承并定义QYObjor?在这种情况下,尝试将<代码> SET(CMACHY-AutoCon)添加到您的CMAKLISTS.TXTBY……考虑将您的SRC/Toes作为一个库,然后让您的测试链接到它。您还可以使用CMake的<代码> CTEST <代码> >(可能还有QtTest)来运行测试。@juzzlin-我已经更新了我的问题,现在我收到了未定义的参考
相对于QSerialPortInfo
库你是否链接到测试中的Qt串口库?@juzzlin-是,使用命令qt5\u使用模块(${PROJECT\u NAME}Core SerialPort)