C++ code via NMODL with C wrappers?

NMODL and the Channel Builder.
Post Reply
mctavish
Posts: 74
Joined: Tue Mar 14, 2006 6:10 pm
Location: New Haven, CT

C++ code via NMODL with C wrappers?

Post 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
wwlytton
Posts: 66
Joined: Wed May 18, 2005 10:37 pm
Contact:

C++ in mod

Post 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
csh
Posts: 52
Joined: Mon Feb 06, 2006 12:45 pm
Location: University College London
Contact:

Post 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
Last edited by csh on Tue Apr 25, 2006 4:17 pm, edited 1 time in total.
mctavish
Posts: 74
Joined: Tue Mar 14, 2006 6:10 pm
Location: New Haven, CT

Post 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
hines
Site Admin
Posts: 1607
Joined: Wed May 18, 2005 3:32 pm

Post 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.
Post Reply