Did not find -fvisibility=hidden when using CMAKE_CXX_VISIBILITY_PRESET

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.

Make sure to set these variables before you declare your targets.

set(CMAKE_CXX_VISIBILITY_PRESET "hidden")
set(CMAKE_VISIBILITY_INLINES_HIDDEN "YES")

# add the MathFunctions library
add_subdirectory(MathFunctions)

# add the executable
add_executable(Tutorial tutorial.cxx)

From the documentation on <LANG>_VISIBILITY_PRESET:

This property is initialized by the value of the CMAKE_<LANG>_VISIBILITY_PRESET variable if it is set when a target is created.

1 Like

I went back and looked at my notes and remembered that the use of the word create, rather than something like declare made me think it was referring to the time the target is actually created when the build runs.