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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.