From: janin@espresso.rt.cs.boeing.com (Adam Janin 5-3545)
Newsgroups: comp.soft-sys.khoros
Subject: Re: c++/khoros
Date: 18 Dec 92 18:29:52 GMT

A thread on C++ came up several months ago.  I stashed these replies:

--- Forwarded mail from Chien-Ping Lu <lu-chien-ping@cs.yale.edu>

We wrote several program in c++ before, and we want to link them with
our khoros programs. Of course, we don't want to rewrite them in c.
Has anyone tried incorporating programs written in c++ into Khoros?
Is it possible that we simply change CcCmd variable of the imake systems
to c++ or g++ so that we can develop khoros application in c++?

CP
--- End of forwarded message from Chien-Ping Lu <lu-chien-ping@cs.yale.edu>
	
	++++++++++++++++++++++++++++++++++++++++++++++++

--- Forwarded mail from Jun Lu <luj@ecn.purdue.edu>

 lu-chien-ping@cs.yale.edu worte:  
   > 
   > 
   > We wrote several program in c++ before, and we want to link them with
   > our khoros programs. Of course, we don't want to rewrite them in c.
   > Has anyone tried incorporating programs written in c++ into Khoros?
   > Is it possible that we simply change CcCmd variable of the imake systems
   > to c++ or g++ so that we can develop khoros application in c++?
   > 
   No. Simply changing CcCmd would not work. C++ is strongly typed. In addition
   to the apporpriate linkage directive such as 'extern "C" { foo(int) };', you
   must also supply prototypes for the c functions. However, the Khoros include
   header files does not suport C++(or ansi C) prototyping yet.
   
   I have never tried. But if you try. I am sure you'll get compling errors.
   
   > CP
   > 
   > 
   Good luck,
   
   -- John
--- End of forwarded message from Jun Lu <luj@ecn.purdue.edu>

	++++++++++++++++++++++++++++++++++++++++++++++++

--- Forwarded mail from danielle@bullwinkle.eece.unm.edu (Danielle Argiro)

	Hello,

	About using C++ programs with Khoros:

On Nov 25, 11:18am, Chien-Ping Lu wrote:
} Subject: c++ and Khoros
}
} We wrote several program in c++ before, and we want to link them with
} our khoros programs. Of course, we don't want to rewrite them in c.
} Has anyone tried incorporating programs written in c++ into Khoros?
} Is it possible that we simply change CcCmd variable of the imake systems
} to c++ or g++ so that we can develop khoros application in c++?
}
} CP
}-- End of excerpt from Chien-Ping Lu

	Well, this is a simple questions with a complicated answer.
	It really depends *how* you integrate the C++ programs into 
	the Khoros system.  
		------------

	Method 1) "unconventional" solution

	You don't *have* to "integrate" the C++ programs into
	the Khoros system at all.  Simply compile them separately from
	the Khoros system, and then make them available to cantata.
	Hitch: your programs must have no command line arguments, or
	their command line arguments must be formatted identically to
	those in the khoros system, eg,

	% program
	   or
	% program  [-flag1 {value1}  -flag2 {value2} .... ]          
	
	If they fit either of the cases above, making them available to 
	cantata is easily done by creating a UIS *.pane file where the 	
	[-R] line references your C++ program, and adding the UIS file 
	into cantata as detailed in Chapter 1 of the Khoros Programmer's Manual.

	Note:  this same method can be used to integrate Unix type commands,
	shell scripts, and so on, into the visual language.  The only 
	restriction at this time is that they must follow the [-flags {value}]
	convention for the command line. => Important: in the future, this 
	restriction will be lifted as we augment the operation of the [-R] UIS 
	line, and allow you to specify the command line format if it is 
	different than that of standard Khoros programs.
		------------

	Method 2) "hybrid" solution

	Suppose your C++ programs do NOT follow the Khoros convention of
	command line formatting:
	
	% program [-flag1 {value1}  -flag2 {value2} .... ]

	You can use ghostwriter as outlined in Chapter 5 of the Khoros
	Programmer's Manual to give them front ends.  In this case, the
	C++ program becomes your "library routine", while the code generated
	by ghostwriter will be the main driver.  Now, they can be integrated
	into cantata as detailed in Chapter 1 of the Khoros Programmer's 
	Manual, as well as maintained properly using the UIS and PS files.  
	We followed this method to integrate our several shell script tools
	(makemake, imkmf, vman, etc) into the conventional Khoros standard. 
	
	The problem here is that now you will be forced to compile C code
	with C++ code.  We have never attempted to do this, but it is safe to
	guess that you will have some problems.  It is possible, however, that 
	you can modify the Makefiles to compile them properly.  I must agree 
	with John Lu from Purdue when he says he doesn't think it will be as 
	simple as simply changing CcCmd.  Unfortunately, we cannot offer you 
	assistance in doing this, as we have no experience with it ourselves.
		------------
	
	Method 3) "conventional" solution

	This is the one you didn't want to hear.  But you can always 
	re-write the programs using C.   This will eliminate your problems
	as far as the Khoros system is concerned, but perhaps introduce a
	whole new set of problems that you don't want to deal with, and in
	any case take time that you don't want to spend.
		------------

	None of these methods solves the problem as neatly as you might 
	have hoped, but I hope that they at least go towards clarifying 
	the issues here.   I hope that you are able to come to a solution 
	that will suit your needs...

		~ danielle

       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
       ~   Danielle Argiro - danielle@bullwinkle.eece.unm.edu   ~
       ~   Software Design & Development                        ~
       ~   The Khoros Group                                     ~  
       ~   EECE Dept, University of New Mexico                  ~
       ~   Albuquerque, New Mexico  87131  	                ~
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
--- End of forwarded message from danielle@bullwinkle.eece.unm.edu (Danielle Argiro)
	++++++++++++++++++++++++++++++++++++++++++++++++

--- Forwarded mail from klamer@mi.eltn.utwente.nl (Klamer Schutte)

danielle@bullwinkle.eece.unm.edu (Danielle Argiro) wrote:

>	Hello,
>
>	About using C++ programs with Khoros:
>
>On Nov 25, 11:18am, Chien-Ping Lu wrote:
>} Subject: c++ and Khoros
>}
>} We wrote several program in c++ before, and we want to link them with
>} our khoros programs. Of course, we don't want to rewrite them in c.
>} Has anyone tried incorporating programs written in c++ into Khoros?
>} Is it possible that we simply change CcCmd variable of the imake systems
>} to c++ or g++ so that we can develop khoros application in c++?
>}
>} CP
>}-- End of excerpt from Chien-Ping Lu
>
>	Well, this is a simple questions with a complicated answer.
>	It really depends *how* you integrate the C++ programs into 
>	the Khoros system.  
>		------------

>> description of three methods how to use C++ programs / routines with <<
>> Khoros 								<<

It should also be possible to make C++ work with the Khoros libraries as
C++ can work with the standard C library.

This will involve prototypes of all functions called, and wrapping them in
a
extern "C" {
...
};
context. This does mean that all header files need to be updated to support
C++. (And has the advantage that with a little care ANSI C is supported at
the same time!).

Is this (changing all the header files) permissible for the Khoros group?
If so, the way is open to C++.

Klamer.
--
Klamer Schutte			Tel: +31-53-892786	Fax: +31-53-340045
Faculty of electrical engineering -- University of Twente, The Netherlands
preferred: klamer@mi.el.utwente.nl     SMTP: klamer@utelmi01.el.utwente.nl

--- End of forwarded message from klamer@mi.eltn.utwente.nl (Klamer Schutte)

	++++++++++++++++++++++++++++++++++++++++++++++++

--- Forwarded mail from tom@asi.com (Tom Baker)

> We wrote several program in c++ before, and we want to link them with
> our khoros programs. Of course, we don't want to rewrite them in c.
> Has anyone tried incorporating programs written in c++ into Khoros?
> Is it possible that we simply change CcCmd variable of the imake systems
> to c++ or g++ so that we can develop khoros application in c++?
> 
> CP
	
I have done this.  It is a more general problem of how you interface C
and C++ code.  I currently use the Sun C++ compiler, but I do remember
using the g++ compiler to do the same thing.  Although I never
actually interfaced g++ with Khoros, the same methods should apply.
It can be cumbersome at times, but it is worth doing if you have a lot
of C++ code.  What you have to do is develop a C interface to all of
the C++ code.  This involves writing C functions that call the C++
code, and compiling them with C++.  Then you call the C functions from
your Khoros code.

The idea is that you create a library or object module in C++, and
then link with the C code.  The C compiler requires a recognizable
label to link with.  The Sun compiler that I use has an explicit
mechanism for declaring C function names in the C++ environment.  As I
remember, the g++ compiler would declare any function that was not a
class member in a way that ld can understand.  

I generally avoid using the Khoros build system when working with
the C++ code.  I seperate all of the C++ and C code into different
directories.  I either make a C++ library and put it in the
'$TOOLBOX/lib' directory, or I make a C++ object module and copy it
into the Khoros working directory.

You will also want to create a set of member functions that read and
write the viff format from your C++ format.  Once you do this you can
switch between the C++ and Khoros environments pretty easily.

You must use the C++ debugger.  The Sun C debugger core dumps when it
sees the executable.  When I was using g++ there was a seperate
debugger (gdb++ ?), but I think I remember hearing that gdb will
handle both now.

Try to get the C++ <-> C interface working with some toy problems
before you jump into the full Khoros environment.  I hope that this
helps, let me know if you have any problems.

Good luck.


Thomas Baker                    INTERNET: tom@asi.com  
Adaptive Solutions, Inc.	UUCP:  (uunet,ogicse)!adaptive!tom
1400 N.W. Compton Drive, Suite 340      
Beaverton, Oregon 97006

--- End of forwarded message from tom@asi.com (Tom Baker)

	++++++++++++++++++++++++++++++++++++++++++++++++

--- Forwarded mail from Ue-Li Pen <upen@astro.princeton.edu>

a remark on calling c++ from c:

	Method 2) "hybrid" solution

		...

	The problem here is that now you will be forced to compile C code
	with C++ code.  We have never attempted to do this, but it is safe to
	guess that you will have some problems.  It is possible, however, that 
	you can modify the Makefiles to compile them properly.  I must agree 
	with John Lu from Purdue when he says he doesn't think it will be as 
	simple as simply changing CcCmd.  Unfortunately, we cannot offer you 
	assistance in doing this, as we have no experience with it ourselves.
			------------
		
			~ danielle


apropos makefiles, here is a message I grabbed off the comp.lang.c++ newsgroup
which works for ATT CC 2.1.  With g++ you can take a look at the "collect3.c"
file (g++ 1.40.3) in the source code and modify it for your purposes.  It
handles the initialization of global constructors, and replaces the system
loader ld.

>
>calling C++ functions from C program is relatively easy but some precautions
>have to be taken.
>
>I will considere here an example under Sun environment.
>the aim is to call C++ member functions foo() of a class A 
>from a main C program...
>
>Assume the C++ file (named cxxfile.cc) contains following instructions:
>
>	class A 
>	{
>		...
>
>		int foo(int x);
>
>		...
>	};
>
>	...
>
>	A data;   /* declaration of an object A */
>
>	...
>
>	extern "C" int cxxfoo(int x)
>	{
>		data.foo(x);
>	}
>
>Then the C program (main.c) has to contain:
>
>	...
>
>	extern int cxxfoo(int);
>
>	...
>
>	main()
>	{
>		_main();
>		...
>
>		cxxfoo(36);
>
>		...
>
>	}
>
>Now follow next steps:
>
>1/ Generate a C source file from your C++ file
>	
>		CC -Fc cxxfile.cc >cxxfile.c
>
>2/ compile all C source file in order to produce object files
>
>		cc -c cxxfile.c -o cxxfile.o
>		cc -c main.c	-o main.o
>
>3/ build a C source file that allow call of static constructors/destructors
>
>		nm cxxfile.o  | munch > cxxconstruct.c
>
>		-nm gives symbol table of each object file
>		-munch	build a C source file containing initialization of ctors and
>			dtors variables (called by _main() )
>
>4/ compile the generated file
>
>		cc -c cxxconstruct.c -o cxxconstruct.o
>
>5/ link all objects files with C++ library (e.g. in Sun4 environment)
>
>		cc -o main main.o cxxfile.o cxxconstruct.o -L /usr/CC/sun4 -lC
>
>
>It works correctly and I think it will resolve your problem.
>
>
>Good Luck !
>
>Rachid HIMMI (emai rh@absstbg.uucp)
>
>


Ue-Li  (upen@astro.princeton.edu)


--- End of forwarded message from Ue-Li Pen <upen@astro.princeton.edu>

	++++++++++++++++++++++++++++++++++++++++++++++++

--- Forwarded mail from jhh@ii2.dsto.oz.au (John Hildebrandt)

I am interested in writing primitives for Khoros in C++.

I wish to still use composer and ghostwriter/reader to generate the shell code
( C OK here ) but then write the library code ( the algorithm ) in C++.

I am using a SUN Sparcstation 2 with SUN C and C++.

Can anyone give hints on how this maybe done.

-----------------------------
John Hildebrandt
jhh@itd.dsto.oz

--- End of forwarded message from jhh@ii2.dsto.oz.au (John Hildebrandt)

	++++++++++++++++++++++++++++++++++++++++++++++++

--- Forwarded mail from klamer@mi.eltn.utwente.nl (Klamer Schutte)

In comp.soft-sys.khoros jhh@ii2.dsto.oz.au (John Hildebrandt) write:

>I am interested in writing primitives for Khoros in C++.

>I wish to still use composer and ghostwriter/reader to generate the shell code
>( C OK here ) but then write the library code ( the algorithm ) in C++.

>I am using a SUN Sparcstation 2 with SUN C and C++.

I am doing this reguarly now with Sun C++ 2.1. I use composer & ghostwriter to
generate a main() with command line handling. The C++ function does the real
thing.

One problem: Sun C++ 2.1 need a C++ main, not a C. So i have a

#define	main	my_main

as a #define in the ghostwriter-generated C main. And a simple wrapper
in C++ for the main function:

extern "C" my_main(int argc, char *argv[]);

main(int argc, char *argv[])
{
	return my_main(argc,argv);
}

The library function also needs the extern "C" directive.

Klamer

PS a cleaner way should be to have KHOROS support in a similar way for C++ as
   for fortran.


Klamer Schutte			Tel: +31-53-892786	Fax: +31-53-340045
Faculty of electrical engineering -- University of Twente, The Netherlands
preferred: klamer@mi.el.utwente.nl     SMTP: klamer@utelmi01.el.utwente.nl

--- End of forwarded message from klamer@mi.eltn.utwente.nl (Klamer Schutte)