I made a library project, and followed the classical pattern, where I put the implementation details in a src directory, and the interface in a include/MyLib directory.
So the external consumers see what is exposed in the latter, and link to the sources compiled from the former.
But what about the unit tests ? How could I make them access to the implementation details ? I mean free functions and public member functions which are not exposed to the “normal consumers”.
If developing on Windows (or linux etc with default visibility to false), then remember to export all the functions that you want available for testing too.
In our codebase, we have 3 include folders - public, private, and protected. Public gets shipped, protected is available to the tests but not shipped, and private stays private.
The alternative to this solution, which we considered but rejected for now, is to create an Object library target of the code that you want to expose to tests, but not make public in your library. That way, you could link that object library against your tests and your lib both. But we didn’t like the idea of splitting our lib into multiple artificial targets, hence going for the public/private/protected headers.
Sorry for reacting so late.
Here is what I finally have done.
My project has three directories :
include/MyLibrary
src
test
The first one contains the public interface.
The second one contains the sources (private .h + all .cpp)
The src and test directories has their own CMakeLists.txt, and there is an other CMakeLists.txt in the root directory.
The CMakeLists.txt from the src directory is called at the first place, and it builds a target called MyLib.
The CMakeLists.txt from the test directory is called afterwards, and it is linked to this target.
Each directory has its own command target_include_directories().
The one in the test directory is :