Reader small image

You're reading from  Java Coding Problems - Second Edition

Product typeBook
Published inMar 2024
PublisherPackt
ISBN-139781837633944
Edition2nd Edition
Right arrow
Author (1)
Anghel Leonard
Anghel Leonard
author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard

Right arrow

166. Calling the sumTwoInt() foreign function

Do you remember the sumTwoInt() function? We have defined this C function in a native shared library named math.dll (check Problems 144, 145, and 146). Let’s assume that we have placed the math.dll library in the project folder under the lib/cpp path.

We can call this foreign function in almost the same manner as we’ve called _getpid(). Since math.dll is a user-defined library that is not commonly used, it cannot be loaded via defaultLookup(). The solution is to explicitly load the library from the lib/cpp path, as follows:

Linker linker = Linker.nativeLinker();
Path path = Paths.get("lib/cpp/math.dll");
try (Arena arena = Arena.ofConfined()) { 
  SymbolLookup libLookup = SymbolLookup.libraryLookup(
    path, arena);
  ...

Next, we have to find in math.dll the foreign function by name. If your C compiler (for instance, G++) has applied the mangling (or name decoration) technique, then sumTwoInt will...

lock icon
The rest of the page is locked
Previous PageNext Page
You have been reading a chapter from
Java Coding Problems - Second Edition
Published in: Mar 2024Publisher: PacktISBN-13: 9781837633944

Author (1)

author image
Anghel Leonard

Anghel Leonard is a Chief Technology Strategist and independent consultant with 20+ years of experience in the Java ecosystem. In daily work, he is focused on architecting and developing Java distributed applications that empower robust architectures, clean code, and high-performance. Also passionate about coaching, mentoring and technical leadership. He is the author of several books, videos and dozens of articles related to Java technologies.
Read more about Anghel Leonard