Discussion:
[fpc-other] How to set library search path ?
Fred van Stappen
2017-03-12 19:15:39 UTC
Permalink
Hello.


Is it possible to set the search-path for library without to edit-change the config file ?


For example, in Linux, you should edit .bash_profile and add:

LD_LIBRARY_PATH=/usr/local/lib
export LD_LIBRARY_PATH

But is it possible to set that library path without to edit a file, only by command (via a TProcess) ?


And if it is possible for Linux, how to do for Windows and FreeBSD and Mac OS (and other os are welcome).


Thanks.


Fre;D

<http://sites.google.com/site/fiensprototyping/home>
n***@z505.com
2017-03-12 21:19:52 UTC
Permalink
On 2017-03-12 14:15, Fred van Stappen wrote:
> Hello.
>
> Is it possible to set the search-path for library without to
> edit-change the config file ?
>
> For example, in Linux, you should edit .bash_profile and add:
>
> LD_LIBRARY_PATH=/usr/local/lib
> export LD_LIBRARY_PATH
> But is it possible to set that library path without to edit a file,
> only by command (via a TProcess) ?
>
> And if it is possible for Linux, how to do for Windows and FreeBSD and
> Mac OS (and other os are welcome).
>
> Thanks.


SetEnv works only in the program that's running, AFAIK, but may work for
what you need?

i.e. setenv is applied during the program lifetime, AFAIK, and then once
the program ends the env you set is no longer set any more. But, I'm
inexperienced with this - that's what I've read.
_______________________________________________
fpc-other maillist - fpc-***@lists.freepascal.org
http://list
Fred van Stappen
2017-03-12 22:37:55 UTC
Permalink
Hello and thanks for answer.


> SetEnv works only in the program that's running, AFAIK, but may work for what you need?


It is exactly what I want, only for the program that is running.

But how do you do to use SetEnv ?


When using a TProcess, there is a SetEnv parameter, I did not know that it can be set for the main program. (or I miss something).


I ask this because if you use LoadLibrary(alibrary) (= dynamic loading) and if alibrary has dependencies, those dependencies must be in the defined library search path.


But that library search path needs root access if you want to paste something on it.


And I do not want to oblige the users to change something in their system or to have root permission to use my program.


Fre;D



________________________________
De : fpc-other <fpc-other-***@lists.freepascal.org> de la part de ***@z505.com <***@z505.com>
Envoyé : dimanche 12 mars 2017 22:19
À : Other FPC related discussions
Objet : Re: [fpc-other] How to set library search path ?

On 2017-03-12 14:15, Fred van Stappen wrote:
> Hello.
>
> Is it possible to set the search-path for library without to
> edit-change the config file ?
>
> For example, in Linux, you should edit .bash_profile and add:
>
> LD_LIBRARY_PATH=/usr/local/lib
> export LD_LIBRARY_PATH
> But is it possible to set that library path without to edit a file,
> only by command (via a TProcess) ?
>
> And if it is possible for Linux, how to do for Windows and FreeBSD and
> Mac OS (and other os are welcome).
>
> Thanks.


SetEnv works only in the program that's running, AFAIK, but may work for
what you need?

i.e. setenv is applied during the program lifetime, AFAIK, and then once
the program ends the env you set is no longer set any more. But, I'm
inexperienced with this - that's what I've read.
_______________________________________________
fpc-other maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-other
fpc-other Info Page - Free Pascal<http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-other>
lists.freepascal.org
NOTE ***** Due to incidents with spambots subscribing to the list and spamming it, your first message(s) may be delayed a bit before ...
n***@z505.com
2017-03-12 23:18:10 UTC
Permalink
On 2017-03-12 17:37, Fred van Stappen wrote:
> But how do you do to use SetEnv ?

if you call SetEnv in the main program, I guess it doesn't affect the
program inside TProcess?

I may see your problem now :-)

What's this do?

http://lazarus-ccr.sourceforge.net/docs/fcl/process/tprocess.environment.html

http://forum.lazarus.freepascal.org/index.php?topic=7819.0
_______________________________________________
fpc-other maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/l
Giuliano Colla
2017-03-12 23:25:46 UTC
Permalink
Il 12/03/2017 20:15, Fred van Stappen ha scritto:

> But is it possible to set that library path without to edit a file,
> only by command (via a TProcess) ?
>
>
> And if it is possible for Linux, how to do for Windows and FreeBSD and
> Mac OS (and other os are welcome).
>
>

If the libraries are needed to run the program, you cannot use a
TProcess in your application, which would be executed after the loader
has loaded the program.
What you need is something which tells the loader where to search the
libraries required by the program.

As far as Linux and FreeBSD are concerned, my suggestion would be that
you launch your application from a shell script, which does the work.
Sort of an executable file called MyProg.sh (for example) whose content
would be something like:

#!/bin/sh
mylibpath=/usr/special/lib
export LD_LIBRARY_PATH="$mylibpath:$LD_LIBRARY_PATH"
/Path/to/executable/MyProg

That way the environment variable will be properly set to include your
specific library path (together with anything else your end user had
set) before you start the program, and the loader will know where to
look for libraries.

On Mac Os you could do in principle something similar, but you should
get advice from those which are familiar with this OS.
On Windows you might use a .bat file, but again you'd need advice from
Windows users.

Giuliano


_______________________________________________
fpc-other maillist - fpc-***@lists.freepascal.org
http://lists.fr
n***@z505.com
2017-03-12 23:35:43 UTC
Permalink
On 2017-03-12 18:25, Giuliano Colla wrote:
> As far as Linux and FreeBSD are concerned, my suggestion would be that
> you launch your application from a shell script, which does the work.
> Sort of an executable file called MyProg.sh

What happens if you use a function that launches the shell and just run
the exe/elf this way?

http://freepascal.org/docs-html/rtl/unix/fpsystem.html

And not sure if this would help:

http://freepascal.org/docs-html/rtl/unix/fpexeclpe.html

But these above functions do not have the features of TProcess

Not sure what would happen if you ran tprocess with "cmd /c" before the
exe name, if that's even possible, and somehow setenv on that shell
that cmd /c was working with (and on unix, run sh in tprocess)

Again I have not tested any of this, sorry.

_______________________________________________
fpc-other maillist - fpc-***@lists.freepascal.org
http://lists.fre
Fred van Stappen
2017-03-13 11:04:40 UTC
Permalink
Hello and thanks to Silvio, z505, Giuliano.

> Did you try the TProcess.Environment? :-)

Of course but, afaik, it is not what I want.

I will try to explain with a concrete example (but I have much other in same case).

A project (uos) use c libraries.

Mainly all that libraries are "run-alone" (only the called library is needed).

But there are some libraries that have dependencies.

For example libopusfile.so needs libopus.so.

If the main program load such library with loadlibrary(/my/directory/libopusfile.so) ---> it will not work, even if libopus.so is in the same directory that libopusfile.so.
And even if doing loadlibrary(/my/directory/opusfile.so) before loadlibrary(/my/directory/libopusfile.so ) it will nor work because there are no correspondance with the 2 loadlibrary() .

And this is the challenge, how to load that dependeny if they are not in the lib-system directory.

> Another way is passing before compiling: $ LD_LIBRARY_PATH=some-path make.

Yes but for that the directory of dependencies must be known. In some case that dependencies come from elsewhere.

> launch your application from a shell script, which does the work.

Ok, but it is much more complicate for end-user.

What I need is, like for TProcess.Environment.Textvariable, a environment variable for LoadLibrary().

In a previous topic : http://free-pascal-general.1045716.n5.nabble.com/uses-myunit-in-src-myunit-pas-td5727626.html

Michael answered:

>> So it is exactly what I propose:

>> a TLoadLibrary with some features adapted for libraries that TProcess uses
>> for executables (like environnement).
>> Of course TLoadLibrary will have a LoadLibrary() and SafeLoadLibrary() but
>> some properties too.

>> What do you think ?

> I don't see the point at all.
> Why would you copy functionality that the OS provides out of the box ?

> Michael.

Huh, what are those functionality that the OS provides out of the box ?

Fre;D
silvioprog
2017-03-13 15:19:13 UTC
Permalink
On Mon, Mar 13, 2017 at 8:04 AM, Fred van Stappen <***@hotmail.com> wrote:

> Hello and thanks to Silvio, z505, Giuliano.
> [...]
>
And this is the challenge, how to load that dependeny if they are not in
> the lib-system directory.
>

Strange. I'm using loadlibrary/dlsym and both works properly here. o.O
Could you test the attached example? See its result on my Linux:

$ make
Free Pascal Compiler version 3.1.1 [2017/03/12] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling test/test.pp
Linking test/test
38 lines compiled, 0.1 sec

$ make test
I'm lib A
I'm lib B

--
Silvio Clécio
silvioprog
2017-03-13 16:10:17 UTC
Permalink
On Mon, Mar 13, 2017 at 12:19 PM, silvioprog <***@gmail.com> wrote:

> On Mon, Mar 13, 2017 at 8:04 AM, Fred van Stappen <***@hotmail.com>
> wrote:
>
>> Hello and thanks to Silvio, z505, Giuliano.
>> [...]
>>
> And this is the challenge, how to load that dependeny if they are not in
>> the lib-system directory.
>>
>
> Strange. I'm using loadlibrary/dlsym and both works properly here. o.O
> Could you test the attached example? See its result on my Linux:
>
> $ make
> Free Pascal Compiler version 3.1.1 [2017/03/12] for x86_64
> Copyright (c) 1993-2017 by Florian Klaempfl and others
> Target OS: Linux for x86-64
> Compiling test/test.pp
> Linking test/test
> 38 lines compiled, 0.1 sec
>
> $ make test
> I'm lib A
> I'm lib B
>

Example II (using both loadlibrary and dlsym):

- fpc loads liba/liba and calls their funcs;
- liba loads dep libb and calls its func;
- libb loads dep liba and calls its func.

Result:

$ make test
I'm lib A
a.dep: I'm lib B
I'm lib B
b.dep: I'm lib A

I think this test above simulates a plugin/module dependence environment.
:-)

--
Silvio Clécio
Fred van Stappen
2017-03-13 17:16:24 UTC
Permalink
Wow, many thanks Silvio.


OK, your demo works.


But what about if liba has dependencies with no path defined:


---> handle = dlopen("../libb/libb.so", RTLD_LAZY); ---> ok it works but the path was defined.


But if no path was defined, IMO it will work only if libb.so is in /usr/lib/ or usr/local/lib.


And that is the case of many libraries. There is no path defined for the dependencies.


And it is the challenge, how to define a different path than /usr/lib/ or usr/local/lib.


Many thanks.


Fre;D


________________________________
De : fpc-other <fpc-other-***@lists.freepascal.org> de la part de silvioprog <***@gmail.com>
Envoyé : lundi 13 mars 2017 17:10
À : Other FPC related discussions
Objet : Re: [fpc-other] How to set library search path ?

On Mon, Mar 13, 2017 at 12:19 PM, silvioprog <***@gmail.com<mailto:***@gmail.com>> wrote:
On Mon, Mar 13, 2017 at 8:04 AM, Fred van Stappen <***@hotmail.com<mailto:***@hotmail.com>> wrote:

Hello and thanks to Silvio, z505, Giuliano.
[...]
And this is the challenge, how to load that dependeny if they are not in the lib-system directory.

Strange. I'm using loadlibrary/dlsym and both works properly here. o.O Could you test the attached example? See its result on my Linux:

$ make
Free Pascal Compiler version 3.1.1 [2017/03/12] for x86_64
Copyright (c) 1993-2017 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling test/test.pp
Linking test/test
38 lines compiled, 0.1 sec

$ make test
I'm lib A
I'm lib B

Example II (using both loadlibrary and dlsym):

- fpc loads liba/liba and calls their funcs;
- liba loads dep libb and calls its func;
- libb loads dep liba and calls its func.

Result:

$ make test
I'm lib A
a.dep: I'm lib B
I'm lib B
b.dep: I'm lib A

I think this test above simulates a plugin/module dependence environment. :-)

--
Silvio Clécio
silvioprog
2017-03-13 17:38:38 UTC
Permalink
On Mon, Mar 13, 2017 at 2:16 PM, Fred van Stappen <***@hotmail.com> wrote:

> But what about if liba has dependencies with no path defined:
>
> ---> handle = dlopen("../libb/libb.so", RTLD_LAZY); ---> ok it works but
> the path was defined.
>
> But if no path was defined, IMO it will work only if libb.so is in
> /usr/lib/ or usr/local/lib.
>
What about this new attached example (III)?

- fpc loads liba/liba and calls their funcs;
- liba loads dep libb from a path predefined in the app and calls its func;
- libb loads dep liba from a path predefined in the app and calls its func.

If that doesn't work either, I don't know what to do, because there is no
way the system guess where your libraries are located. o.o'

--
Silvio Clécio
silvioprog
2017-03-12 19:21:50 UTC
Permalink
On Sun, Mar 12, 2017 at 4:15 PM, Fred van Stappen <***@hotmail.com> wrote:
>
> Hello.
>
> Is it possible to set the search-path for library without to edit-change
> the config file ?
>
> For example, in Linux, you should edit .bash_profile and add:
>
> LD_LIBRARY_PATH=/usr/local/lib
> export LD_LIBRARY_PATH
>
> But is it possible to set that library path without to edit a file, only
> by command (via a TProcess) ?
>
[...]

Did you try the TProcess.Environment? :-)

Another way is passing before compiling: $ LD_LIBRARY_PATH=some-path make
blah

--
Silvio Clécio
Marco van de Voort
2017-03-13 19:09:47 UTC
Permalink
In our previous episode, Fred van Stappen said:
>
> Is it possible to set the search-path for library without to edit-change the config file ?

No, since then having a search path at all would provide no security value
at all, since all "infected" programs could just change it.
_______________________________________________
fpc-other maillist - fpc-***@lists.freepascal.org
http://lists.free
Fred van Stappen
2017-03-13 20:15:48 UTC
Permalink
@ Silvio, I extremely appreciate your help. Many thanks.


>> Is it possible to set the search-path for library without to edit-change the config file

> No, since then having a search path at all would provide no security valueat all, since all "infected" programs could just change it.


Aaaargh, this is a very sad news (even if I understand why).


Hum, I have try this with a TProcess, without root access, before to LoadLibrary(lib_with_dependency) and it seems to work in Linux:


echo "export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/the/path/of/dependency" >> ~/.bashrc

What do you think about it ?


Many thanks for help.


Fre;D
Continue reading on narkive:
Loading...