Iter Ator
Iter Ator

Reputation: 9298

Why do I still get "unresolved external symbol" error after adding the reference to the test project?

I created a C++ project in Visual Studio 2019. I added two files to it: MyClass.hpp and MyClass.cpp. Then I created a test project and added the original project as a reference.

If I doubleclick on the reference in the test project, I can see MyClass. But if I try to run the tests, I get this error:

MyTest.obj : error LNK2019: unresolved external symbol "public: struct MyRef __thiscall MyClass::SetData(struct Data)" (?SetData@MyClass@@QAE?AUMyRef@@UData@@@Z) referenced in function "private: virtual void __thiscall MyTest_SetData_Test::TestBody(void)" (?TestBody@MyTest_SetData_Test@@EAEXXZ)

What do I wrong? Why is MyClass.cpp not compiled, even if it is in the referenced project? I can build the original project without error. I only get the link error, when I try to build the test project.


I came across this question about unresolved external symbol error, but it did not solve my issue. This is a Visual Studio specific problem.

If I add MyClass.cpp to the test project manually, then it works. But I do not want to do that for each file, because my project may have many more cpp source files, not just this one. I would like to make it work using the "references" feature.

When I created the test project, I selected the original project as a reference.

Upvotes: 3

Views: 4346

Answers (2)

La bla bla
La bla bla

Reputation: 8718

Just to add something to the previous comments/answers:

You didn't mention if you build your reference project as a static or dynamic library.

This can be set by going to the project's Properties->General->Configuration Type

Note that if you select Dynamic Library (.dll), your symbols (such as MyClass::SetData(struct Data)) will not be exported by default and you'd need to add the __declspec(dllexport) keyword. See here

Upvotes: 2

bolov
bolov

Reputation: 75853

Adding project B to project A as a reference basically just assures that project B is compiled whenever project A is compiled.

You need extra setup:

  1. make sure project B builds as a library (and not an executable)
  2. consider two paths accessible from both projects. This will be where the project B library will be installed. This comprises of two things:
    • the binary (static library is the simplest setup)
    • the library headers
  3. on compilation of project B you need to install both the resulting binary and the headers to these paths. For this you can make a post build step that copies the necessary files: Properties » Build Events » Post-Build Event

    Alternately just use project B's build folder and source location.

  4. in project A:

    • add the header path to the include paths:
      Properties » C/C++ » General » Additional include directories
    • add the library to the list of libraries to link:
      Properties » Linker » Input » Additional dependencies
    • add the binary path to the linker library search
      Properties » Linker » General » Additional Library Directories

Upvotes: 5

Related Questions