diff --git a/CMakeLists.txt b/CMakeLists.txt
index 13d11b909eb616f974ef7cfca5085d19f81bf6ee..04fddf5558d118d92e28b55f14b310d61f282be8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -347,11 +347,6 @@ if(NOT CMAKE_C_COMPILER_ID STREQUAL "MSVC")
   )
 endif()
 
-set(EXTRACFLAG)
-extract_valid_c_flags(EXTRACFLAG
-  -fvisibility=hidden
-)
-
 if(ENABLE_DEBUG)
   set(DEBUGBUILD 1)
 endif()
@@ -449,7 +444,6 @@ message(STATUS "summary of build options:
       CXXFLAGS:       ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_${_build_type}}
       WARNCFLAGS:     ${WARNCFLAGS}
       CXX1XCXXFLAGS:  ${CXX1XCXXFLAGS}
-      EXTRACFLAG:     ${EXTRACFLAG}
     Library:
       Shared:         ${enable_shared}
       Static:         ${enable_static}
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 7b012acbe843bf815e6b818528095f7bd0eaca5d..a6adc04dcd06f49c69f7aa2dc13975223b1c97dc 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -24,14 +24,24 @@ set(NGHTTP2_SOURCES
   nghttp2_http.c
 )
 
-add_library(nghttp2-obj OBJECT ${NGHTTP2_SOURCES})
+# Public shared library
+add_library(nghttp2 SHARED ${NGHTTP2_SOURCES})
 # Needed because the object files are linked into a shared library.
-set_target_properties(nghttp2-obj PROPERTIES
-  POSITION_INDEPENDENT_CODE ON)
-add_library(nghttp2 SHARED $<TARGET_OBJECTS:nghttp2-obj>)
 set_target_properties(nghttp2 PROPERTIES
-  COMPILE_FLAGS "${WARNCFLAGS} ${EXTRACFLAG}"
-  VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION})
+  COMPILE_FLAGS "${WARNCFLAGS}"
+  VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION}
+  C_VISIBILITY_PRESET hidden
+)
+
+if(HAVE_CUNIT)
+  # Static library (for unittests because of symbol visibility)
+  add_library(nghttp2_static STATIC ${NGHTTP2_SOURCES})
+  set_target_properties(nghttp2_static PROPERTIES
+    COMPILE_FLAGS "${WARNCFLAGS}"
+    VERSION ${LT_VERSION} SOVERSION ${LT_SOVERSION}
+    ARCHIVE_OUTPUT_NAME nghttp2
+  )
+endif()
 
 install(TARGETS nghttp2
   LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}")
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 08c89b0ecf034e6074b05204f49cc366312c57fb..1d6af25ca3f3105fe1e4c2d4a9c7d7e00dfd9f24 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -248,7 +248,6 @@ if(ENABLE_ASIO_LIB)
   )
   # XXX use add_compile_options with a list instead of COMPILE_FLAGS
   set_target_properties(nghttp2_asio PROPERTIES
-    COMPILE_FLAGS "${EXTRACFLAG}"
     VERSION 1.0.0 SOVERSION 1)
 
   install(TARGETS nghttp2_asio
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index 1ffa16c76a3c1c9a9066667121c4f2409967e140..5a6418345cae6ab797e1d738e201b193d998b4e6 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -26,10 +26,12 @@ if(HAVE_CUNIT)
 
   add_executable(main EXCLUDE_FROM_ALL
     ${MAIN_SOURCES}
-    $<TARGET_OBJECTS:nghttp2-obj>
   )
   target_include_directories(main PRIVATE ${CUNIT_INCLUDE_DIRS})
-  target_link_libraries(main ${CUNIT_LIBRARIES})
+  target_link_libraries(main
+    nghttp2_static
+    ${CUNIT_LIBRARIES}
+  )
   add_test(main main)
 
   if(ENABLE_FAILMALLOC)
@@ -40,9 +42,11 @@ if(HAVE_CUNIT)
     )
     add_executable(failmalloc EXCLUDE_FROM_ALL
       ${FAILMALLOC_SOURCES}
-      $<TARGET_OBJECTS:nghttp2-obj>
     )
-    target_link_libraries(failmalloc ${CUNIT_LIBRARIES})
+    target_link_libraries(failmalloc
+      nghttp2_static
+      ${CUNIT_LIBRARIES}
+    )
     add_test(failmalloc failmalloc)
   endif()