Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-other / 2017-03

How to set library search path ?

14 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Fred van Stappen

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>

Reply by email

silvioprog
to Fred van Stappen
Fred van Stappen wrote 14 lines
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

Reply by email

Unknown author
to Fred van Stappen: Hello. Is it possible to set the search-path for library without to edit-change the…
Fred van Stappen wrote 16 lines
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.

Reply by email

Fred van Stappen
to Unknown author

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 de la part de
Envoyé : dimanche 12 mars 2017 22:19
À : Other FPC related discussions
Objet : Re: [fpc-other] How to set library search path ?

Fred van Stappen wrote 16 lines
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.

Reply by email

Giuliano Colla
to Fred van Stappen: Hello. Is it possible to set the search-path for library without to edit-change the…

Il 12/03/2017 20:15, Fred van Stappen ha scritto:

Show quoted text 8 lines
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

Reply by email

Unknown author
to Giuliano Colla
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.

Reply by email

Fred van Stappen
to Unknown author

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:

Show quoted text 13 lines
> 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

Reply by email

silvioprog
to Fred van Stappen
Fred van Stappen 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

Reply by email

silvioprog
to silvioprog
silvioprog wrote 25 lines
On Mon, Mar 13, 2017 at 8:04 AM, Fred van Stappen 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

Reply by email

Fred van Stappen
to silvioprog

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 de la part de silvioprog
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 wrote: On Mon, Mar 13, 2017 at 8:04 AM, Fred van Stappen 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

Reply by email

silvioprog
to Fred van Stappen
Fred van Stappen wrote 8 lines
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

Reply by email

Fred van Stappen
to Marco van de Voort

@ 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

Reply by email