From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Mike Solomon Newsgroups: gmane.lisp.guile.user Subject: Re: Guile, C++, and Mac OS X 10.4 (powerpc) Date: Thu, 29 Jul 2010 10:04:09 +0200 Message-ID: References: <501CC8FC-0348-4E6D-87D7-67AD82CD45B1@telia.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1280390581 26510 80.91.229.12 (29 Jul 2010 08:03:01 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 29 Jul 2010 08:03:01 +0000 (UTC) Cc: guile-user@gnu.org To: Hans Aberg Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Thu Jul 29 10:03:00 2010 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 1OeO4w-0004bj-Vd for guile-user@m.gmane.org; Thu, 29 Jul 2010 10:02:59 +0200 Original-Received: from localhost ([127.0.0.1]:44925 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OeO0l-0002Ye-47 for guile-user@m.gmane.org; Thu, 29 Jul 2010 03:58:39 -0400 Original-Received: from [140.186.70.92] (port=53847 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OeO0c-0002Y9-UQ for guile-user@gnu.org; Thu, 29 Jul 2010 03:58:32 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OeO0a-0005u7-0a for guile-user@gnu.org; Thu, 29 Jul 2010 03:58:29 -0400 Original-Received: from smtp02.osg.ufl.edu ([128.227.74.165]:40355 helo=smtp.ufl.edu) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OeO0Z-0005to-SZ for guile-user@gnu.org; Thu, 29 Jul 2010 03:58:27 -0400 Original-Received: from [10.101.0.58] (nsc.ciup.fr [193.52.24.125]) (authenticated bits=0) by smtp.ufl.edu (8.14.0/8.14.0/3.0.0) with ESMTP id o6T7wLWc001090; Thu, 29 Jul 2010 03:58:25 -0400 User-Agent: Microsoft-Entourage/11.4.0.080122 Thread-Topic: Guile, C++, and Mac OS X 10.4 (powerpc) Thread-Index: Acsu9J9R3dtxFJrnEd+ETgARJHM+bg== In-Reply-To: <501CC8FC-0348-4E6D-87D7-67AD82CD45B1@telia.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=1.12.8161:2.4.5, 1.2.40, 4.0.166 definitions=2010-07-29_02:2010-02-06, 2010-07-29, 2010-07-28 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 ipscore=0 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx engine=5.0.0-1005130000 definitions=main-1007290009 X-UFL-Spam-Level: * X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) 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:8028 Archived-At: Dear Hans, Thank you very much for your response. I'm including here some stuff I did (a sort of c++ to c and back again) in hopes that it may be useful to others trying to do the same thing. Asterisk delimiters mean a new file, whose name is in the first comment. *** //bessel-extension.h #ifndef BESSEL_EXTENSION_H #define BESSEL_EXTENSION_H #ifdef __cplusplus extern "C" { #endif int silly (int x); #ifdef __cplusplus } // extern "C" #endif #endif BESSEL_EXTENSION_H *** //bessel-extension.cc #include "bessel-extension.h" #include "vector" int silly (int x) { // include random c++ functionality std::vector *foo = new std::vector; foo->push_back (1); foo->push_back (2); int rv = foo->at (0) +foo->at (1)+x; delete foo; return rv; } *** // bessel.h #ifndef BESSEL_H #define BESSEL_H #ifdef __cplusplus extern "C" { #endif void init_bessel(); #ifdef __cplusplus } // extern "C" #endif #endif BESSEL_H *** // bessel.c #include #include #include #include SCM j0_wrapper(SCM x) { return scm_make_real(j0 (scm_num2dbl(x,"j0"))); } SCM silly_wrapper(SCM x) { return scm_from_int(silly (scm_to_int (x))); } void init_bessel() { scm_c_define_gsubr("j0",1,0,0,j0_wrapper); scm_c_define_gsubr("silly",1,0,0,silly_wrapper); } *** // bessel.cc #include "bessel-extension.h" #include "bessel.h" *** Then, compile: g++ -c bessel-extension.cc gcc -c -I. -fPIC bessel.c g++ -bundle bessel-extension.o bessel.o -o libguile-bessel.so -lguile bessel.cc (this last line will change based on your platform...ie bundle becomes shared for linux...use libtool if you're really serious about making something durable!) Then: guile> (load-extension "./libguile-bessel" "init_bessel") guile> (j0 2) 0.223890779141236 guile> (silly 6) 9 Hope this helps the fledgling C++ programmer using guile! ~Mike On 7/28/10 11:19 PM, "Hans Aberg" wrote: > On 28 Jul 2010, at 21:49, Mike Solomon wrote: > >> Hey guile users, >> Trying to compile the simple example bessel.c from >> Writing-Guile-Extensions.html (renamed bessel.cc because I'm using g+ >> +), I >> encountered the following error: >> >> bessel.cc: In function 'void init_bessel()': >> bessel.cc:13: error: invalid conversion from 'scm_unused_struct* >> (*)(scm_unused_struct*)' to 'scm_unused_struct* (*)(...)' >> bessel.cc:13: error: initializing argument 5 of 'scm_unused_struct* >> scm_c_define_gsubr(const char*, int, int, int, scm_unused_struct* (*) >> (...))' > > The SCM type is a pointer to an undefined C type - C hack, which > clashes with C++. Clever in C, but bad for C++ users. > >> This even comes up when I put everything in extern "C" { ... }. I >> have seen >> other postings on the net for other software (lilypond, swig) that >> speaks of >> the same issue, and some suggest that it is a problem with g++ and not >> guile. I'm using powerpc-apple-darwin8-g++-4.0.1 (GCC) 4.0.1 (Apple >> Computer, Inc. build 5370) > > When you use g++, all stuff is compiled as C++. So use gcc and add c++ > libraries when linking. > > Then in formally correct C++, main() must be C++. Gcc accepts calling C > ++ from C, but attempting to pass an exception through a C function is > converted to a termination exception. > > So I wrote a C++ function scm::init_guile() calling scm_init_guile() > and other stuff that needs to be initialized. Then > int main(int argc, char** argv) { > init_guile(); > > try { > ... > } > ... > } > > Then write a header bessel.h > > #ifndef BESSEL_H > #define BESSEL_H > > /* Copyright ... > Free Software Foundation GNU General Public License > > > */ > > #ifdef __cplusplus > extern "C" { > #endif > > void init_bessel(); > ... > > #ifdef __cplusplus > } // extern "C" > #endif > > #endif BESSEL_H > > > The file bessel.c is compiled as C, but your C++ files include bessel.h. > > Hans > > > >