Linking Between Two Projects in a Solution

S

Sami Lakka

I have a solution with a two projects. The first project contains my classes
and an executable using those classes. The second project contains unit
tests for the classes. Because the unit test framework that I use requires a
main function I have to keep the tests in a separate project. Now when I try
to link the unit test project I get a lot of "Error LNK2019: unresolved
external symbol" errors. Both projects compile just fine and and the first
project creates OBJ files in the output directory. What should I do so that
the linker in the unit test project correctly links with the OBJ files of
the first project? Both of these projects should create executables.
 
A

adebaene

I have a solution with a two projects. The first project contains my classes
and an executable using those classes. The second project contains unit
tests for the classes. Because the unit test framework that I use requires a
main function I have to keep the tests in a separate project. Now when I try
to link the unit test project I get a lot of "Error LNK2019: unresolved
external symbol" errors. Both projects compile just fine and and the first
project creates OBJ files in the output directory. What should I do so that
the linker in the unit test project correctly links with the OBJ files of
the first project? Both of these projects should create executables.

You need to add a reference to the 1st project in the 2nd project.

In Visual Studio 2005, right-click on the 2nd project in Solution
Explorer -> "References..." -> "Add New Reference" -> "Projects" Tab -
choose your 1st project.

Arnaud
MVP - VC
 
B

Ben Voigt

You need to add a reference to the 1st project in the 2nd project.

In Visual Studio 2005, right-click on the 2nd project in Solution
Explorer -> "References..." -> "Add New Reference" -> "Projects" Tab -

That will only work for pure managed exes, or dll assemblies, because native
mixed exe assemblies aren't relocatable.

You probably will end up moving your common testable code into a dll.
That's needed for reusability in .NET anyway. In native C++, source code
reuse is a viable option as well, but that's what you're up against right
now.

If you want the same compiler options, package those .obj files into a
library (dll or static .lib). If you need different compiler settings,
include the source files in both projects.
 
A

Arnaud Debaene

Ben Voigt said:
That will only work for pure managed exes, or dll assemblies, because
native mixed exe assemblies aren't relocatable.

Dooh! I missed the fact that the 1st project was an exe, sorry about that
:-(
If you want the same compiler options, package those .obj files into a
library (dll or static .lib).
Indeed. This is of course the solution....

Arnaud
MVP - VC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top