16

Is there a way to call a C++ shared library function from within a vim plugin written in vimscript?

Say there is a hello_world.so that has a function hello_world(). I want to call this function whenever the vim user uses a particular key binding.

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
Vamsi
  • 365
  • 2
  • 8

1 Answers1

14

Yes you can do this, try: help libcall

You'll have to export the functions as undecorated C functions with the "cdecl" calling convention I suspect:

From vim help:

For Win32, the functions you write must be placed in a DLL and use the normal C calling convention (NOT Pascal which is used in Windows System DLLs). The function must take exactly one parameter, either a character pointer or a long integer, and must return a character pointer or NULL. The character pointer returned must point to memory that will remain valid after the function has returned (e.g. in static data in the DLL). If it points to allocated memory, that memory will leak away. Using a static buffer in the function should work, it's then freed when the DLL is unloaded.

There's an example of how to do it here.

eckes
  • 64,417
  • 29
  • 168
  • 201
Benj
  • 31,668
  • 17
  • 78
  • 127
  • @Vamsi One can also write in C++ extension to python, perl or whatever other language you want that both supports something like FFI (foreign function interface) and is supported by vim itself. It should be less tricky as C++ extensions to python are much more common then C++ extensions to vim and if something happens you’ll be able to get more help. – ZyX Jan 23 '12 at 16:58