It is possible to make a library that can be used in many languages? Or at least a library th开发者_JAVA技巧at can be used in several languages.
If so, what documentation do you recommend me to achieve this?
Yes, by providing a C interface to it. Most if not all mayor languages provide the ability to bind to C functions.
In .NET, any assembly you create can be used by any other .NET language. So if you create a library in C#, you can make use of it in J#, C++.NET, VB.NET, etc.
The Java Virtual Machine(JVM) can run many languages (not just Java). Any library written in one of these languages can be called from another language on the list.
SWIG can be used to automatically create many of the language wrappers everyone's talking about here. In many cases, the wrapper has two components: a C++ one that is rolled into your DLL, and one written in the language.
A C library will be accessible from the most languages in the most environments.
However, you ask in your comment how to define classes for use in multiple languages. C has no classes. If you want an object-oriented API, you can:
- Define Java classes for use with languages running on the JVM.
- Define .NET classes for use with languages running on .NET.
One option, depending on what you're writing, may be to write a single implementation in C, then expose it via separate wrappers in C++, Java and .NET.
A library can be created that can be used by more than one language. One of the issues is the calling convention.
A calling convention defines how parameters are passed to a function as well as any setup required. For example, one convention may specify that parameters are passed from right to left (the right most parameter first). Some conventions say to pass values in the N registers; others demand that all parameters passed by address (pointer).
Some calling conventions may decrement a stack pointer, which is used by the function, for storing parameters. Some may not even use a stack.
Yes, a library can be used by more than one language. The trick is how to write code in a language to properly access functions in the library.
精彩评论