What is the CMake equivalent to "gcc -fvisibility=hidden" when controlling the exported symbol of a shared library?
Instead of setting compiler flags directly, you should be using a current CMake version and the <LANG>_VISIBILITY_PRESET properties instead. This way you can avoid compiler specifics in your CMakeLists and improve cross platform applicability (avoiding errors such as supporting GCC and not Clang).
I.e., if you are using C++ you would either call
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
to set the property globally, or set_target_properties(MyTarget PROPERTIES CXX_VISIBILITY_PRESET hidden)
to limit the setting to a specific library or executable target. If you are using C just replace CXX by C in the aforementioned commands. You may also want to investigate the VISIBLITY_INLINES_HIDDEN property as well.
The documentation for GENERATE_EXPORT_HEADER includes some more tips and examples related to both properties.
You can add a flag to the Cmake compiler like that:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
To make sure that this only done under Linux you can use this code:
if(UNIX AND CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
endif()