From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: David Fang Newsgroups: gmane.lisp.guile.user Subject: Re: Guile and C++ Date: Tue, 18 May 2010 17:22:40 -0400 (EDT) Message-ID: <20100518171030.Q67999@shannon.csl.cornell.edu> References: <04136158-092D-4563-B654-DA853FD783BF@telia.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed X-Trace: dough.gmane.org 1274217797 19312 80.91.229.12 (18 May 2010 21:23:17 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 18 May 2010 21:23:17 +0000 (UTC) Cc: guile-user@gnu.org, Noah Lavine To: Hans Aberg Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue May 18 23:23:15 2010 connect(): No such file or directory Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1OEUFu-0000kn-Fm for guile-user@m.gmane.org; Tue, 18 May 2010 23:23:14 +0200 Original-Received: from localhost ([127.0.0.1]:50518 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OEUFt-0003Ps-KF for guile-user@m.gmane.org; Tue, 18 May 2010 17:23:13 -0400 Original-Received: from [140.186.70.92] (port=46469 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OEUFl-0003OM-Aw for guile-user@gnu.org; Tue, 18 May 2010 17:23:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OEUFi-0003Y5-8D for guile-user@gnu.org; Tue, 18 May 2010 17:23:05 -0400 Original-Received: from csl.cornell.edu ([128.84.224.10]:4045 helo=vlsi.csl.cornell.edu) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OEUFi-0003WV-4G for guile-user@gnu.org; Tue, 18 May 2010 17:23:02 -0400 Original-Received: from shannon.csl.cornell.edu (shannon.csl.cornell.edu [128.84.224.88]) by vlsi.csl.cornell.edu (8.13.4/8.13.4) with ESMTP id o4ILMi8n058509; Tue, 18 May 2010 17:22:49 -0400 (EDT) Original-Received: from shannon.csl.cornell.edu (localhost.csl.cornell.edu [127.0.0.1]) by shannon.csl.cornell.edu (8.13.8/8.13.8) with ESMTP id o4ILMiYW068887; Tue, 18 May 2010 17:22:44 -0400 (EDT) (envelope-from fang@shannon.csl.cornell.edu) Original-Received: from localhost (fang@localhost) by shannon.csl.cornell.edu (8.13.8/8.13.8/Submit) with ESMTP id o4ILMit8068884; Tue, 18 May 2010 17:22:44 -0400 (EDT) (envelope-from fang@shannon.csl.cornell.edu) In-Reply-To: <04136158-092D-4563-B654-DA853FD783BF@telia.com> X-detected-operating-system: by eggs.gnu.org: FreeBSD 2.0-4.2 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:7792 Archived-At: >> How would this work if I wanted to not run Guile embedded in a C++ >> program, but instead load the C++ code at runtime from a regular Guile >> interpreter? (This could use either the libffi binding or the regular >> Guile ones.) > > Since Guile is written in C, formally according to the C++ standard, you can > only load Guile from C++ code, and not the opposite. So main() must be C++. > > GCC though has integrated C and C++ fully, it seems. So you can call C++ from > C code, but the latter do not implement C++ exception stacks, and may not > call C++ initialization function properly (I haven't checked). It means that > throwing and catching exceptions within C++ functions called from C seems to > work fine, but if one tries to pass it through a C function, the program will > throw a terminate exception. Hi, FWIW, I've used guile with C++ both ways: calling the main REPL loop in libguile from a C++ main, and also by creating loadable modules in C++, and calling load-extension with an initialization function. Both work just fine, but you should be aware that guile errors do not unwind the stack on error, so C++ stack objects do not get destroyed in the event of exceptions/errors in the guile library. I usually write the initialization function in the extern "C" namespace to avoid symbol name mangilng. Also, global statics in C++ modules are initialized upon loading, from what I've seen. Fang David Fang http://www.csl.cornell.edu/~fang/ http://www.achronix.com/