c++ - 简单 qtest 失败并显示 'multiple definition of main'

标签 c++ qt compiler-errors qtestlib

这是我第一次进行测试,我决定在 Qt 中尝试它,因为我现在正在做一个 Qt 项目。我试图做的基本上是一个单元测试的“hello world”,这样我就可以熟悉 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组成。

简单.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

test_simple.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

test_simple.cpp:

void test_Simple::testNothing()
{

    Simple s;
    s.setA(3);

    QCOMPARE( s.a(), 3 );
    QCOMPARE( s.a(), 2 );
}

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

最佳答案

QTEST_MAIN( test_Simple )

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

关于c++ - 简单 qtest 失败并显示 'multiple definition of main',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18250554/

相关文章:

c++ - C++/错误: no matching function for call to

c++ - 如何编译wxsqlite3和sqlite以获得加密支持

c++ - 尝试从链表返回数据时qt中的段错误

c# - QT 中字节数组到 double 转换失败

c++ - QBoxLayout 与 QMainWindow 对比 QWidget

c++ - 有没有办法在 QTableView 的顶部显示文本行?

c++ - 每个文件启用范围保护

c - 出现错误 "assignment makes pointer from integer without a cast"

c++ - 当 typedef 名称与可变参数模板参数名称一致时出现 GCC 错误

boost - Clang不编译g++项目