From: sunwukong
Subject: CFFI and SLIME
Date: 
Message-ID: <1159090154.962240.272810@b28g2000cwb.googlegroups.com>
Hi,

I'm writing a C wrapper library for a C++ library to use it from Lisp.
I can load the wrapper library, if SBCL is run from shell, but not if
SBCL is run by SLIME. I have to first load the C++ library, and only
then can I load the wrapper library.

I'm using SBCL 0.9.16, the darcs version of CFFI and the CVS version of
SLIME.

A simplified example follows.

file a.hh:
----------
class A { public: void func(); };

file a.cc:
----------
#include <iostream>
#include "a.hh"
void A::func() { std::cout << "Hello World!" << std::endl; }

file b.cc:
----------
#include "a.hh"
extern "C" {
  void a_func() { A *a = new A(); a->func(); delete a; }
}

file Makefile:
--------------
all: libA.so libB.so

libA.so: a.cc
	g++ -o ·@ -shared $<

libB.so: b.cc
	g++ -o ·@ -shared -L. -lA $<


Output of SBCL running in a shell:
----------------------------------
* (require :cffi)
NIL
* (cffi:load-foreign-library "libB.so")
#P"/home/salvi/project/ab/libB.so"
* (cffi:defcfun a-func :void)
A-FUNC
* (a-func)
Hello World!
NIL

The same in SLIME:
------------------
CL-USER> (require :cffi)
NIL
CL-USER> (cffi:load-foreign-library "libB.so")
Unable to load foreign library: libB.so
   [Condition of type CFFI:LOAD-FOREIGN-LIBRARY-ERROR]
...
; Evaluation aborted
CL-USER> (cffi:load-foreign-library "libA.so")
#P"/home/salvi/project/ab/libA.so"
CL-USER> (cffi:load-foreign-library "libB.so")
#P"/home/salvi/project/ab/libB.so"
...

What am I doing wrong?

Thanks,

Peter

From: Pascal Bourguignon
Subject: Re: CFFI and SLIME
Date: 
Message-ID: <873bah2rel.fsf@thalassa.informatimago.com>
"sunwukong" <··········@gmail.com> writes:

> Hi,
>
> I'm writing a C wrapper library for a C++ library to use it from Lisp.
> I can load the wrapper library, if SBCL is run from shell, but not if
> SBCL is run by SLIME. I have to first load the C++ library, and only
> then can I load the wrapper library.
> [...]
> Output of SBCL running in a shell:
> ----------------------------------
> * (require :cffi)
> NIL
> * (cffi:load-foreign-library "libB.so")
> #P"/home/salvi/project/ab/libB.so"
> * (cffi:defcfun a-func :void)
> A-FUNC
> * (a-func)
> Hello World!
> NIL
>
> The same in SLIME:
> ------------------
> CL-USER> (require :cffi)
> NIL
> CL-USER> (cffi:load-foreign-library "libB.so")
> Unable to load foreign library: libB.so
>    [Condition of type CFFI:LOAD-FOREIGN-LIBRARY-ERROR]
> ...
> ; Evaluation aborted
> CL-USER> (cffi:load-foreign-library "libA.so")
> #P"/home/salvi/project/ab/libA.so"
> CL-USER> (cffi:load-foreign-library "libB.so")
> #P"/home/salvi/project/ab/libB.so"
> ...
>
> What am I doing wrong?

My get is that the current directory is not the same.  
Try to load the foreign library using an absolute path.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein
From: sunwukong
Subject: Re: CFFI and SLIME
Date: 
Message-ID: <1159164417.820310.236390@b28g2000cwb.googlegroups.com>
Pascal Bourguignon wrote:
> "sunwukong" <··········@gmail.com> writes:
>
> > I'm writing a C wrapper library for a C++ library to use it from Lisp.
> > I can load the wrapper library, if SBCL is run from shell, but not if
> > SBCL is run by SLIME. I have to first load the C++ library, and only
> > then can I load the wrapper library.
> > [...]
> > Output of SBCL running in a shell:
> > ----------------------------------
> > * (require :cffi)
> > NIL
> > * (cffi:load-foreign-library "libB.so")
> > #P"/home/salvi/project/ab/libB.so"
> > * (cffi:defcfun a-func :void)
> > A-FUNC
> > * (a-func)
> > Hello World!
> > NIL
> >
> > The same in SLIME:
> > ------------------
> > CL-USER> (require :cffi)
> > NIL
> > CL-USER> (cffi:load-foreign-library "libB.so")
> > Unable to load foreign library: libB.so
> >    [Condition of type CFFI:LOAD-FOREIGN-LIBRARY-ERROR]
> > ...
> > ; Evaluation aborted
> > CL-USER> (cffi:load-foreign-library "libA.so")
> > #P"/home/salvi/project/ab/libA.so"
> > CL-USER> (cffi:load-foreign-library "libB.so")
> > #P"/home/salvi/project/ab/libB.so"
>
> My get is that the current directory is not the same.
> Try to load the foreign library using an absolute path.

I've tried but it's the same. Thanks anyway.
Any other tips? I'm really puzzled with this one...

Peter
From: Frank Goenninger DG1SBG
Subject: Re: CFFI and SLIME
Date: 
Message-ID: <m2slig44mm.fsf@pcsde001.local>
"sunwukong" <··········@gmail.com> writes:

>> > The same in SLIME:
>> > ------------------
>> > CL-USER> (require :cffi)
>> > NIL
>> > CL-USER> (cffi:load-foreign-library "libB.so")
>> > Unable to load foreign library: libB.so
>> >    [Condition of type CFFI:LOAD-FOREIGN-LIBRARY-ERROR]
>> > ...
>> > ; Evaluation aborted
>> > CL-USER> (cffi:load-foreign-library "libA.so")
>> > #P"/home/salvi/project/ab/libA.so"
>> > CL-USER> (cffi:load-foreign-library "libB.so")
>> > #P"/home/salvi/project/ab/libB.so"
>>
> I've tried but it's the same. Thanks anyway.
> Any other tips? I'm really puzzled with this one...
>
> Peter

What is the path to the library on your system? Could you post this
here? Thx!

Also, please look at cffi:define-foreign-library and
cffi:use-foreign-library.

Best,
   Frank
From: sunwukong
Subject: Re: CFFI and SLIME
Date: 
Message-ID: <1159199472.009803.259650@m73g2000cwd.googlegroups.com>
Frank Goenninger DG1SBG wrote:
> "sunwukong" <··········@gmail.com> writes:
>
> >> > The same in SLIME:
> >> > ------------------
> >> > CL-USER> (require :cffi)
> >> > NIL
> >> > CL-USER> (cffi:load-foreign-library "libB.so")
> >> > Unable to load foreign library: libB.so
> >> >    [Condition of type CFFI:LOAD-FOREIGN-LIBRARY-ERROR]
> >> > ...
> >> > ; Evaluation aborted
> >> > CL-USER> (cffi:load-foreign-library "libA.so")
> >> > #P"/home/salvi/project/ab/libA.so"
> >> > CL-USER> (cffi:load-foreign-library "libB.so")
> >> > #P"/home/salvi/project/ab/libB.so"
>
> What is the path to the library on your system? Could you post this
> here? Thx!

It's the /home/salvi/project/ab/ directory that holds both libA.so and
libB.so, if I understand your question correctly.

> Also, please look at cffi:define-foreign-library and
> cffi:use-foreign-library.

That was my first try, but when it didn't succeed I've decided that
load-foreign-library (being shorter) is more convenient for testing :)

Anyway, if any of you wants to play around with this, here's a little
tarball with the test program:
http://people.inf.elte.hu/salvi/download/problem.tar.gz
I would like to know if anyone has the same problem.

Peter
From: Frank Goenninger DG1SBG
Subject: Re: CFFI and SLIME
Date: 
Message-ID: <m2k63r4zgc.fsf@pcsde001.local>
"sunwukong" <··········@gmail.com> writes:

>
> It's the /home/salvi/project/ab/ directory that holds both libA.so and
> libB.so, if I understand your question correctly.
>
>> Also, please look at cffi:define-foreign-library and
>> cffi:use-foreign-library.
>
> That was my first try, but when it didn't succeed I've decided that
> load-foreign-library (being shorter) is more convenient for testing
> :)

Those work reliably over here.
>
> Anyway, if any of you wants to play around with this, here's a little
> tarball with the test program:
> http://people.inf.elte.hu/salvi/download/problem.tar.gz
> I would like to know if anyone has the same problem.
>
> Peter

I played aorund with the tarball and get a clean load. 
The steps with SLIME are:

At the prompt
CL-USER > 

type ,  (= Comma)

In the minibuffer of Emacs you'll see:

Command:

as the prompt. Now type:

change-directory [RETURN]

Then you see 

Directory: 

as the prompt. Here you enter the path to your "ab" directory. If this
succeeds then SLIME will show you

default-directory: .../ab

as a message in the minibuffer of Emacs.

Now do:

CL-USER > (cffi:load-foreign-library "libB.so")
; Foreign loading libB.so
T
CL-USER >

That's how it works for me. If you don't want to change path in SLIME
please specify the full absolute path to your lib. If that doesn't
work then you have to load libA.so before libB.so. If that doesn't
work then you have to reinstall your computer ;-)

HTH

  Frank 
From: sunwukong
Subject: Re: CFFI and SLIME
Date: 
Message-ID: <1159207622.172504.56800@m7g2000cwm.googlegroups.com>
> CL-USER > (cffi:load-foreign-library "libB.so")
> ; Foreign loading libB.so
> T
> That's how it works for me.

Good for you :)

> If you don't want to change path in SLIME
> please specify the full absolute path to your lib. If that doesn't
> work then you have to load libA.so before libB.so. If that doesn't
> work then you have to reinstall your computer ;-)

It seems I have to reinstall my computer... but it's good to know that
it works with other setups.

Thank you,

Peter
From: Ken Tilton
Subject: Re: CFFI and SLIME
Date: 
Message-ID: <nKWRg.5$hN1.1@newsfe10.lga>
sunwukong wrote:
>>CL-USER > (cffi:load-foreign-library "libB.so")
>>; Foreign loading libB.so
>>T
>>That's how it works for me.
> 
> 
> Good for you :)
> 
> 
>>If you don't want to change path in SLIME
>>please specify the full absolute path to your lib. If that doesn't
>>work then you have to load libA.so before libB.so. If that doesn't
>>work then you have to reinstall your computer ;-)
> 
> 
> It seems I have to reinstall my computer...

Another productive day in the life of "free" software.

:)

kt

-- 
Cells: http://common-lisp.net/project/cells/

"I'll say I'm losing my grip, and it feels terrific."
    -- Smiling husband to scowling wife, New Yorker cartoon