Subject: Re: Linking SWI-Prolog with Link Grammar Parser v3.0

From: Lionel Ains <[email-masked]>
Date: Tue, 1 Aug 2000 19:00:42 +0100 (WET DST)

Hello

I have decided DLLs to link the foreign code to the Prolog engine.
I'm using cygwin for Windows NT, and compiling my DLL with gcc under cygwin.
Using the two lines given inside the manual, the prolog Engine crashes when executing 
load_foreign_library/1. I have thus used another description I found about bulding a DLL with cygwin.
Here are the commands I use (assuming that t.c is the foreign language source code):
gcc -I/cygdrive/c/Program\ Files/SWI-Prolog/include -c t.c
echo -e "EXPORTS\npl_lowercase\ninstall" > libt.def
gcc -s -Wl,--base-file,libt.base -o libt.dll t.o /cygdrive/c/Program\ Files/SWI-Prolog/bin/Libpl.dll 
-Wl,-e,_install
dlltool --base-file libt.base --def libt.def --output-exp libt.exp --dllname libt.dll

The source file consists of the exmaple lowercase/2 predicate (chap 5.9), slightly modified to adapt to 
cygwin:

#include <SWI-Prolog.h>
#include <stdlib.h>
#include <ctype.h>
#include <windows.h>

foreign_t pl_lowercase(term_t u, term_t l) {
char *copy;
char *s, *q;

int rval;

 if (!PL_get_atom_chars(u, &s))
 return PL_warning("lowercase/2: instantiation fault");
 copy = malloc(strlen(s)+1);

 for (q=copy; *s; q++, s++)
 *q= (isupper(*s) ? tolower(*s) : *s);
 *q = '\0';

 rval=PL_unify_atom_chars(l, copy);
 free(copy);

 return rval;
}

int main() {
 printf("Running main\n");
 return 0;
}

install_t install() {
 printf("Starting install function of library\n");
 PL_register_foreign("lowercase", 2, pl_lowercase, 0);
 printf("Exiting install function of library\n");
}

This gives me a library libt.dll
When I try load_foreign_library(libt). under SWI-Prolog, I get immediately a Yes. without any output of 
the debug messages included in install, which means that lowercase is not registered, and the failure 
when calling lowercase/2 confirms it.

I've first tried the default compilation commands (figure 5.7 chap 5.9), it generates the dll but when I 
call load_foreign_library(libt), I have the two debugging messages "Starting install function of library" 
and "Exiting install function of library" and Prolog hangs.

Does someone have a preceding experience concerning cygwin, or would there be another free C 
compiler that could do the job?

Thanks a lot,

Lionel

> -------Message d'origine-------
> De : "Holmes-Higgin, Paul" <[email-masked]>
> Date : 31/07/100 12:07:01
> RE: Linking SWI-Prolog with Link Grammar Parser v3.0
> 
> Lionel
> 
> The best way of linking foreign code to SWI-Prolog has to be using shared libraries (DLLs under 
Win), then you don't need to rebuild or even relink Prolog each time you change the foreign code.
> From what you are saying, I guess that the Link Grammar Parser API is not available as a DLL under 
NT, so the best thing would be to make it a DLL, but add in the SWI-specific foreign function code to 
actually call the Link Parser C API. MS Visual C/C++ works fine for this on NT. If it is available as a 
DLL and you don't want to keep editing their source to add in the SWI stubs, then consider building a 
stub DLL that calls their DLL.
> 
> There are some examples in the SWI-Prolog manual (Sec 5.4 & 5.6), although there aren't explicit 
instructions for how to compile or link the code under specific platforms.
> 
> Of course, they may be others out there that have better ideas!
> 
> Cheers
> 
> Paul.
> 
> ---
> 
> Paul Holmes-Higgin
> 
> Documentum UK
> 
> Email: [email-masked]
> 
> Tel: +44 (0)208 867 3179
> 
> -----Original Message-----
> From: Lionel Ains [mailto:[email-masked]]
> Sent: Monday, July 31, 2000 11:48 AM
> To: [email-masked]
> Subject: Linking SWI-Prolog with Link Grammar Parser v3.0
> 
> Hello,
> 
> I am trying to interface SWI-Prolog with the API of Link Grammar Parser 3.0, a freeware parsing 
> grammar in english sentences.
> The API is programmed in C and I should then just have to use it in a foreign code linked with 
> the Prolog engine.
> I have a few question concerning this operation:
> - Do I have to recompile the whole Prolog engine each time I make a modification inside the 
foreign 
> language code?
> - Which C compiler should I use to compile Prolog and the API, undex Windows NT?
> - Given that I would just like to register new foreign predicates, which part of the existing SWI-Prolog 
> source code do I have to modify to add the new predicates (which will use the Link Grammar Parser 
> API) to the list of built-in predicates?
> 
> Many thanks for this help,
> 
> Lionel
______________________________________________________
Boîte aux lettres - Caramail - http://www.caramail.com