0

I've been trying to set up a CPPUnitTest to test a C++ project. I've came across an error where I've got two rotating errors depending on how I try to solve my problem.

I've got two projects in a solution in Visual Studio. One is for testing, one is for the project itself. I'm having these errors whilst trying to reference the project in the testing project.

If I do this, I get a LNK2019 (unresolved external symbol) error any time I try to construct an object or call a function:

#pragma once
#ifndef REFERENCE_H
#define REFERENCE_H
#include "../Stuff/Thing.h"
#include "../Stuff/OtherThing.h"
#endif

However, if I do this, I get a LNK2005 (test2.obj: blahblahlblah is already defined in test1.obj) error since two of the tests reference it:

#pragma once
#ifndef REFERENCE_H
#define REFERENCE_H
#include "../Stuff/Thing.cpp"
#include "../Stuff/OtherThing.cpp"
#endif

Deleting one of the tests fixes the problem with the latter (.cpp) but obviously that's not very good.

I think I may have missed a step somewhere along the way but I'm not sure what it is. I did add the "project" project as a dependency to the test.

Does anyone have the solution to this?

  • Is the project you added as a dependency a `.lib` or `.dll` project. A project that is an `.exe` will not work. – drescherjm Apr 27 '16 at 17:44
  • I have no idea. I just added it in Build Dependencies -> Project Dependencies. Should I do something else here? – throwsFireball Apr 27 '16 at 17:57
  • You have no idea what kind of projects you are building? What I am saying is you can not make an application dependent on a second application and expect the symbols to link since you can not link to an application in native `c++`. – drescherjm Apr 27 '16 at 17:58
  • Ah, I just realised that I skipped over that. For anyone who's wondering, he's talking about Project Properties and the Configuration Type. It is currently set to .exe but I've just tried switching it out for a .lib / .dll and that doesn't seem to have solved the problem. Am I skipping something or missing something here? – throwsFireball Apr 27 '16 at 18:14
  • ***that doesn't seem to have solved the problem.*** You can not arbitrarily switch the project type. My advice is to create a static library project that both your application projects depend on. Put all of the code that you need to share in the static library project. – drescherjm Apr 27 '16 at 18:15
  • I'm sorry, I'm more used to Java, PHP and the like. C++ has a lot of stuff going on that I'm not really understanding here. I'm testing pretty much all my code through my CPPUnitTests, so the library would just hold all the code. Is it possible to just reference that through? – throwsFireball Apr 27 '16 at 19:02
  • After you put your code into a static library your method of setting the dependency between projects should work. Remember that a static library will not contain a `int main()` You need to separate your code into what belongs in the applications that use the library and what belongs in the library. – drescherjm Apr 27 '16 at 19:04
  • Here is an example: https://msdn.microsoft.com/en-us/library/ms235627.aspx – drescherjm Apr 27 '16 at 19:10

0 Answers0