From owner-freebsd-questions@FreeBSD.ORG Thu Jan 31 14:11:18 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9662E16A419 for ; Thu, 31 Jan 2008 14:11:18 +0000 (UTC) (envelope-from wundram@beenic.net) Received: from mail.beenic.net (mail.beenic.net [83.246.72.40]) by mx1.freebsd.org (Postfix) with ESMTP id 4E8AC13C4E1 for ; Thu, 31 Jan 2008 14:11:18 +0000 (UTC) (envelope-from wundram@beenic.net) Received: from [192.168.1.38] (a89-182-148-8.net-htp.de [89.182.148.8]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.beenic.net (Postfix) with ESMTP id 2A5ECA44529 for ; Thu, 31 Jan 2008 15:10:57 +0100 (CET) From: "Heiko Wundram (Beenic)" Organization: Beenic Networks GmbH To: freebsd-questions@freebsd.org Date: Thu, 31 Jan 2008 15:12:32 +0100 User-Agent: KMail/1.9.7 References: <80f4f2b20801310548g33ee5f48ne90c2e86cc33346d@mail.gmail.com> In-Reply-To: <80f4f2b20801310548g33ee5f48ne90c2e86cc33346d@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200801311512.32396.wundram@beenic.net> Subject: Re: C interpreters X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 31 Jan 2008 14:11:18 -0000 Am Donnerstag, 31. Januar 2008 14:48:15 schrieb Jim Stapleton: > as a secondary (probably stupid) question: how hard is it to write a > library in C++ and allow C programs to use it? To write a library in C++ to which C programs have access, you'll have to write a set of wrapper functions for every method of a class you want to expose to C which basically get an object pointer as the first parameter and the actual method arguments as the rest. For example: test.cc ------- #include "test.hh" #include "test.h" Test::Test() { } int Test::something(int data) { return 0; } extern "C" { TestObject NewTest() { return new Test(); } int TestSomething(TestObject ob, int data) { return reinterpret_cast(ob)->something(data); } } test.hh ------- #ifndef TEST_HH #define TEST_HH class Test { Test(); int something(int data); }; #endif // TEST_HH test.h ------ #ifndef TEST_H #define TEST_H typedef void* TestObject; #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ TestObject NewTest(); int TestSomething(TestObject ob, int data); #ifdef __cplusplus } #endif /* __cplusplus */ #endif /* TEST_H */ test.c ------ #include "test.h" int main(int argc, char** argv) { TestObject testob; testob = NewTest(); TestSomething(testob,1); } This lets you use the compiled test.cc (for example, as a library, to get around the problem of having to link your C-program against libstdc++) together with a C program. Be aware of the fact that C doesn't know function overloading, so you'll basically have to implement that by defining different methods for every type of overloaded function you want to accept. Depending on how large the C++ framework is which you're trying to wrap (and in how much it uses "advanced" C++ features), this is an easy (i.e., repetitive) or a hard/close to impossible task, especially when it comes to templates. YMMV. -- Heiko Wundram Product & Application Development