From lains@caramail.com  Tue Aug  1 18:54:34 2000
Received: from mail.caramail.com (mail.caramail.com [195.68.99.70])
	by swi.psy.uva.nl (8.9.3/8.9.3) with ESMTP id SAA02318
	for <prolog@swi.psy.uva.nl>; Tue, 1 Aug 2000 18:54:19 +0200 (MET DST)
Received: from caramail.com (www29.caramail.com [195.68.99.49])
	by mail.caramail.com (8.8.8/8.8.8) with SMTP id TAA28726;
	Tue, 1 Aug 2000 19:00:42 +0100 (WET DST)
Date: Tue, 1 Aug 2000 19:00:42 +0100 (WET DST)
Posted-Date: Tue, 1 Aug 2000 19:00:42 +0100 (WET DST)
From: Lionel Ains <lains@caramail.com>
To: "Holmes-Higgin, Paul" <paul.holmes.higgin@documentum.com>,
        prolog@swi.psy.uva.nl
Message-ID: <965156104009615@caramail.com>
X-Mailer: Caramail - www.caramail.com
Mime-Version: 1.0
Subject: Re: Linking SWI-Prolog with Link Grammar Parser v3.0
Content-Type: multipart/mixed; boundary="=_NextPart_Caramail_009615965156104_ID"

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

--=_NextPart_Caramail_009615965156104_ID
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

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 =3D malloc(strlen(s)+1);

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

 rval=3DPL_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" <paul.holmes.higgin@documentum.com>
> 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: paulhh@documentum.com
> 
> Tel: +44 (0)208 867 3179
> 
> -----Original Message-----
> From: Lionel Ains [mailto:lains@caramail.com]
> Sent: Monday, July 31, 2000 11:48 AM
> To: prolog@swi.psy.uva.nl
> 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=EEte aux lettres - Caramail - http://www.caramail.com


--=_NextPart_Caramail_009615965156104_ID--

