help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Interface for C libs in Octave


From: Jaroslav Hajek
Subject: Re: Interface for C libs in Octave
Date: Thu, 24 Jun 2010 11:01:13 +0200

On Thu, Jun 24, 2010 at 10:41 AM, TOT <address@hidden> wrote:
>
> Is here anyone who can help me with interface my library with Octave?
> Please help me!
>
> The behaving of my library is very much the same as libpng.
> Except it reads and produces other kind of binaries.
>
> So if anyone can show me how to interface to libpng or to some other that
> kind of libraries,
> then I can reproduce it for my library.
>
> I promise that I'll post here a tutorial about my work, once I'll finish it.
>
> Please help!
>
> Now I do the following steps:
>
> 1. I compiled shared library using -fPIC option (the library was static).
>
> 2. I checked it using ldd and nm for a function which I need to call from
> Octave:
>    nm libmyclib.so | grep -i myfunc, and get:
>    0000000000020130 T myfunc
>
>    That means that the symbols are there and my function can be recognized
> easily.
>
> 3. Let me call this function myfunc.
>                   definition of it is:
>                                          double myfunc();
>   so it takes no parameter and returns a double precision floating point
> value.
>
> 4. The lib is now in /opt/myclib/lib and header file is in
> /opt/myclib/include
>
> 5. Then I go to my extension file for Octave:
>                    #include <octave/oct.h>
>                    extern "C" {
>                    #include <myclib.h>
>                    // I tried this double myfunc() too;
>                    }
>                    //And this too: #include <myclib.h>
>                    DEFUN_DLD (myfunction, args, nargout,
>                    "My function from myclib")
>                    {
>                     int nargin = args.length ();
>                     double t = 1.0;
>                     t = cddx_wtime();
>                     int n = 1;
>                     n = (int) t;
>                     octave_stdout << "Hello World has " << nargin
>                     << " input arguments and "
>                     << nargout << " output arguments.\n"
>                     << "and the time is" << n << " !\n";
>                     return octave_value_list ();
>                     }
> 6. I use mkoctfile together with all include and libs paths and list of
> libraries.
> 7. Everything compiles fine. But when I do nm simple.oct | grep -i myfunc I
> get U myfunc which means that my function is undefined... I get the same
> error in the octave window (undefined symbol).
> 8. I tried to use generic calls (from verbose output of mkoctave):
>    g++ -c -fPIC -I/usr/include/octave-3.0.5
> -I/usr/include/octave-3.0.5/octave -O2 -g -I/opt/myclib/include -I.
> simple.cc -o simple.o
>    g++ -fPIC -shared -Wl,-Bsymbolic -o simple.oct simple.o
> -L/usr/lib/octave-3.0.5 -loctinterp -loctave -lcruft
> -Wl,-Bsymbolic-functions -llapackgf-3 -lblas-3gf -lfftw3 -lreadline
> -lncurses -ldl -lhdf5 -lz -lm -lgfortranbegin -lgfortran -L/opt/myclib/lib/
> -Bdynamic -lmyclib -lgomp -lpthread
> 9. The result is the same :-(
>
> Please point me to the solution of this problem!
> You are my only hope indeed...
>
> Thanks in Advance and Best Regards,
> Anar Z. Yusifov.
>

The problem is that you incorrectly assume what -L does. -L includes
the specified directory in searching for libs during the link phase,
but does not store this path anywhere in the executable.
I suggest you get familiar with ldd, which shows you the shared
dependencies of an ELF library or executable. see man ldd for more.

type
ldd simple.oct

and check out the result. Most likely you will see
mylib.so => not found
in there. This means that simple.oct itself doesn't have sufficient
info to find this dependency.
You have several options:

1. install mylib.oct in a standard location, like /usr/local/lib. It
should be found there.
2. add /opt/mylib/lib into /etc/ld.so.conf

after both 1 and 2, you need to run ldconfig to update linker cache

3. hardwire the path to mylib.so into simple.oct. This is most useful
if you're compiling the binary for exclusively local use.
It can be done by the option -Wl,-rpath=/opt/mylib/lib
(-Wl forwards a compiler option to the linker)

hth

-- 
RNDr. Jaroslav Hajek, PhD
computing expert & GNU Octave developer
Aeronautical Research and Test Institute (VZLU)
Prague, Czech Republic
url: www.highegg.matfyz.cz



reply via email to

[Prev in Thread] Current Thread [Next in Thread]