Re: [eclipse-clp-users] C/C++ communication with ECLiPSe

From: Joachim Schimpf (Independent Contractor) <"Joachim>
Date: Wed, 11 Jun 2008 17:36:06 +0100
Soheil Samii wrote:
> Hi!
> 
> I have a simple task scheduling problem for which I use ECLiPSe to find a 
> solution. Basically, I generate the CLP formulation from a C++ program to an 
> ECLiPSe file called scheduling.ecl (attached). 
> 
> I have tried to run it directly in ECLiPSe and it works fine:
> [scheduling].
> schedule(StartTimes).
> 
> The result (StartTimes) is a list of starting times for the tasks.
> 
> Then, I compile and run it from within a C++ program using the interfacing 
> provided by the ECLiPSe C/C++ libraries. I have tried both the C approach and 
> the C++ approach to get the result of the scheduling back to my C++ program 
> and I have encountered problems in both cases, which I now describe:
> 
> * The C approach:
> 
> #include "eclipse.h"
> 
> // after the generation of scheduling.ecl
> ec_init():
> pword startTimes = ec_newvar();
> int success;
> pword *head;
> pword *tail;
> 
> ec_post_goal(ec_term(ec_did("compile",1),ec_string("scheduling")));
> ec_post_goal(ec_term(ec_did("schedule",1),startTimes));
> ec_resume();
> success = ec_get_list(startTimes,head,tail);
> 
> The problem here is that I get a segmentation fault, but clearly "startTimes" 
> is a list of integer values. Does anybody know why I get a segfault?

If you have constructed a data structure _before_ calling ec_resume(),
and want to access it again _afterwards_, you have to keep a reference
to it as an ec_ref:

...
ec_ref startTimesRef = ec_ref_create(startTimes);
...
ec_resume();
success = ec_get_list(ec_ref_get(startTimesRef),head,tail);


> 
> 
> * The C++ approach:
> 
> #include "eclipseclass.h"
> 
> // after the generation of scheduling.ecl
> ec_init();
> EC_word startTimes = newvar();
> post_goal(term(EC_functor("compile",1),"scheduling"));
> post_goal(term(EC_functor("schedule",1), startTimes));
> ec_resume();

For C++, the only change should be declaring startTimes as EC_Ref,
and the automatic type coercions should take care of the rest:

...
EC_Ref startTimes;
...


Sorry, code untested.

-- Joachim
Received on Wed Jun 11 2008 - 09:36:30 CEST

This archive was generated by hypermail 2.3.0 : Wed Sep 25 2024 - 15:13:20 CEST