Say that I am making a shared library and have set:
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
So that I must manually export the public API. Now say that I have a templated class that is split into 2 files: a my_lib.hpp
which contains just the declaration:
template <typename Scalar>
class MyLibClass { ... };
and a my_lib.cpp
which contains the implementation and explicit instantiation:
...
template class MyLibClass<float>;
template class MyLibClass<double>;
I then use CMake's generate_export_header()
to generate a my_lib_export.hpp
file which contains the appropriate export declarations. Which file should I include it into? In the header it would be:
#include "my_lib_export.hpp"
class MY_LIB_EXPORT MyLibCass { ... };
While in the implementation file, it would be:
#include "my_lib_export.hpp"
...
template class MY_LIB_EXPORT MyLibClass<float>;
template class MY_LIB_EXPORT MyLibClass<double>;
In my very simple example, both compile and link without issue. But I'm wondering, is there a difference between the two? Do they serve different purposes, or are they completely interchangeable?