2
votes

I am trying to integrate Prolog and C++. I am using SWI-Prolog for Prolog and Embarcadero Rad Studio for C++. I want to take input in C++, process the input in Prolog and display the output in C++.

First it was giving "Unable to perform link" error, but after linking library of SWI-Prolog it gave the error:

[ILINK32 Error] Error: 'C:\PROGRAM FILES
(X86)\SWIPL\LIB\LIBSWIPL.LIB' contains invalid OMF record, type 0x21
(possibly COFF)

I searched and find to resolve it using implib.exe and created a new library newlibswipl.lib.

After linking it in program it gives error as follows:

[ILINK32 Error] Error: Unresolved external '_PL_cut_query' referenced
from C:\USERS\DOCUMENTS\RAD STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ
[ILINK32 Error] Error: Unresolved external '_PL_new_term_ref'
referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unresolved
external '_PL_exception' referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unresolved
external '_PL_fatal_error' referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unresolved
external '_PL_chars_to_term' referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unresolved
external '_PL_put_term' referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unresolved
external '_PL_predicate' referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unresolved
external '_PL_open_query' referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unresolved
external '_PL_next_solution' referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unresolved
external '_PL_get_arg' referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unresolved
external '_PL_get_name_arity' referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unresolved
external '_PL_atom_chars' referenced from C:\USERS\DOCUMENTS\RAD
STUDIO\PROJECTS\DEBUG\NEWPROLOG.OBJ [ILINK32 Error] Error: Unable to
perform link

C++ code:

// newprolog.cpp

#include <math.h>
#include <iostream>
#include <sstream> 
#include "SWI-cpp.h"
#include "SWI-Prolog.h"
#include "SWI-Stream.h"

#pragma comment(lib,"C:\Program Files (x86)\swipl\lib\newlibswipl.lib");
using namespace std;

term_t a; 
term_t b;
term_t ans;
functor_t fun;

int main()
{
  int digit;
  cout << "\nPlease enter a digit to calculate it's Factorial.." << endl;
  cin >> digit;
  PlCall("consult(swi('C:\Program Files(x86)\swipl\swipl-win.rc'))"); 
  PlCall("consult('factorial.pl')");
  a = PL_new_term_ref();
  PL_put_integer(a, digit);
  b   = PL_new_term_ref();
  ans = PL_new_term_ref();
  fun = PL_new_functor(PL_new_atom("factorial"),2);
  PL_cons_functor(ans, fun, a, b);
  int fact;
  if(PL_call(ans, NULL)) {
    PL_get_integer(b, &fact);
    //int numb = fact;
  }
  return 0;
}

And factorial.pl:

factorial(1, 1) :- !.
factorial(X, Fac) :-
  X > 1,
  Y is X - 1,
  factorial(Y, New_Fac),
  Fac is X * New_Fac.

Actually I have referred it from a youtube video in which the person has used QT creator instead of Rad Studio. The link is Swi-Prolog with QT Creator C++ interface But I want to do it using Embarcadero Rad Studio. So could anyone help me out Please..

1
I'm guessing that you try to use LIBSWIPL.LIB that was compiled with a different compiler. This won't work, the compilers use different object formats. You must compile all the source of SWI-Prolog with C++Builder. - M.M
Sorry Friend I don't know how to compile all the source of SWI-Prolog with C++Builder. So can you help me how to do it ?? - Rohit G
Not really. I suggest reading through SWI Prolog's documentation (especially build instructions) and see if it mentions a Borland C++ mode. If not then you have some work ahead of you. - M.M

1 Answers

2
votes

Thank you all for your suggestions. I got the solution to link the library of SWI-Prolog with Embarcadero Rad Studio.

Solution:

  1. I used the implib.exe in command prompt to convert the SWI-Prolog library libswipl.dll to get the COFF format library as the library was in the OMF format and the required format was COFF.

  2. After getting the COFF version of the library of SWI-prolog, I copied it to the Embarcadero's Project Folder and included it in the Project.

This inclusion resolved the linking error and executed the Project.

Thank you to all for your suggestions..