Page 1 of 1
C++ code via NMODL with C wrappers?
Posted: Thu Apr 20, 2006 11:49 am
by mctavish
I understand that C code can be written in NMODL. What I'm wondering, though, is if that C code can contain wrappers to C++ objects/libraries. If so, is there a simple example of how to link?
Thanks,
Tom
C++ in mod
Posted: Fri Apr 21, 2006 9:03 am
by wwlytton
You cannot include C++ in mod files.
However there are C++ packages in Neuron: vectors and lists that have wrappers compiled and therefore can be used. eg
extern void vector_resize();
extern int vector_instance_px();
One could use these examples to include an additional file in the initial build and provide wrappers for subsequent use in mod. I'm not sure how exactly how/where to add a new source file so that it gets picked up in the build sequence.
Bill
Posted: Mon Apr 24, 2006 4:29 pm
by csh
Hi,
I'm not sure how exactly how/where to add a new source file so that it gets picked up in the build sequence.
You can add a C++ file to any directory (at least, it worked with the scopmath directory). Just name it *.cpp so that g++ and C++-specific compiler flags will be used instead of gcc. You will have to add it to the directory's Makefile.am as well and run automake and autoconf before building.
If you want to call your C++ functions from a C file, you will have to use the extern keyword when declaring or defining your C++ function:
Code: Select all
extern "C" void someCppFunc(/*...*/) {/*...*/}
Thereby, you specify that C's linkage conventions are being used.
Details on how to call C functions from the interpreter are given in this thread:
https://www.neuron.yale.edu/phpBB2/viewtopic.php?t=95
Contact me if you need sample code or more details. I hope I could help,
Christoph
Posted: Tue Apr 25, 2006 1:58 pm
by mctavish
Thanks for the replies. I was going the route of modifying Makefile.am files and building a custom version of NEURON, but I was hoping for something where I could add my code to the standard NEURON build as with NMODL files.
Tom
Posted: Sat Apr 29, 2006 9:03 am
by hines
It might not be too difficult to modify the nrnivmodl and nrniv_makefile scripts in nrn/bin (actually the *.in precursor files if you have autoconf/automake/libtool installed) to look at the suffixes of the files you pass to nrnivmodl (default is all the
mod files in the working directory). Then you could do anything you wanted with c++ and enclose what you wanted exposed to hoc in extern "C"{}. Then get all the c2hoc interface magic with a mod file that called them from FUNCTION or
PROCEDURE blocks. If you want an entirely new c++ class just like the ones
in the ivoc or nrniv source directories it should not be too difficult to call the
class2oc registration function from
a stub mod file.
The only problem with all this is that it will not work with the current mswin installer distribution since I supply only the minimal c development stuff and left out the c++ stuff.