简单 qtest 失败,'multiple definition of main'

Simple qtest fails with 'multiple definition of main'

本文关键字:definition of main multiple qtest 失败 简单      更新时间:2023-10-16

这是我第一次做测试,我决定在Qt中尝试,因为我现在正在做一个Qt项目。我试图做的基本上是一个单元测试的"你好世界",这样我就可以熟悉Qt测试了。然而,问题是编译测试失败:

$ make
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I/usr/include/qt4/QtTest -I. -I. -o test_simple.o test_simple.cpp
/usr/bin/moc-qt4 -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I/usr/include/qt4/QtTest -I. -I. test_simple.h -o moc_test_simple.cpp
g++ -c -m64 -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++-64 -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I/usr/include/qt4/QtTest -I. -I. -o moc_test_simple.o moc_test_simple.cpp
g++ -m64 -Wl,-O1 -o qtests test_simple.o moc_test_simple.o    -L/usr/lib/x86_64-linux-gnu -lQtTest -lQtGui -lQtCore -lpthread 
moc_test_simple.o: In function `main':
moc_test_simple.cpp:(.text.startup+0x0): multiple definition of `main'
test_simple.o:test_simple.cpp:(.text.startup+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [qtests] Error 1

该代码由Simple类和test_Simple测试类组成。

simple.h:

#ifndef SIMPLE_H
#define SIMPLE_H
#include <QObject>
class Simple : public QObject
{
    int _a,_b;
public:
    void setA(int a) { _a = a; }
    void setB(int b) { _b = b; }
    int a() { return _a; }
    int b() { return _b; }
};
#endif //SIMPLE_H

testrongimple.h:

#ifndef TEST_SIMPLE_H
#define TEST_SIMPLE_H
#include <QTest>
#include "simple.h"
class test_Simple : public QObject
{
    Q_OBJECT
private slots:
    void testNothing();
};
QTEST_MAIN( test_Simple )
#endif //TEST_SIMPLE_H

testrongimple.cpp:

void test_Simple::testNothing()
{
    Simple s;
    s.setA(3);
    QCOMPARE( s.a(), 3 );
    QCOMPARE( s.a(), 2 );
}

编辑:除了这个问题,我想确认你不能测试一个不是从QObject继承的类,对吗?

QTEST_MAIN(test_Simple)

这为您定义了一个主函数——所以只需将它放在您的测试用例"main".cpp中,而不是放在头中!