The relatively new V Programming Language (Vlang) boasts of some interesting features such as “fast compilation” and runtime performance “as fast as C”, at the same time being a “simple language”.
As I normally do with other languages that I learn and use, I decided to see how easy it is to build a shared library, a Windows DLL in this case. Today’s article explains this aspect of the language.
I am using V 0.3.3 on my Windows 10 (64 bit) machine.
First, let us define a couple of functions. Here is the code I wrote:
The function “fibonacci” computes the “nth” Fibonacci number and the function “lucas” computes the “nth” Lucas number. Because both functions are identical except for the starting values, I wrote a helper function to share the logic.
The functions that need to be exported from the DLL must use the “export” attribute and must be defined as “pub”. Accordingly, the functions “fibonacci” and “lucas” are both public and have the “export” attribute, whereas the helper function is private and is not exported.
Compiling the source to generate the DLL is quite simple. Just run “v -shared -prod dllfns.v” from the command prompt (Note: This requires “gcc” or “MSVC” to be present in the PATH). This will create the DLL “dllfns.dll” along with a few other files. To use the generated DLL in another program, we do not require any other dependent file. This makes deploying the DLL easy.
To consume the Vlang DLL, I first wrote a C++ program:
I copied the DLL to the C++ executable directory and then ran the program:
Works as expected!
As another test, I wrote a Lisp program (LispWorks 64 bit Windows) to call the two functions defined in the DLL:
That also works perfectly!
My first experience with Vlang is quite positive and I am quite eager to explore the other features of the language in due course.
Here are the source files I used.
Have a great weekend!
Recent Comments