1

I've got a (qt) program in visual studio that I would like to create (qt) unit tests for. I thought the best way to do this would be to have the unit tests in a separate project in the same Visual Studio solution and then include the classes I want to test from the other project.

However, I created a second project for the unit tests, and when I include a class from the first project and initialize it, I get a linker error such as the following:

SimpleTest.obj : error LNK2019: unresolved external symbol "public:
__cdecl SimpleClass::SimpleClass(void)" (??0SimpleClass@@QEAA@XZ) referenced in function "private: void __cdecl SimpleTest::testSimpleClass(void)" (?testSimpleClass@SimpleTest@@AEAAXXZ)

I've added the directory of .h and .cpp files to the additional includes directory in properties of the test project, and I've added the .lib file output with the exe from the first project in the additional library directories of the second projects properties, but none of this seems to have fixed this linker error and allowed the tests to run. What am I missing to get my project to successfully compile?

For reference, the files I have are:

SimpleClass.h (In project 1):

#pragma once
class SimpleClass
{
public:
    SimpleClass();
};

SimpleClass.cpp (In project 1):

#include "SimpleClass.h"

SimpleClass::SimpleClass()
{
}

SimpleTest.h (In project 2):

#pragma once

#include <QTest>

class SimpleTest : public QObject
{
Q_OBJECT
private slots:
    void testSimpleClass();
};

SimpleTest.cpp (In project 2):

#include "SimpleTest.h"

#include "SimpleClass.h"

void SimpleTest::testSimpleClass()
{
    SimpleClass *splClass = new SimpleClass();
}

main.cpp (In project 2):

#include <QtTest>
#include "testqstring.h"
#include "SimpleTest.h"

int main(int argc, char** argv) {
    QApplication app(argc, argv);

    TestQString testSuite1;
    SimpleTest testSuite2;
    return QTest::qExec(&testSuite1, argc, argv) | QTest::qExec(&testSuite2, argc, argv);
}
m7913d
  • 10,244
  • 7
  • 28
  • 56
Jon
  • 495
  • 2
  • 9
  • 22
  • 1
    Seems like `SimpleClass` is not included in your project or you are not linking to the library that contains it. – drescherjm Jul 04 '17 at 15:52
  • I've added the location of SimpleClass to the Aditional Include Directories, along with the location of the .lib generated with the exe in the Aditional Library Directories in the properties of the second project in Visual Studio. Is there more I need to do to allow the second project to recognise SimpleClass? – Jon Jul 04 '17 at 15:54
  • 2
    What does " the .lib file output with the exe " mean? Does your first project genearte a libarary or an executable? You would eithe radd the cpp file directly to the project - "add existing item", or add libarary as a reference. – doctorlove Jul 04 '17 at 15:55
  • ***along with the location of the .lib*** Adding the folder of the lib is not sufficient. You must link to the library either via linker settings in your project settings or a `#pragma comment( lib, "mylib.lib")` – drescherjm Jul 04 '17 at 15:58
  • 1
    Possible duplicate of [Linker error when function definition is in cpp file](https://stackoverflow.com/questions/31225672/linker-error-when-function-definition-is-in-cpp-file) – doctorlove Jul 04 '17 at 16:05

0 Answers0