diff --git a/CMakeLists.txt b/CMakeLists.txt
index b7d1c42..91b4195 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -67,8 +67,6 @@ target_link_libraries(matroska PUBLIC EBML::ebml)
 set_target_properties(matroska PROPERTIES
   VERSION 7.0.0
   SOVERSION 7
-  CXX_VISIBILITY_PRESET hidden
-  VISIBILITY_INLINES_HIDDEN ON
 )
 target_include_directories(matroska
   PRIVATE
@@ -87,6 +85,14 @@ target_sources(matroska
     ${CMAKE_CURRENT_BINARY_DIR}/matroska_export.h
 )
 
+# generate_export_header() tests hidden visibility. Use it.
+if(COMPILER_HAS_HIDDEN_VISIBILITY AND COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
+  set_target_properties(ebml PROPERTIES
+    CXX_VISIBILITY_PRESET hidden
+    VISIBILITY_INLINES_HIDDEN ON
+  )
+endif()
+
 if(NOT BUILD_SHARED_LIBS)
   target_compile_definitions(matroska PUBLIC MATROSKA_STATIC_DEFINE)
 endif()
