Xref: utzoo comp.lang.c++:11599 gnu.g++.help:464 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!spool.mu.edu!uunet!mcsun!unido!pbinfo!.uni-paderborn.de!eggberd From: eggberd@.uni-paderborn.de (Bernd Eggert) Newsgroups: comp.lang.c++,gnu.g++.help Subject: Combination of C and C++ Message-ID: <1991Feb12.151007.29831@uni-paderborn.de> Date: 12 Feb 91 15:10:07 GMT Sender: eggberd@uni-paderborn.de (Bernd Eggert) Organization: Uni-GH Paderborn, West Germany Lines: 120 This message is posted for someone who cannot post to the net himself. Please respond to the net or to axel@cadlab.cadlab.de ! ------------------------------------------------------------- I have a question concerning the combination of C and C++. Assume the following situation: You want to write a library in C++. People working with your library should be able to use it either from C++ or from C. In order to make functions callable from C you use the 'extern "C"' directive. With this directive you can write ordinary C functions 'around' the C++ objects. These functions can be called from a C program. The problem occurs when you try to link the C program with the C++ library. The normal linker does not care for constructors or destructors and therefore static objects will not be initialized. Is there an elegant and portable way to solve this problem? We are interested in solutions for AT&T CC and g++. Below is an example which works if you use the C++-linker (but not with a normal linker) Thanks for any help. ------------------------------------------------------------------------------- File 'hello.C': ------------------------------------------------------------------------------- /* this is a C++ class to be used by a C program */ #include #include #include class hello { private: char name[20]; public: hello(char *n) { strcpy(name,n); } void print() { cout << "Hello " << name << "\n"; /* cout requires initialization */ printf("Hello %s\n",name); /* printf is no problem */ } }; /* these functions enable the non-C++-user to use the class 'hello': */ extern "C" { void *create_person(char *n) { return((void *)new hello(n)); } void print_person(void *p) { ((hello *)p)->print(); } } ------------------------------------------------------------------------------- File 'chello.c' ------------------------------------------------------------------------------- /* this is a C program dealing with a C++ object */ void *create_person(char *n); void print_person(void *p); main() { void *p; __main(); /* this is necessary for initializing static objects in g++ */ p = create_person("Henry"); print_person(p); } ------------------------------------------------------------------------------- File 'makefile': ------------------------------------------------------------------------------- chello: chello.o hello.o g++ -o chello chello.o hello.o chello.o: chello.c gcc -c chello.c hello.o: hello.C g++ -c hello.C ------------------------------------------------------------------------------- Axel Meckenstock CADLAB University of Paderborn phone : +49 5251 284-120 Siemens Nixdorf Information Systems fax : +49 5251 284-140 Bahnhofstr. 32 email : axel@cadlab.cadlab.de D-4790 Paderborn -------------------------------------------------------------------------------