Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #89470

Re: Link at runtime instead of build-time

From Christian Gollwitzer <auriocus@gmx.de>
Newsgroups comp.lang.c++
Subject Re: Link at runtime instead of build-time
Date 2023-03-06 09:29 +0100
Organization A noiseless patient Spider
Message-ID <tu489m$1kqqq$2@dont-email.me> (permalink)
References <6e54715f-9094-46c2-94ec-82aa9d7eaccdn@googlegroups.com> <tu2m2g$1ddsn$1@dont-email.me> <bd9d76af-a28d-4c27-9b1b-4246cc25a8e4n@googlegroups.com> <tu32id$1en3f$1@dont-email.me> <35a8ec8a-bb4a-480e-a04b-4db8e3f3614an@googlegroups.com>

Show all headers | View raw


Am 06.03.23 um 01:10 schrieb Frederick Virchanza Gotham:
> On Sunday, March 5, 2023 at 9:46:04 PM UTC, Christian Gollwitzer wrote:
>>
>> If you need a lot of the functionality of the main program in the
>> library, then instead put this functionality in a library if its own
>> (say, libengine.so).
> 
> 
> My program has to be one statically-linked executable file.
> 
> I found the following doing a web search:
> 
> https://stackoverflow.com/questions/20658809/dynamic-loading-and-weak-symbol-resolution
> 
> If you scroll down on that page, a guy called Praxeolitic writes "Here is an example in which the executable itself has an undefined symbol that is resolved through dynamic loading", and he gives a code example. His code example is /exactly/ what I'm trying to achieve, but it doesn't work on modern Linux. It seems to have worked 9 years ago, but something has changed since then. I've tried using gcc-4.8 on modern Linux but the executable I get doesn't work.
> 
> Does anyone know what might have changed in Linux in the past 10 years to stop this working? Or does anyone have an email address for Praxeolitic? Anyone know who Praxeolitic is?
> 
> I think I'll download an old copy of Ubuntu from 10 years ago and run it in a VM and try this out.

Have you seen the comment below this message from 2018, that the dynamic 
linker only respects weak symbols when you set LD_DYNAMIC_WEAK? Quoting 
from the referenced page:

LD_DYNAMIC_WEAK (since glibc 2.1.91)
               By default, when searching shared libraries to resolve a
               symbol reference, the dynamic linker will resolve to the
               first definition it finds.

               Old glibc versions (before 2.2), provided a different
               behavior: if the linker found a symbol that was weak, it
               would remember that symbol and keep searching in the
               remaining shared libraries.  If it subsequently found a
               strong definition of the same symbol, then it would
               instead use that definition.  (If no further symbol was
               found, then the dynamic linker would use the weak symbol
               that it initially found.)

               The old glibc behavior was nonstandard.  (Standard
               practice is that the distinction between weak and strong
               symbols should have effect only at static link time.)  In
               glibc 2.2, the dynamic linker was modified to provide the
               current behavior (which was the behavior that was provided
               by most other implementations at that time).

               Defining the LD_DYNAMIC_WEAK environment variable (with
               any value) provides the old (nonstandard) glibc behavior,
               whereby a weak symbol in one shared library may be
               overridden by a strong symbol subsequently discovered in
               another shared library.  (Note that even when this
               variable is set, a strong symbol in a shared library will
               not override a weak definition of the same symbol in the
               main program.)

               Since glibc 2.3.4, LD_DYNAMIC_WEAK is ignored in secure-
               execution mode.


This sounds very hackish to me. Most portable (and easiest) solution 
will be a shell script. The overall goal reminds me to the Tk toolkit. 
In Tcl, you can do "package require Tk" which loads the Tk toolkit 
dynamically. However, there are at least two files - Tk is its own 
shared object, which depends on libX11 etc., while Tcl alone does not 
depend on libX11. For a single file executable (called starpack) they go 
through some hoops, especially attaching a load to the end of the 
executable (similar to a self-extracting ZIP file), which is addressed 
as a virtual file system. Are you sure it is worth to go through this 
for your program? The simplest solution (besides a shell script) would 
be to split the GUI part into its own library, which is installed as a 
separate package.

	Christian

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Link at runtime instead of build-time Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-03-05 09:59 -0800
  Re: Link at runtime instead of build-time Christian Gollwitzer <auriocus@gmx.de> - 2023-03-05 19:12 +0100
    Re: Link at runtime instead of build-time Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-03-05 11:11 -0800
      Re: Link at runtime instead of build-time Christian Gollwitzer <auriocus@gmx.de> - 2023-03-05 22:45 +0100
        Re: Link at runtime instead of build-time Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-03-05 16:10 -0800
          Re: Link at runtime instead of build-time Christian Gollwitzer <auriocus@gmx.de> - 2023-03-06 09:29 +0100
            Re: Link at runtime instead of build-time Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-03-06 02:37 -0800
      Re: Link at runtime instead of build-time "Alf P. Steinbach" <alf.p.steinbach@gmail.com> - 2023-03-06 15:45 +0100
        Re: Link at runtime instead of build-time Muttley@dastardlyhq.com - 2023-03-06 17:14 +0000
          Re: Link at runtime instead of build-time Frederick Virchanza Gotham <cauldwell.thomas@gmail.com> - 2023-03-06 13:41 -0800
            Re: Link at runtime instead of build-time gazelle@shell.xmission.com (Kenny McCormack) - 2023-03-06 22:20 +0000
            Re: Link at runtime instead of build-time Christian Gollwitzer <auriocus@gmx.de> - 2023-03-06 23:58 +0100

csiph-web