Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #89450
| From | Christian Gollwitzer <auriocus@gmx.de> |
|---|---|
| Newsgroups | comp.lang.c++ |
| Subject | Re: Link at runtime instead of build-time |
| Date | 2023-03-05 19:12 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <tu2m2g$1ddsn$1@dont-email.me> (permalink) |
| References | <6e54715f-9094-46c2-94ec-82aa9d7eaccdn@googlegroups.com> |
Am 05.03.23 um 18:59 schrieb Frederick Virchanza Gotham:
> Let's say I have one program and one shared library on a *nix computer.
>
> I build my program to link dynamically with the library as follows:
>
> g++ -o prog prog.cpp -l:libmonkey.so
>
> So let's say that this produces an executable file that works fine.
>
> Next I change the build command to the following:
>
> g++ -o prog prog.cpp
>
> And so now of course I get a linker error because of unresolved symbols (I'm missing the functions implemented in the library). To overcome this linker error, I change the build command again:
>
> g++ -o prog prog.cpp -Wl,--unresolved-symbols=ignore-in-object-files
>
> So now we get a viable executable file, but when we run it, it segfaults. It segfaults when it tries to access something from the shared library.
>
> So next I make a one-line change at the beginning of 'main':
>
> int main(void)
> {
> dlopen("libmonkey.so", RTLD_NOW|RTLD_GLOBAL);
>
> // The rest of main goes here
> }
>
> So now the program works fine.
>
> I think however, that this strategy only works when the program doesn't use global variables which are exported from the library. That is to say, it works fine for exported functions but not for exported global variables. Am I right here?
>
> I have more testing to do on this, but I need to get this working. I need to remove the library from the 'NEEDED' section in the program's ELF file, and instead I have to use 'dlopen' at runtime to load it in.
As always, I'm questioning this NEED. Why do you want to load the
required library at runtime? Do you want to adjust the path at
initialization? If it is only about packaging the library with the
application, you could use the run-path when linking; e.g. say your
library is distributed in a lib folder alongside your executable, then
-Wl,-rpath,'$ORIGIN'/lib
in the linker line would do that. Another alternative is to write a
small shell script which adjusts LD_LIBRARY_PATH before invoking the binary.
I can see the need for dlopen() only in case where the functionality is
somewhat optional, like in a plugin system.
Christian
Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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