Home > Archive > Prolog > January 2006 > Could not load foreign predicates in SWI Prolog 5.6.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Could not load foreign predicates in SWI Prolog 5.6.
|
|
|
| Hi all,
I have installed SWI Prolog 5.6 and I am trying to implement a
foregin predicate (in C language) and use it in SWI Prolog.
I am facing the following issue with loading foreign predicates:
I have implemented a predicate "say_hello" in the library
"myshared.so".
-- The implementation works fine when I start the pl interpreter and
then type
-- ?- load_foreign_object(myshared).
-- Yes
-- ?- say_hello(X,Y).
-- Predicate implemented in C is called.
-- ?-
However, if I put the above lines in a .pl file and load the .pl file
into the prolog interpreter, then load_foreign_library() call doesnt
seem to work.
I am not getting a "file not found" error. But if I try to use the
foreign predicate (viz: say_hello), I get a "function undefined" error.
Could some one help me in resolving this issue.
thanks in advance
Sujai.
| |
| Jan Wielemaker 2006-01-10, 4:10 am |
| On 2006-01-06, Sujai <sujai_antony2001@yahoo.com> wrote:
> Hi all,
> I have installed SWI Prolog 5.6 and I am trying to implement a
> foregin predicate (in C language) and use it in SWI Prolog.
>
> I am facing the following issue with loading foreign predicates:
>
> I have implemented a predicate "say_hello" in the library
> "myshared.so".
> -- The implementation works fine when I start the pl interpreter and
> then type
> -- ?- load_foreign_object(myshared).
> -- Yes
> -- ?- say_hello(X,Y).
> -- Predicate implemented in C is called.
> -- ?-
>
> However, if I put the above lines in a .pl file and load the .pl file
> into the prolog interpreter, then load_foreign_library() call doesnt
> seem to work.
> I am not getting a "file not found" error. But if I try to use the
> foreign predicate (viz: say_hello), I get a "function undefined" error.
>
> Could some one help me in resolving this issue.
Problems and solutions are generally in the subtle details. Why not add
the contents of the .pl file to your post? Considering you write "viz:",
there may be a module issue.
--- Jan
| |
|
| Hello Jan,
Thanks for your reply. Here is my .pl file that i am trying to load
into Prolog interpreter.
%%>---------------------begin
% SWI prolog's built in predicate
load_foreign_library(myshared).
% call "say_hello" that is implemented in myshared.so
read_C(X):-
say_hello(200,X),
write(X).
%%>--------------------- end
I compiled my C module using the following gcc command.
gcc -shared -fpic myshared.c -o myshared.so
Let me know if you need any further details.
ps: I didnt understand your comment about viz:. I just used "viz" for
its literal meaning here. That is "namely".
thanks again
Sujai.
| |
| Jan Wielemaker 2006-01-10, 4:10 am |
| On 2006-01-06, Sujai <sujai_antony2001@yahoo.com> wrote:
> Hello Jan,
> Thanks for your reply. Here is my .pl file that i am trying to load
> into Prolog interpreter.
>
> %%>---------------------begin
> % SWI prolog's built in predicate
> load_foreign_library(myshared).
Ha! You *define* load_foreign_library/1 instead of calling it. Use
the following (note the ":-")
:- load_foreign_library(myshared).
Or, even better as you want this code to be loaded if you restart a
saved program state:
:- initialization
load_foreign_library(myshared).
> % call "say_hello" that is implemented in myshared.so
> read_C(X):-
> say_hello(200,X),
> write(X).
> %%>--------------------- end
>
> I compiled my C module using the following gcc command.
> gcc -shared -fpic myshared.c -o myshared.so
better use
plld -shared -o myshared myshared.c
It sets up paths and deals with many of the platform issues to make this
work on all wel supported platforms.
> Let me know if you need any further details.
>
> ps: I didnt understand your comment about viz:. I just used "viz" for
> its literal meaning here. That is "namely".
I thought so, but viz:say_hello(200,X) calls say_hello/2 in the module
viz. I wasn't sure what you meant.
Cheers --- Jan
| |
|
| Hi Jan,
Thanks lot. That changed worked. I am able to load foreign
predicates from a .pl file.
~Sujai.
| |
| Jan Wielemaker 2006-01-10, 4:10 am |
| On 2006-01-07, Sujai <sujai_antony2001@yahoo.com> wrote:
> Hi Jan,
> Thanks lot. That changed worked. I am able to load foreign
> predicates from a .pl file.
Great. If you have this type of trouble, look through the libraries.
Especially for foreign language issues, get the source and study the
package sources. Some are pretty complicated (for example ODBC and
rdf_db), while others (notably in the `clib' package) are very
straightforward. If you need to write portable code, the packages
contain the autoconf and makefile infrastructure. Copy one, delete
the content and fill it with your project.
Cheers --- Jan
|
|
|
|
|