From 894f26bd873e816eca99c3f49ee3847a175ba51a Mon Sep 17 00:00:00 2001 From: Uri Simchoni Date: Mon, 9 Oct 2023 22:51:14 +0300 Subject: [PATCH] Hide non-API functions when position independent code is enabled Usage of -fvisibility=hidden is the recommended default for DSO's. If the spdlog build is for a shared library, then API symbols have SPDLOG_API marking on them, and cause the symbol to be exported, with other symbols now hidden by default. If the build is for a static library with position-independent code, then spdlog is to be embedded as implementation detail in another shared library or executable. In that case we don't want to export any symbol. Conveniently, SPDLPG_API in that case resolves to nothing, so with the default of hidden all symbols are made hidden. --- CMakeLists.txt | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index b2f6b61c..df2513d3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -141,6 +141,11 @@ if(SPDLOG_BUILD_PIC) set(CMAKE_POSITION_INDEPENDENT_CODE ON) endif() +if((${CMAKE_POSITION_INDEPENDENT_CODE}) OR (${CMAKE_CXX_FLAGS} MATCHES "-fPIC")) + set(CMAKE_CXX_VISIBILITY_PRESET hidden) + set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) +endif() + find_package(Threads REQUIRED) message(STATUS "Build type: " ${CMAKE_BUILD_TYPE}) # ---------------------------------------------------------------------------------------