From: Luca Boccassi <[email protected]>

Detect the SONAME at build time, and provide it as a variable for
programs to use, to avoid harcoding.
---
 m4/libdl.m4 | 20 +++++++++++++++++---
 1 file changed, 17 insertions(+), 3 deletions(-)

diff --git a/m4/libdl.m4 b/m4/libdl.m4
index b24fad832b..98664982ba 100644
--- a/m4/libdl.m4
+++ b/m4/libdl.m4
@@ -1,5 +1,5 @@
 # libdl.m4
-# serial 4
+# serial 5
 dnl Copyright (C) 2024-2026 Free Software Foundation, Inc.
 dnl This file is free software; the Free Software Foundation
 dnl gives unlimited permission to copy and/or distribute it,
@@ -8,6 +8,18 @@ dnl This file is offered as-is, without any warranty.
 
 dnl From Bruno Haible.
 
+dnl Set VARIABLE to the shared library name matching REGEXP in the executable
+dnl that AC_LINK_IFELSE has just linked. readelf works when cross-compiling,
+dnl ldd works on more native platforms.
+AC_DEFUN([gl_DLOPEN_SONAME],
+[
+  $1=$(
+    (LC_ALL=C readelf -d conftest$EXEEXT ||
+     LC_ALL=C ldd conftest$EXEEXT) 2>/dev/null |
+      sed -n 's/.*\($2\).*/\1/p'
+  )
+])
+
 dnl Set LIBDL to '-ldl' if it is needed to use the functions declared
 dnl in <dlfcn.h> (dlopen, dlsym, etc.), or to empty otherwise.
 dnl Set HAVE_DLOPEN to 1 if dlopen and dlsym are available, or to 0 otherwise.
@@ -21,7 +33,8 @@ AC_DEFUN([gl_LIBDL],
     [gl_cv_lib_dl],
     [AC_LINK_IFELSE(
        [AC_LANG_PROGRAM(
-          [[#include <dlfcn.h>
+          [[#include <stddef.h>
+            #include <dlfcn.h>
           ]],
           [[void *handle = dlopen (NULL, RTLD_LAZY);
             return ! (handle && dlsym (handle, "main"));]])],
@@ -32,7 +45,8 @@ AC_DEFUN([gl_LIBDL],
        LIBS="$LIBS -ldl"
        AC_LINK_IFELSE(
          [AC_LANG_PROGRAM(
-            [[#include <dlfcn.h>
+            [[#include <stddef.h>
+              #include <dlfcn.h>
             ]],
             [[void *handle = dlopen (NULL, RTLD_LAZY);
               return ! (handle && dlsym (handle, "main"));]])],
-- 
2.47.3


Reply via email to