
Dear CLIPS users:

==>
==>  From: Thomas Buck <buck@goya.gris.informatik.uni-tuebingen.de>
==>  Subject: Interfacing CLIPS and C++
==>  To: user alias <'clips-list@cossack.cosmic.uga.edu'>
==>  Date: Wed, 16 Sep 92 9:53:23 MESZ
==>
==>  Dear people:
==>
==>  as I am seeing too much messages about interfacing CLIPS and C++, I want to
==>  tell you that I have a patially compiled version of the CLIPS shell in C++.
==>
==>  Let me explain of what I mean with that. At the beginning I wanted to 
==>  compile all the CLIPS 5.1 stuff in C++, but as I got too much error 
==>  messages, I changed my ideas for a while. So I made a 'libClips.a' library
==>  (including all the files from the distribution, except the 'main.c' file)
==>  with the standard cc compiler and compiled the 'main.C' with the AT&T CC 
==>  compiler. I got a very good result. I just made the right prototypes of the
==>  routines that 'main.C' calls, and everything went ok.

So, let me explain in more details what I did.

1. As I already said, all the CLIPS code stuff I compiled with a standard
   'cc' compiler, except the 'main.c' file. I also built a 'libClips.a'
   library file, that has everything from the stuff.

2. For compiling the 'main.c' file and generating an executable CLIPS program,
   I had to tell the 'CC' (C++) compiler that the code was interfaced with C,
   so I used the discriminator:

           extern "C" {
           }

   which tells that everything inside of the two brackets are in C object code.
   As the 'main' routine in the 'main.c' file calls 3 routines:

           - InitializeCLIPS
           - RerouteStdin
           - CommandLoop

   I had to put those prototypes in the proper form.

3. In the file 'commline.h' (which defines CommandLoop), I did the following:

           #if defined(__cplusplus)
           extern "C" {
           #endif
              LOCALE VOID                           CommandLoop(void);
           #if defined(__cplusplus)
           }
           #endif

   The purpose of the 'defines' above is that the code in 'commline.c' can
   still be compiled with standard C. If you want to compile this file with
   C++, you have to give a parameter '-D__cplusplus' to the C++ compiler, so
   that it sees the 'extern' statement.

4. In the file 'sysdep.h' (which defines InitializeCLIPS and RerouteStdin),
   I did the following:

           #if defined(__cplusplus)
           extern "C" {
           #endif
              LOCALE VOID                           InitializeCLIPS(void);
              LOCALE VOID                           RerouteStdin(int,char *[]);
           #if defined(__cplusplus)
           }
           #endif

5. As the C++ compilers are normally very strong in type checking,
   don't forget to set the option 'ANSI_COMPILER' to '1' in the file
   'setup.h'.

6. The biggest changes are in the file 'main.c'. Instead of telling what I did,
   I will forward my own version of it. The same philosophy of changing can be
   applied here also.


-----< begin of file 'main.C' >-----
   /*******************************************************/
   /*      "C" Language Integrated Production System      */
   /*                                                     */
   /*                  A Product Of The                   */
   /*             Software Technology Branch              */
   /*             NASA - Johnson Space Center             */
   /*                                                     */
   /*             CLIPS Version 5.10  07/17/91            */
   /*                                                     */
   /*                     MAIN MODULE                     */
   /*******************************************************/

/*************************************************************/
/* Purpose:                                                  */
/*                                                           */
/* Principal Programmer(s):                                  */
/*      Gary D. Riley                                        */
/*                                                           */
/* Contributing Programmer(s):                               */
/*                                                           */
/* Revision History:                                         */
/*                                                           */
/*************************************************************/

#include <stdio.h>
#include "setup.h"
#include "sysdep.h"
#include "commline.h"

#if defined(__cplusplus)

/*************************************************************/
/* Purpose: Convert the compilation to C++                   */
/*                                                           */
/* Principal Programmer(s):                                  */
/*      Thomas de Araujo Buck                                */
/*                                                           */
/* Contributing Programmer(s):                               */
/*                                                           */
/* Revision History:                                         */
/*      Patch 1: First try on Wed Sep  9 14:20:26 MESZ 1992  */
/*                                                           */
/*************************************************************/

#endif


#if defined(__cplusplus)
extern "C" {
#endif


#if ANSI_COMPILER
int main(int,char *[]);
VOID UserFunctions(void);
#else
int main();
VOID UserFunctions();
#endif


#if defined(__cplusplus)
}
#endif

/***************************************************************/
/* MAIN: Start execution of CLIPS.  This function must be      */
/*   redefined in order to embed CLIPS within another program. */
/*   Example of redefined main:                                */
/*     main()                                                  */
/*       {                                                     */
/*        init_clips();                                        */
/*            .                                                */
/*            .                                                */
/*            .                                                */
/*        process_data();                                      */
/*        RunCLIPS(-1);                                        */
/*        evaluate_data();                                     */
/*            .                                                */
/*            .                                                */
/*            .                                                */
/*        final_results();                                     */
/*       }                                                     */
/***************************************************************/

#if defined(__cplusplus)
int main (int argc, char *argv[])
#else
int main(argc,argv)
  int argc;
  char *argv[] ;
#endif /* defined(__cplusplus) */
{
   InitializeCLIPS();
   RerouteStdin(argc,argv);
   CommandLoop();
   return(-1);
}

/*************************************************************/
/* UserFunctions:  The function which informs CLIPS of any   */
/*   user defined functions.  In the default case, there are */
/*   no user defined functions.  To define functions, either */
/*   this function must be replaced by a function with the   */
/*   same name within this file, or this function can be     */
/*   deleted from this file and included in another file.    */
/*   User defined functions may be included in this file or  */
/*   other files.                                            */
/*   Example of redefined UserFunctions:                     */
/*     UserFunctions()                                       */
/*       {                                                   */
/*        DefineFunction("fun1",'i',fun1,"fun1");            */
/*        DefineFunction("other",'f',other,"other");         */
/*       }                                                   */
/*************************************************************/
VOID UserFunctions()
{
#ifdef _MODIFIED_BUCK
   return;
#endif /* _MODIFIED_BUCK */
}

-----< end of file 'main.C' >-----

So I hope with these changes the system will be o.k. after the compilation.
Please fell free to ask me for more information, or giving me better solutions
for this interface :-).

Cheers,
Thomas

--
THOMAS DE ARAUJO BUCK   [Internet] buck@goya.gris.informatik.uni-tuebingen.de
Universitaet Tuebingen             Wilhelm Schickard Institut fuer Informatik
Auf der Morgenstelle 10, C-9                 7400 Tuebingen 1  -  DEUTSCHLAND
Phone: +49 (0) 7071 / 29-5464                     Fax: +49 (0) 7071 / 29-5466
