CMake: link shared libraries with -z defs (a.k.a. --no-undefined)

Also a best practice from "How To Write Shared Libraries"[1]

[1] https://akkadia.org/drepper/dsohowto.pdf

Signed-off-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Thiago Macieira
2025-03-18 16:43:48 -07:00
parent c3f5fac2cf
commit 060da248de
+8 -1
View File
@@ -40,6 +40,8 @@ endif()
# Include additional modules that are used unconditionally
include(GNUInstallDirs)
include(GenerateExportHeader)
include(CheckLinkerFlag)
include(CheckSymbolExists)
add_library(tinycbor
src/cborencoder.c
@@ -90,6 +92,12 @@ set_target_properties(tinycbor PROPERTIES
)
if(BUILD_SHARED_LIBS)
set_target_properties(tinycbor PROPERTIES C_VISIBILITY_PRESET hidden)
# Check if the linker supports "-z defs" (a.k.a "--no-undefined")
check_linker_flag(C "-Wl,-z,defs" HAVE_NO_UNDEFINED)
if(HAVE_NO_UNDEFINED)
target_link_options(tinycbor PRIVATE "-Wl,-z,defs")
endif()
else()
target_compile_definitions(tinycbor PUBLIC CBOR_STATIC_DEFINE)
endif()
@@ -98,7 +106,6 @@ endif()
generate_export_header(tinycbor BASE_NAME "cbor" EXPORT_MACRO_NAME "CBOR_API" EXPORT_FILE_NAME "tinycbor-export.h")
# Check for open_memstream and store the result in HAVE_OPEN_MEMSTREAM
include(CheckSymbolExists)
check_symbol_exists(open_memstream stdio.h HAVE_OPEN_MEMSTREAM)
check_symbol_exists(funopen stdio.h HAVE_OPEN_FUNOPEN)
check_symbol_exists(fopencookie stdio.h HAVE_OPEN_FOPENCOOKIE)