Linking an object file to a Test Project

asked 2021-11-04T04:15:36.06+00:00
NumericalFunctional 21 Reputation points

I want to link an object file to a native test project (all C++ if that matters). This is well documented. See for example Use the Microsoft Unit Testing Framework for C++ in Visual Studio.

Unfortunately I can't make it work. I always get a linker error. My stripped down example doesn't have templates.

/*  
  S o m e C l a s s . h  
*/  
  
#pragma once  
  
class SomeClass {  
public:  
  int const num;  
  
  SomeClass();  
  int Result();  
};  

and

/*  
  S o m e C l a s s . c p p  
*/  
  
#include "SomeClass.h"  
  
SomeClass::SomeClass() : num(987) {};  
  
int SomeClass::Result() {  
  return num;  
}  

My directory structure is :

HelloApp                                      <= contains the solution file  
HelloApp\Debug                          <= contains final output  
HelloApp\HelloApp                      <= contains SomeClass.h and SomeClass.cpp  
HelloApp\HelloApp\Debug          <= contains SomeClass.obj  
HelloApp\SecondTest                   <= contains a test project  
HelloApp\SecondTest\Debug       <=contains the partial build of the test project  

I added HelloApp as a reference in the SecondTest project.

I set additional dependencies for the SecontTest

146378-2021-11-04-3.png

I tired several variants, like the full path, and gasses involving relative paths.

I set the additional directories for the linker (actually tired many different variants).

146419-2021-11-04-2.png

It compiles but doesn't link. The errors are:
146330-2021-11-04.png

Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying a test as a means of analysis or diagnosis.
249 questions
{count} votes

1 answer

Sort by: Most helpful
  1. answered 2021-11-04T09:39:12.537+00:00
    PengGe-MSFT 3,236 Reputation points

    Hi, @NumericalFunctional

    Welcome to Microsoft Q&A!

    I created a project with reference to your code, and then I created a test project with reference to this document.
    146525-image.png

    I ran the test program and threw the same exception as yours.
    I refer to the document you gave, add the names of the .obj or .lib files to Additional Dependencies, add the directory path of the .obj or .lib files to Additional Library Directories, add the header directory of the project under test to Include Directories. My project ran successfully.
    146551-image.png
    146552-image.png
    146526-image.png

    This is my project structure:
    146495-image.png

    Please check if you missed some steps.

    Sincerely,
    Peng
    *
    If you have extra questions about this answer, please click "Comment". If the answer is the right solution, please click "Accept Answer" and kindly upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    No comments