Function libraries are the easiest to create. They only let the user call functions in your library and do not provide any classes. The first step is to create internal function. Just create a global function in your library module, for example:
Bool iMyLibraryFunction(Int32 v, String s) { GePrint("v: " + String::IntToString(v) + ", s: " + s); return v != 42; }
Note the naming convention with an i
in front. Then you need to create the library structure. This should be a structure derived from _<_span title="struct c4dlibrary"_>_C4DLibrary with a function pointer to your kind of function. The struct
should be placed in the public header file of your library and #included
into the library module:
struct MyFunctionLib : public [\_ry">C4DLibrary](../c4d_library/struct_C4DLibrary251.html) { Bool (\*MyLibraryFunction)(Int32 v, String s); };
Creating a function pointer from a function is really only adding two parentheses and a \*
like above. Next you should create a global instance of this struct in your library module and write the registration function like this: (With the _IORITY">C4DMSG_PRIORITY message you should tell CINEMA 4D that your library needs to be registered before other plugins.)
`
MyFunctionLib flib;
Bool RegisterMyFunctionLib()
{
// Clear the structure
ClearMem(&flib, sizeof(flib));
// Fill in all function pointers
flib.MyLibraryFunction = iMyLibraryFunction;
// Install the library
return _Int32 size">InstallLibrary(MY_UNIQUE_FUNCTION_LIBRARY_ID, &flib, 1000, sizeof(flib));
}
Note that you do not need a
&to take the function pointer address of the
iMyLibraryFunctionfunction. Also note that
MY_UNIQUE_FUNCTION_LIBRARY_IDshould be a unique ID from [Plugin Cafe](http://www.plugincafe.com). It should be placed in the public header:
const Int32 MY_UNIQUE_FUNCTION_LIBRARY_ID = ...;
Now we only need to create the public glue to this library. In the public header file, add in a function with the same signature as your internal one: (This is why we appended an
ito the internal function, because otherwise they would have collided when we included the public header into the library module.)
Bool MyLibraryFunction(Int32 v, String s);
Then you create a public _.cpp_ module for your library. In it you first create this helper function:
MyFunctionLib* flib_cache = nullptr;
MyFunctionLib* CheckMyFunctionLib(Int32 offset)
{
return (MyFunctionLib* ) _DLibrary store">CheckLib(MY_UNIQUE_FUNCTION_LIBRARY_ID, offset, (C4DLibrary** ) &flib_cache);
}
Finally you implement the glue function like this:
Bool MyLibraryFunction(Int32 v, String s)
{
MyFunctionLib* flib = CheckMyFunctionLib([_rary::LIBOFFSETs,m">LIBOFFSET(MyFunctionLib, MyLibraryFunction)](../c4d_library/global_c4d_library213.html#liboffse_<_span title=));
if (!lfib || !flib->MyLibraryFunction) return false ;
return flib->MyLibraryFunction(v, s);
}
Now anyone can import your public _myfunctionlibrary.h_ and _myfunctionlibrary.cpp_ files into their project, and as long as the library is installed the function
MyLibraryFunction()` will work just as an ordinary function.
This is the complete listing for each file:
`
// myfunctionlibrary.h
#include "c4d.h"
Bool MyLibraryFunction(Int32 v, String s);
// INTERNAL
const Int32 MY_UNIQUE_FUNCTION_LIBRARY_ID = ...;
struct MyFunctionLib : public [_tle="struct C4DLibrary">C4DLibrary](../c4d_library/struct_C4DLibrary25_<_span title=)
{
Bool (*MyLibraryFunction)(Int32 v, String s);
};
// INTERNAL
// myfunctionlibrary.cpp
#include "myfunctionlibrary.h"
MyFunctionLib* flib_cache = nullptr;
MyFunctionLib* CheckMyFunctionLib(Int32 offset)
{
return (MyFunctionLib* ) [_t offset, C4DLibrary store">CheckLib(MY_UNIQUE_FUNCTION_LIBRARY_ID, offset, (C4DLibrary** ) &flib_cache)](../c4d_library/global_c4d_library213.html_<_span title=);
}
Bool MyLibraryFunction(Int32 v, String s)
{
MyFunctionLib* flib = CheckMyFunctionLib([_le=" c4d_library::LIBOFFSETs,m">LIBOFFSET(MyFunctionLib, MyLibraryFunction)](../c4d_library/global_c4d_library213.h_<_span title=));
if (!lfib || !flib->MyLibraryFunction) return false ;
return flib->MyLibraryFunction(v, s);
}
// myfunctionlibrarymodule.cpp
#include "myfunctionlibrary.h"
Bool iMyLibraryFunction(Int32 v, String s)
{
GePrint("v: " + String::IntToString(v) + ", s: " + s);
return v != 42;
}
MyFunctionLib flib;
Bool RegisterMyFunctionLib()
{
// Clear the structure
ClearMem(&flib, sizeof(flib));
// Fill in all function pointers
flib.MyLibraryFunction = iMyLibraryFunction;
// Install the library
return [_lib,Int32 version, Int32 size">InstallLibrary(MY_UNIQUE_FUNCTION_LIBRARY_ID, &flib, 1000, sizeof(flib))](../c4d_library/global_c4d_library213.ht_<_span title=);
}