In Step 10 of the tutorial, the final challenge is to use a module to generate the export header file.
There is an example in this forum that introduces CMAKE_CXX_VISIBILITY_PRESET, so I set that to hidden
as in the example:
include(GenerateExportHeader)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
set(header_export_file_name MathFunctions_export.h)
generate_export_header(MathFunctions
EXPORT_FILE_NAME ${header_export_file_name}
INCLUDE_GUARD_NAME HEADER_c23435e8ad2d458db29bf6a6151d4515
)
I used this on Ubuntu Linux 24.04 (on WSL2) with gcc 13.2
and make
. I expected to see -fvisibility=hidden
in the link command for libMathFunctions.so
. It looks like the link is run with cmake
using the file link.txt
. Its content is
/usr/bin/c++ -fPIC -shared -Wl,-soname,libMathFunctions.so -o ../libMathFunctions.so CMakeFiles/MathFunctions.dir/MathFunctions.cxx.o ../libSqrtLibrary.a
But I don’t see the visibility flag there, or anywhere else in the binary directory.
To get some insight, I added a function to do sqrt without specifying a visibility attribute (sqrt_default
), and one specifically hidden with MATHFUNCTIONS_NO_EXPORT
(sqrt_hidden
).
This showed that sqrt_default
was visible while sqrt_hidden
was hidden, causing a linker error if used in tutorial.cxx.
Then I added -fvisibility=hidden
to the command in link.txt and found that now sqrt_default
is hidden.
So it seems that the compile and link are behaving as expected and the only thing unexpected is that the visibility flag is missing from the link command.
It seems that this is worth looking into because it would be a quiet failure - symbols that are expected to be hidden by default are not being hidden.
Or maybe, as I am new to CMake, there is something I am missing.
Using cmake 3.28 with minimum required 3.15.