vendredi 11 septembre 2020

What is the correct way to set up a UnitTest project for C++ in Visual Studio with additional include directories in the main Project?

I am trying to test a C++ Project in Visual Studio 2019.

The project, which should be tested, has a class MyClass. The project folder has a directory "include" and I did set up the path in Properties > C/C++ > General > Additional include directory.

MyClass.h:

#pragma once

#include <string>

class MyClass
{
public:
    void print() const;
};

MyClass.cpp:

#include "MyClass.h"

void MyClass::print() const {
    printf("MyClass print"); 
}

This code executes just fine.

Now when I try to test this solution with a new UnitTest Project in the same solution I have some problems how to include MyClass and to test it there.

I included the reference to the first project in my UnitTest Project.

Variant 1 in UnitTest1.cpp:

#include "..\ConsoleApplication1\include\MyClass.h"
#include "..\ConsoleApplication1\MyClass.cpp"

Result: Error C2011 -> "MyClass": class type new definition

Error C2027 -> Use of undefined type "MyClass"

Error C2070 -> "print": Modifiers not allowed on nonmember functions

Error C2079 -> undefined Class "MyClass"

Variant 2 in UnitTest.cpp:

#include "..\ConsoleApplication1\include\MyClass.h"

Result: Error LNK2019, 1120 unresolved externals

I believe this is because of the additional include directory in the main project. Without additional include directory and with "#include "include\MyClass.h" in MyClass.cpp, the UnitTest works correctly . How do i include the Classes from the origninal project into the UnitTest Project properly?

Aucun commentaire:

Enregistrer un commentaire