* Ported the C++ examplesto the shared object dynamic linking scheme.

git-svn-id: svn://svn.zoy.org/elk/trunk@14 55e467fa-43c5-0310-a8a2-de718669efc6
This commit is contained in:
sam 2003-08-20 22:18:34 +00:00
parent 074467d7f1
commit 29a718f477
2 changed files with 18 additions and 27 deletions

View File

@ -6,13 +6,13 @@ Scheme primitives.
See constructor.c in this directory for compilation instructions. See constructor.c in this directory for compilation instructions.
Here is a transcript showing a test run under Solaris 2.4 using the Here is a transcript showing a test run under Linux using the
GNU g++ compiler: GNU g++ compiler:
% g++ -fpic -I/usr/elk/include -c class.c % g++ -shared -fPIC -I/usr/elk/include -c class.c -o class.so
% %
% scheme % scheme
> (load 'class.o) > (load 'class.so)
> (define x (make-foo)) > (define x (make-foo))
x x
@ -23,7 +23,7 @@ GNU g++ compiler:
> (read-val x) > (read-val x)
11 11
> (exit) > (exit)
% %
-----------------------------------------------------------------------------*/ -----------------------------------------------------------------------------*/
@ -72,18 +72,20 @@ Object P_Write_Val(Object x, Object y) {
return Void; return Void;
} }
Foo_Print(Object h, Object port, int raw, int depth, int length) { int Foo_Print(Object h, Object port, int raw, int depth, int length) {
Printf(port, "#[foo %d]", FOO(h)->foo.read_val()); Printf(port, "#[foo %d]", FOO(h)->foo.read_val());
return 0;
} }
int Foo_Equal(Object x, Object y) { int Foo_Equal(Object x, Object y) {
return FOO(x)->foo.read_val() == FOO(y)->foo.read_val(); return FOO(x)->foo.read_val() == FOO(y)->foo.read_val();
} }
void elk_init_foo() { extern "C" void elk_init_foo() {
T_Foo = Define_Type(0, "foo", NOFUNC, sizeof(struct S_Foo), T_Foo = Define_Type(0, "foo", NOFUNC, sizeof(struct S_Foo),
Foo_Equal, Foo_Equal, Foo_Print, NOFUNC); Foo_Equal, Foo_Equal, Foo_Print, NOFUNC);
Define_Primitive((Object(*)(...))P_Make_Foo, "make-foo", 0, 0, EVAL); Define_Primitive((Object(*)(...))P_Make_Foo, "make-foo", 0, 0, EVAL);
Define_Primitive((Object(*)(...))P_Read_Val, "read-val", 1, 1, EVAL); Define_Primitive((Object(*)(...))P_Read_Val, "read-val", 1, 1, EVAL);
Define_Primitive((Object(*)(...))P_Write_Val, "write-val!", 2, 2, EVAL); Define_Primitive((Object(*)(...))P_Write_Val, "write-val!", 2, 2, EVAL);
} }

View File

@ -3,24 +3,13 @@
This simple C++ program demonstrates that static constructors (destructors) This simple C++ program demonstrates that static constructors (destructors)
are invoked by Elk when loading a compiled C++ file (when exiting). are invoked by Elk when loading a compiled C++ file (when exiting).
o Compile the program with CC -c -I/usr/elk/include constructor.c, where o Compile the shared object, for instance:
/usr/elk is the toplevel directory of your Elk installation. Under
Solaris 2.x (and SysVR4) you also have to specify -pic (-fpic for g++).
o Invoke Elk and set the load-libraries to point to the C++ and the CC -pic -shared -I/usr/elk/include constructor.c -o constructor.so
C library, e.g. type something like:
(set! load-libraries "-L/usr/lang/SC2.0.1 -lC -lc") g++ -fPIC -shared -I/usr/include/elk constructor.c -o constructor.so
or
(set! load-libraries "-R/usr/lang/lib -L/usr/lang/lib -lC -lc")
or just
(set! load-libraries "-lC -lc")
depending on the platform and the place where the C++ library has
been installed on your system. If you are using g++, you may have
to mention both the g++ library and the gcc library.
o Now "(load 'constructor.o)", observe the "invoking constructor" message, o Now "(load 'constructor.so)", observe the "invoking constructor" message,
and evaluate "(test)", which should return 3. Terminate the interpreter and evaluate "(test)", which should return 3. Terminate the interpreter
and observe the "invoking destructor" message. and observe the "invoking destructor" message.
@ -47,16 +36,16 @@ o If static constructors don't get called when loading compiled C++ files,
#include "scheme.h" #include "scheme.h"
#include <iostream.h> #include <iostream>
class C { class C {
public: public:
int i; int i;
C() { C() {
cerr << "[invoking constructor]" << endl; std::cerr << "[invoking constructor]" << std::endl;
i = 3; i = 3;
} }
~C() { cerr << "[invoking destructor]" << endl; } ~C() { std::cerr << "[invoking destructor]" << std::endl; }
}; };
C c; C c;
@ -65,10 +54,10 @@ Object P_Test() {
return Make_Integer(c.i); return Make_Integer(c.i);
} }
void elk_init_constructor() { extern "C" void elk_init_constructor() {
Define_Primitive((Object (*)(...))P_Test, "test", 0, 0, EVAL); Define_Primitive((Object (*)(...))P_Test, "test", 0, 0, EVAL);
} }
void elk_finit_constructor() { extern "C" void elk_finit_constructor() {
cerr << "Goodbye." << endl; std::cerr << "Goodbye." << std::endl;
} }