-
Notifications
You must be signed in to change notification settings - Fork 584
Basic Architecture
The three main components of JavaCPP are the Builder
, the Loader
, and the Pointer
. The first one takes care of parsing C/C++ header files and building native libraries at compile time, the second gets invoked at runtime to load these libraries, and extract them from JAR files into the cache if necessary, while the third is responsible for providing an API to call native functions and access native variables.
The execution of the Builder
typically consists of 4 stages, either as a standalone command line utility or via a plugin such as BuildMojo
for Maven:
- Runs optional external build scripts, such as
cppbuild.sh
from the JavaCPP Presets,- Outputs native libraries (.so, .dylib, .dll, etc),
- Runs the
Parser
on the givenInfoMap
and the header files listed in the@Platform
annotation,- Outputs native Java interfaces (.java), as explained further in the Mapping Recipes,
- Runs the
Generator
on the Java interfaces,- Outputs native JNI code in C++ (.cpp, .cu, .mm, etc),
- Runs the native C++ compiler (GCC, Clang, MSVC, NVCC, etc) on the JNI code,
- Outputs native libraries containing JNI wrappers, optionally archived in a JAR file.
When executing Loader.load()
at runtime, either manually or as part of the automatically generated static { }
blocks, it does 3 things automatically:
- Searches the class path for the native libraries, including all dependencies,
- Extracts them, if found in JAR files, to its cache by default in
~/.javacpp/cache
, - Loads all the libraries via
System.load()
, orSystem.loadLibrary()
if not found on the class path.
Users access through subclasses of Pointer
all native arrays, functions, and objects of class
, struct
, or union
types. The following subclasses are provided by default for basic data types:
-
BytePointer
,ShortPointer
,IntPointer
,LongPointer
,CLongPointer
,SizeTPointer
,FloatPointer
,DoublePointer
,BoolPointer
,CharPointer
,PointerPointer
,FunctionPointer
Pointer
calls all functions, and reads and writes all variables via JNI, which introduces overhead. For this reason, JavaCPP also comes with the indexer
package, mapping native data to multidimensional arrays (aka tensors, including unsigned integer types and half floats) via sun.misc.Unsafe
, to support memory regions larger than 2 GB, falling back on NIO buffers only when not supported such as on Android.
There is also PointerScope
to manage more easily and efficiently the native memory and resources used by a group of Pointer
objects.