Reputation: 9298
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
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
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:
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.
in project A:
Upvotes: 5