0

I need to load a library in a LISP script.

The script "loader.lisp" and the library "mylib.dll" are in the same folder "parent_dir".

If I run the script from inside the folder (current directory = "parent_dir") it works fine:

(load "loader.lisp") ;OK lib loaded successfully

but if the current directory is somewhere else, it fails to load (of course it looks for the lib in the wrong dir):

(load "parent_dir/loader.lisp") ;ERROR, of course I'm in the wrong working dir!

;Error opening shared object "mylib.dll":
;dlopen(mylib.dll, 10): image not found.

The "loader.lisp" script contains (also) the following code:

(setq LIB_PATH "mylib.dll")

(if (string= (software-type) "Darwin")
    (setq LIB_PATH "mylib_osx.lib"))

#+allegro
  (load LIB_PATH)
#+sbcl
  (sb-alien:load-shared-object LIB_PATH)

The question is: how can I make the loader.lisp script "working directory"-independent?

UPDATE: I specify that the script ant the interpreter executable are not in the same directory.

Thank you very much!

UnableToLoad
  • 315
  • 6
  • 18
  • What OS are you using? – Mark Karpov Jun 18 '14 at 11:55
  • the script can be run bot on osx or windows 7, actually there is also the following code to make it load the appropriate library, according to the os: (if (string= (software-type) "Darwin") (setq LIB_PATH "mylib_osx.lib")) – UnableToLoad Jun 18 '14 at 14:30

1 Answers1

1

I would suggest the following solutions:

  • Use full path;
  • Keep the library file always in the same directory as your program (well, sometimes it's not a bad idea);
  • Change parameter "start in" of your program;
  • Add path to your library file into OS variable "PATH".

It's rather a question about concept of OS working directory than about Lisp itself.

As far as I see solution involving "PATH" variable could be the best one. You can just test this method and tell us if it worked.

How to set path in Windows.

Mark Karpov
  • 7,499
  • 2
  • 27
  • 62
  • 1) I distribute the library, I can't hardcode the path. 2) is already there in the same folder 3) what do you mean? 4) Same as 1, since I distribute it, I would like to be as easy as possible for my users (many of them have a really basic computer knowledge). I know it's a working directory pb, I was wondering if there is a solution to make the life easier to my users... – UnableToLoad Jun 18 '14 at 14:35
  • @UnableToLoad, you can write script for your installer that will add path to your library into "PATH" variable. Then any program will be able to use your library (OS will "know" where it is). As for 2, it's quite strange, since programs usually search for files in directories where they placed first. – Mark Karpov Jun 18 '14 at 14:50
  • what do you mean about the parameter "start in"? I don't think it's quite strange, a "load" is relative to the current working directory of the interpreter, not to the path of the script in which it is invoked (that would be what I need...) – UnableToLoad Jun 20 '14 at 12:41
  • @UnableToLoad, on Windows, "start in" is a parameter that can be set in options of a shortcut or from [command line](http://superuser.com/questions/396394/how-do-i-set-an-executables-working-directory-via-the-command-line-prior-to-ex). It changes working directory of executing program. – Mark Karpov Jun 20 '14 at 12:52
  • @UnableToLoad, Oh, it's clearer now. I thought that you library is in the same directory as your .exe (interpreter). May be there is a way to get directory of your loader.lisp... I'll check it. Here is [this question](http://stackoverflow.com/questions/8409527/common-lisp-get-path-to-file), but it's pretty useless. I would do it with "PATH" variable anyway. – Mark Karpov Jun 20 '14 at 13:06
  • I also taught about ASDF, but I don't know if does what I need (or if it's an overkill). Using (truename ".") you can get the current working dir with SBCL, maybe we can need it. – UnableToLoad Jun 20 '14 at 13:58
  • About the path: it could still be not very user friendly (I have users who hardly understand the concept of "working directory" and who cannot use the bash...). – UnableToLoad Jun 20 '14 at 14:09
  • @UnableToLoad, Do you exclude possibility of using installer for your library? Something like Inno Setup. This way you can set path without user's participation. – Mark Karpov Jun 20 '14 at 14:12
  • That would be a good idea (or also a script to set the path for the current session). Thank you very much! – UnableToLoad Jun 20 '14 at 14:20