1

I am trying to add some missing skills to my repertoire, one of which is setting up a proper parallel test project.

I have a simple setup in one project (EffCPP) to experiment with some C++ concepts and I am trying to set up a Google Test project (Test) in another one that can access all relevant files and test them.

Unfortunately, after setting up the testing project successfully, I started having problems constructing data from the EffCPP project in the Test one.

Specifically, a linker error that says my test project cannot link a private static method that the public constructor accesses.

I am sure I am missing something about the compilation process here but the solution seems quite non obvious and the query itself is hard to google.

I have a github repository of all this located here : https://github.com/Vidrohi/EffectiveCPP.git

It was suggested that I try to create a simple example of the situation in the question. What follows is my attempt at explaining the same :

///////////////////////// EffCPP ////////////////////////////////


/////////////////////////  ClassToBeTested.h ////////////////////////////////


class ClassToBeTested
{
private:
    unsigned int m_id;

    static unsigned int s_currentId;
    static unsigned int GetNextId();

public:

    ClassToBeTested():m_id(GetNextId())
    {}  
}

/////////////////////////  ClassToBeTested.cpp ////////////////////////////////

#include "ClassToBeTested.h"

unsigned int Performance::NonPOD::s_currentId = 0;

unsigned int Performance::NonPOD::GetNextId()
{
    return ++s_currentId;
}

///////////////////////// TestProject ////////////////////////////////

///////////////////////// Tests.cpp /////////////////////////

#include "gtest/gtest.h"
#include "../EffCPP/Chapter6/ClassToBeTested.h" // Just the path where that header is located really

std::string name("NAME");
TEST(name, name) {
    EXPECT_EQ(1, 1); // Just to make sure the test framework is running correctly

    ClassToBeTested cp;
}

Edit 2 : Interestingly I found another question which talks about pretty much the same issue link but this just suggests that I set up a third project to build as a lib and then link that static lib in. Is that the only way to do this ?

Vidrohi
  • 112
  • 10
  • 1
    We're not downloading an entire repository to look for one particular issue. Distill into into a [mcve] and [edit] that into your question. – Angew is no longer proud of SO Nov 20 '17 at 07:58
  • Hmm, interesting, I can try but if the problem is with the way the projects are set up , something to do with #include settings or linker settings, how will anyone diagnose it without access to the projects ? – Vidrohi Nov 20 '17 at 08:00
  • An MCVE can still consist of several files etc. But they should all be minimal: such that if you take a single thing out from them, the problem disappears. Once you've prepared such a *minimal* example, there are two possible outcomes: either you'll figure out the issue yourself in the process (the best outcome), or you'll have material for a good, answerable SO question. But invest your time first, before asking others to do the same. Yes, it can be time-consuming (I've spent my share of time creating MCVEs for my question), but it's how SO (or good asking for help in general) works. – Angew is no longer proud of SO Nov 20 '17 at 08:06
  • Well, there's the essence of the setup, I know that it will all work if its in one Visual studio solution. Strange that the other solution should even care ... I mean the source files are just that ... source. – Vidrohi Nov 20 '17 at 08:14
  • Are you somehow making sure `TestProject` links against `EffCPP`? – Angew is no longer proud of SO Nov 20 '17 at 08:18
  • Perhaps that is where I am missing something, I did add a Visual studio reference from the project to the TestProject to EffCPP. I thought that would be enough to allow for the linker to find all relevant cpp's. I've got the additional includes set up as well ofcourse. – Vidrohi Nov 20 '17 at 08:22
  • The linker doesn't care about `.cpp` files at all. The linker's inputs are *object files* (products of compilation), *static libraries* (objects files packed into one file by an archiver/librarian program), or *shared libraries* (products of linking). On Windows, you link against shared libraries (DLLs) and executables via their "import library," which is a `.lib` file. – Angew is no longer proud of SO Nov 20 '17 at 08:26
  • You might want to take a look at the [canonical SO question about linking](https://stackoverflow.com/q/12573816/1782465). – Angew is no longer proud of SO Nov 20 '17 at 08:26
  • I guess I should have been more specific , I was hoping that the VS reference would be enough to provide `Test` access to the obj files of `EffCPP`. If it was a lib or a dll I'd set up a dependency differently ofcourse but the EffCPP project builds to an exe. regardless, I will read the information about linking. – Vidrohi Nov 20 '17 at 08:30

1 Answers1

0

One possible solution to this problem is to not create an executable from the EffCPP project but instead create a static lib. This static lib can then easily be provided to the test project which can then link it and run itself as an exe.

Another possible solution can be found in this question but that requires creating a third project.

I am still looking for a way to be able to use just two projects and build both as .exe

Vidrohi
  • 112
  • 10