Using CMake and gcc/clang compilers I want to create a shared library out of this file (Registrator.cpp
):
#include ...
namespace
{
struct Registrator
{
Registrator()
{
...
}
~Registrator()
{
...
}
} g_registrator [[gnu::used, gnu::visibility("default")]];
} // namespace
This object is not used anywhere else. Its responsibility is solely to do some things during its (de)construction.
When defining it as:
add_library(${libname} OBJECT Registrator.cpp)
It is a way to prevent the linker from optimizing out this TU as in OBJECT
case all sources get into final executable.
However I want to be able to use this as a SHARED
library as well. And in case of:
add_library(${libname} SHARED Registrator.cpp)
the library is not linked at all, and gnu::visibility("default")
unfortunately doesn't quite cut it.
Is there any way to achieve this? Thanks for help in advance.
Cheers :)