Re: Undefined procedure on foreign library in 6.6.4

Jan Wielemaker <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
On 04/02/2014 04:54 PM, jon judge wrote:
> Hi,
>
> I successfully installed 6.6.4 using your apt-add-repository
> ppa:swi-prolog/stable url (not from source),  All worked perfectly first
> time - I can run swipl.
>
> But now I'm hitting issues accessing a foreign library predicate that
> appears to build correctly. I've removed nearly all the code to simplify /
> reproduce:
>
> #include <SWI-Prolog.h>
>
> static foreign_t testfo(void)
> {
>	PL_succeed;
> }
>
> install_t install_libtestfo()
> {
>   PL_register_foreign("testfo", 0, testfo, 0);
> }
>
> When I build the above from eclipse as a shared object, it compiles no
> problem, creating the shared object:
>
> -rwxr-xr-x 1 jon  jon    41531 Apr  2 15:18 libtestfo.so
>
> Now I link to swipl:
>
> # swipl-ld -v -shared -o libtestfo ../testfo.c
>	eval `swipl --dump-runtime-variables`
>		CC="gcc"
>		PLBASE="/usr/lib/swi-prolog"
>		PLARCH="i386"
>		PLLIBS=""
>		PLLIB="-lswipl"
>		PLCFLAGS="-fno-strict-aliasing -pthread -fPIC -D_FORTIFY_SOURCE=2 -fPIC
> -I/usr/include/ncursesw"
>		PLLDFLAGS="-rdynamic -Wl,-Bsymbolic-functions -Wl,-z,relro -g -O2
> -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security
> -pthread"
>		PLSOEXT="so"
>		PLTHREADS="yes"
>	gcc -c -fpic -fno-strict-aliasing -pthread -fPIC -D_FORTIFY_SOURCE=2 -fPIC
> -I/usr/include/ncursesw -D_REENTRANT -D__SWI_PROLOG__
> -I/usr/lib/swi-prolog/include -o ../testfo.o ../testfo.c
>	gcc -o libtestfo.so -shared -rdynamic -Wl,-Bsymbolic-functions -Wl,-z,relro
> -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat
> -Werror=format-security -pthread ../testfo.o -L/usr/lib/swi-prolog/lib/i386
> -lswipl
>	rm ../testfo.o
>
> .. no errors.  The new library is definitely built.. timestamp updated, and
> file size has increased compared to the version above.
>
> -rwxr-xr-x 1 root root 1237082 Apr  2 15:20 libtestfo.so
>
> Check the symbols are in there:
>
> root@phoenix:/home/jon/Dev/testfo/Debug# nm libtestfo.so | grep testfo
> 0000e4b5 T install_libtestfo
> 0000e4ab t testfo
>
> .. all ok.
>
> Now in prolog, testfo.pl containing a single line:
>
> :- use_foreign_library(foreign('/home/jon/Dev/testfo/Debug/libtestfo.so')).
>
> This loads correctly, so it has found the install predicate etc..

Well, copy-and-pasting the commands from swipl-ld and editing them a
little, it turns out that the fatal option is -Wl,-Bsymbolic-functions.
Once you delete that from the link command, all works just fine. I don't
understand why (see man ld for what the option does). It indeed does
call the install function (verified with an Sdprintf() call in there).
But, it seems it is not really calling PL_register_foreign() inside
Prolog.

If I run Prolog under gdb and put a break on PL_register_foreign, it
nicely breaks, but this seems a different PL_register_foreign(). If I
put a breakpoint in a function called by PL_register_foreign(), it no
longer breaks, so it indeed does not seem to be calling
PL_register_foreign() in Prolog itself.

Before this can be fixed, we must know what is going on.  Anyone?

Maybe our Debian maintainer (Eugeniy) has an idea?

	Cheers --- Jan

> 5 ?-
> pdt_reload:pdt_reload(['/home/jon/Dev/pl/testfo.pl']).
> |    % /home/jon/Dev/pl/testfo.pl compiled 0.00 sec, 5 clauses
> true.
>
> But I get undefined procedure when trying to access it:
>
> 6 ?- testfo.
> ERROR: [Thread pdt_console_client_0_Default Process] toplevel: Undefined
> procedure: testfo/0 (DWIM could not correct goal)
>
> It's always tempting to make 1+1=3, but someone else hit the similar
> symptoms today:
>
> http://stackoverflow.com/questions/22811055/why-am-i-not-able-to-load-this-c-code-in-prolog-in-linux-but-i-am-able-in-os-x
>
> I have successfully done this before lots of times on 6.2.6 compiled from
> source.. but for this install I'm on a completely new machine using
> packages, and wondering what I am doing wrong.. / have forgotten / how the
> process is different.
>
> Thanks,
> Jon.
>
>
>
>
> --
> View this message in context: http://swi-prolog.996271.n3.nabble.com/Undefined-procedure-on-foreign-library-in-6-6-4-tp14676.html
> Sent from the SWI Prolog mailing list archive at Nabble.com.
> _______________________________________________
> SWI-Prolog mailing list
> [email protected]
> https://lists.iai.uni-bonn.de/mailman/listinfo.cgi/swi-prolog
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.