Since we are going to use our library from another language (and compiler), we need to be able to find these function by name only. However, when exporting to a shared library these functions need to have an unique name, and how this name is generated (=name mangling) depends on the compiler. In C++ you can have several (overloaded) functions with the same name. This tells the compiler to use C linkage for the function. Adds “extern “C”” to the beginning of the function definition.# define NATIVEPLUGIN_API extern "C" _declspec(dllimport)įirst, we define our NativePlugin_EXPORTS preprocessor definition, which does the following: # define NATIVEPLUGIN_API extern "C" _declspec(dllexport) Plugin source codeįirst, create a C++ header file called native_plugin.h: In other words: We first write the code (in C++), then build it for a specific architecture (Linux, Windows, Android) to a shared library (.dll /. When making plugins for Unity, we want to use shared (dynamic) libraries. On Windows, shared (dynamic) libraries are stored in “.dll” files, while on Linux and Android they have the “.so” file extension. These are libraries that can be loaded at runtime, while static libraries are resolved at compile time. On Windows these are called DLLs (Dynamic Link Library, Microsoft’s implementation of the shared library concept). If you’re a Windows user then you’re probably already familiar with shared libraries. There are two types of libraries: shared libraries and static libraries. The library file will contain the compiled code (similar to an executable), but is meant to be used as a library rather than being executed directly. If you want to write some code in language A and use it in a software made with language B, then you need to compile the code into a library and then use that library from language B. The first thing you need to know, is what a shared/dynamic library is. NOTE: I have created a git repository for this tutorial here: (simply use that if you’re in a hurry) Shared libraries In this post, I will go more into details, and show an example where I create a plugin in C++, build it for Linux, Windows and Android, and use it through Unity. Unity’s documentation explains briefly how this can be done, but when it comes to cross platform plugins (for Windows, Linux, Android, etc.) I found the documentation a bit lacking. Maybe you needed to write some of your code in C++, or there is a C++ library that you want to use in Unity. When developing games and software in Unity you sometimes need to use native C++ plugins.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |