# calling c++ function from script

**URL:** <https://community.unix.com/t/calling-c-function-from-script/146016>\
**Category:** Programming\
**Created:** [June 18, 2003, 4:48am UTC](https://community.unix.com/t/calling-c-function-from-script/146016 "2003-06-18T04:48:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Lebamb](https://community.unix.com/letter_avatar/lebamb/32/6_449c533f586939b6e5f246632a46421f.png) [@Lebamb](https://community.unix.com/u/Lebamb)\
**Post date:** [June 18, 2003, 4:48am UTC](https://community.unix.com/t/calling-c-function-from-script/146016/1 "2003-06-18T04:48:47Z")

</div>

hi,

I just started to work on unix,

I was wondering if it is possible to call a c++ function from a script.  
now, i don't mean starting a program, i mean dynamicaly calling a funtion (like working with a dll)

thanks

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [June 18, 2003, 11:09am UTC](https://community.unix.com/t/calling-c-function-from-script/146016/2 "2003-06-18T11:09:30Z")

</div>

Not in general. A few shells like ksh have some features that allow them to extended in this manner. And ksh will dynamically load a shared library if needed. But you can't use just any c++ function. It must be specially written to know how it will be called, what it must return, and what global variables it can use.

---

<div class="post-metadata">

**Author:** ![google](https://community.unix.com/user_avatar/community.unix.com/google/32/65_2.png) [@google](https://community.unix.com/u/google)\
**Post date:** [June 24, 2003, 6:55am UTC](https://community.unix.com/t/calling-c-function-from-script/146016/3 "2003-06-24T06:55:51Z")

</div>

> [@](#):
>
> Originally posted by Perderabo  
> A few shells like ksh have some features that allow them to extended in this manner. And ksh will dynamically load a shared library if needed.

Perderabo, Could you elaborate on this? and possibly point me in the direction of a good book with the details? This sounds interesting..

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [June 24, 2003, 9:53am UTC](https://community.unix.com/t/calling-c-function-from-script/146016/4 "2003-06-24T09:53:54Z")

</div>

Information is limited to say the least. I don't know of any book that covers it. And that doesn't surprise me. The details could change with the next release of ksh. There is a [Faq](http://www.kornshell.com/doc/faq.html) on the [Kornshell](http://www.kornshell.com/) which says:

> [@](#):
>
> Q4. How do I add built-in commands?  
> A4. There are two ways to do this. One is write a shared library  
> with functions whose names are b\_xxxx where xxxx is the name of  
> the builtin. The function b\_xxxx takes three argument. The first  
> two are the same as a mail program. The third parameter is  
> a pointer argument which will point to the current shell context.  
> The second way is to write a shared library with a function named  
> lib\_init(). This function will be called with an argument of 0  
> after the library is loaded. This function can add built-ins  
> with the sh\_addbuiltin() API function. In both cases, the  
> library is loaded into the shell with the "builtin" utility.

(I'm sure there's a typo in that quote, "mail program" should be "main program".) This is about all that I feel I quote here. In this [article](http://www.cs.princeton.edu/~jlk/lj/), there is an example. But the documentation with the source code had some differences. You really need to download the source code and read the docs that come with it. The file to look at is "builtins.mm" and you can read it with:  
nroff -mm \< builtins.mm | col -b | more
