Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I write my C++ code as a static or dynamic library if I want to test it?

I'm working on a C++ project in Visual Studio and wanted to test it using Google Test.

In order to do so, I built a solution which includes three projects:

  1. Code project as a console application.
  2. Google Test as a static library.
  3. Tests project as a console application.

I wrote the code in the code project and a test in the test project.

When I tried building the solution, I got a lnk2019 error which stopped happening when I moved the classes to the tests project (but that's not where I want them to be).

I read that the best way to do so is by writing the classes as a library so I'll be able to use them in both (code and tests) projects.

Should I do that as a static library or as a DLL (and why)?

Other solutions are welcomed as well.

like image 786
Eyal Weisz Avatar asked Oct 23 '25 18:10

Eyal Weisz


1 Answers

The key thing to understand here: you are getting things backwards.

You do not make this decision in order to get to "easier" testing. You first and foremost have to understand the "production requirements" towards your delivery. You create a static library, if that is the best for shipping your product. If not, you create a dynamic library.

For testing, you either deal with the chosen "delivery format"; or you think about a simple way that allows you to build a different thing for testing (although that is discouraged, as you want to test your product, nothing else).

Don't get me wrong: it is important to design for testing; but the decision you are asking for should be driven by your production requirements!

In other words: the "form factor" of your product should depend on product requirements, it should not be determined by your test setup.

like image 87
GhostCat Avatar answered Oct 26 '25 08:10

GhostCat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!