C++ unit test: "unresolved external symbol"

I've created a new solution in Rider and selected "Unit Test Project" in the wizard.  I named the solution "TestExample" and its project "Tests".  In this solution, I created a second project and selected "Library" (type: "Dynamic Library") in the wizard.  I named the second project "Sut".  So, now I have one solution "TestExample" containing two projects: "Tests" (which will contain the tests) and "Sut" (which will contain code that I would like to use in the tests).

No matter what I try, I can't convince Rider to successfully run tests in "TestExample" that use things from "Sut".

I modified the file "test.cpp" from project "TestExample" so that it contains the following:

#include "../Sut/NumberGetter.h"
#include "gtest/gtest.h"

TEST(TestCaseName, TestName)
{
EXPECT_EQ(11, get_number());
}

I created a file named "NumberGetter.cpp" in project "Sut" with the following contents:

#include "NumberGetter.h"

int get_number()
{
return 11;
}

I created a file named "NumberGetter.h" in project "Sut" with the following contents:

#pragma once

int get_number();

At this point, I tried to run the test by left-clicking on the run icon to the left of the test in file "test.cpp", but I got the following error:

  unresolved external symbol "int __cdecl get_number(void)" (?get_number@@YAHXZ) referenced in function "private: virtual void __cdecl TestCaseName_TestName_Test::TestBody(void)" (?TestBody@TestCaseName_TestName_Test@@EEAAXXZ)

Hoping to resolve this, I added the "Sut" project as a reference to the "Tests" project (right-click on the "Tests" project, select "Add... > Reference", check the box next to "Sut", then select "Add") and tried again.  However, now it errors out with:

  Microsoft.CppCommon.targets(1096, 5): [MSB6006] "link.exe" exited with code 1104.
  cannot open file 'C:\vs-ws\TestExample\x64\Debug\Sut.lib'

What am I doing wrong?  It works fine if I add the "NumberGetter.h" and "NumberGetter.cpp" files directly to the "Tests" project. However, if that's how it's supposed to be done, then I don't really understand why Rider gives you the choice to add a "Unit Test Project" in the new project wizard.  If one has authored a "Library" type project, then how does one add unit tests to it?

I've read the Rider tutorials, browsed places like YouTube, and done various Internet searches.  I'd really appreciate it if somebody could point me in the right direction.  Thank you in advance!

0
2 comments

More info: Using Visual Studio 2022, I followed the Visual Studio 2022 guide for writing unit tests for C++ DLLs, and the procedure worked.  I was able to run tests in a test project that tested code in a separate project from within Visual Studio 2022 using Google Test.  I was also able to run the tests from within Rider.

That's cool, but I was surprised that Rider didn't work so well even though it offers an option to create a test project.  Apparently the missing link was some boilerplate related to __declspec(dllexport).  Perhaps Rider just doesn't support adding the right things in the right places in this case.

It would be nice if it were easier to create tests from within Rider, but this experience makes me feel like I don't want to spend more time doing that; I think I will just stick to modifying the solution/project structure from within Visual Studio 2022 and avoid doing it from within Rider entirely, since it seems tricky to get right from within Rider, despite the fact that it seems possible.

0

Hello,

Thank you for contacting Rider support and for keeping us up to date.

Apologies for keeping you so long without an answer.

When develop a C++ library, you should keep in mind that you should add the __declspec(dllexport) attribute to those global/namespaced functions or classes that you are going to access outside of that library.

You can simplify your flow by creating a custom C++ class template, that would be adding dllexport attribute for you when you desire it. (see the screenshot)

Hope that helps.
Should you have any other questions or difficulties, please let me know.

Have a nice day!

0

Please sign in to leave a comment.