JNI header in cmd prompt - java-native-interface

I am trying to make an header for JNI
This is the code in java:
package javaapplication2;
public class JavaApplication2 {
public static void main(String[] args) {
JavaApplication2 obj=new JavaApplication2();
obj.print();
// TODO code application logic here
}
private native void print();
}
The cmd prompt command is
cd C:\Users\ABC\Documents\NetBeansProjects\JavaApplication2
javah -o head.h-jni-classpath../../build/classes javaapplication2.JavaApplication2.java
It shows these errors
Exception in thread "main" java.lang.IllegalArgumentException: directories not supported
at com.sun.tools.javac.file.RegularFileObject.(RegularFileObject.java:70)
at com.sun.tools.javac.file.RegularFileObject.(RegularFileObject.java:64)
at com.sun.tools.javac.file.JavacFileManager.getJavaFileObjectsFromFiles(JavacFileManager.java:785)
at com.sun.tools.javah.JavahTask.run(JavahTask.java:463)
at com.sun.tools.javah.JavahTask.run(JavahTask.java:329)
at com.sun.tools.javah.Main.main(Main.java:46)

Note that javah is deprecated. With JDK 8 or higher you can use javac -h. It's also easier:
cd C:\Users\ABC\Documents\NetBeansProjects\JavaApplication2\javaapplication2
javac -h .. JavaApplication2.java

Related

Tensorflow Lite c++ Build

I have already put a question about the access violation of the TensorFlow lite c++ API. No one answered it so far, I believe the error I made is with selecting the wrong header- and library files from the Bazel build.
The steps that I have done to get the Tensorflow Lite Header and Libraries are from Youtube Tutorial and from Tensorflow.
Get Required Python (for me Python 3.9.5)
Install required Packages locally
Install Bazel (for me 3.7.2) and MSYS2 (after installation run pacman -S git patch unzip) and add it to Path
Check VS Build Tools 2019 for C++ (I have VS 19 Community with MSVC v142 & Windows 10 SDK)
Download and Unzip Tensorflow Sources from Github (Release of 2.5.3)
Inside the Tensorflow Sources, use python .\configure.py to start configure the bazel build (I only used Yes for override eigen strong inline, the rest is kept on the default value)
The I opened GitBash cmd inside the tensorflow source bazel build -c opt //tensorflow/lite:tensorflowlite
After a successful build later I get the "bazel-bin", "bazel-out", "bazel-tensorflow-2.5.3" and "bazel-testlogs" folder.
I created the following folders tensorflow/include/tensorflow/lite & core and tensorflow/include/flatbuffers for the headers and finally the tensorflow/lib for the libraries.
I copied the tensorflowlite.dll & tensorflow.dll.if.lib from the build directory (tensorflow-2.5.3\bazel-bin\tensorflow\lite) into the tensorflow/lib directory together with the flatbuffers.lib (from tensorflow-2.5.3\bazel-bin\external\flatbuffers\src)
I copied the tensorflow-2.5.3\bazel-bin\external\flatbuffers\src_virtual_includes\flatbuffers\flatbuffers headers into the tensorflow/include/flatbuffers directory
I copied the tensorflow-2.5.3\tensorflow\lite and tensorflow-2.5.3\tensorflow\core from the original sources into the tensorflow/include/tensorflow/lite & core directory.
After those steps, I could create a new VS Project and add the created linker and include information. And created the following short example to read the input layer.
#include "tensorflow/lite/interpreter.h"
#include "tensorflow/lite/kernels/register.h"
#include "tensorflow/lite/model.h"
#include "tensorflow/lite/optional_debug_tools.h"
#define TFLITE_MINIMAL_CHECK(x) \
if (!(x)) \
{ \
fprintf(stderr, "Error at %s:%d\n", __FILE__, __LINE__); \
exit(1); \
}
int main()
{
std::string filename = "C:/project/tflitetesting/models/classification/mobilenet_v1_1.0_224_quant.tflite";
std::unique_ptr<tflite::FlatBufferModel> model =
tflite::FlatBufferModel::BuildFromFile(filename.c_str());
tflite::ops::builtin::BuiltinOpResolver resolver;
tflite::InterpreterBuilder builder(*model, resolver);
std::unique_ptr<tflite::Interpreter> interpreter;
builder(&interpreter);
TFLITE_MINIMAL_CHECK(interpreter->AllocateTensors() == kTfLiteOk);
printf("=== Pre-invoke Interpreter State ===\n");
tflite::PrintInterpreterState(interpreter.get());
interpreter->SetAllowFp16PrecisionForFp32(true);
interpreter->SetNumThreads(1);
// Get Input Tensor Dimensions
unsigned char* input = interpreter->typed_input_tensor<unsigned char>(0);
}
But I am still receiving the access violation exception inside interpreter.h at
const Subgraph& primary_subgraph() const {
return *subgraphs_.front(); // Safe as subgraphs_ always has 1 entry.
}
What am I doing wrong? I dont want to build the shared library since the target (Coral Edge) has direct access to those functions (ex. interpreter->typed_input_tensor<unsigned char>(0); too.
The thing is, you cannot Debug a Release (Optimized) version.
with the command bazel build -c opt //tensorflow/lite:tensorflowlite you will create an "Release" version of the dll's and lib's.
Therefore just apply bazel build -c dbg //tensorflow/lite:tensorflowlite to get the debug tflite c++ version.

dyld[49745] missing symbol called using c++ swig with node.js by calling a function out of the library

I try to create an example using SWIG and NodeJS on my M1 (arm64) Mac,
but I want to mention this as early as possible:
this issue appears also on an Intel (x64) Mac.
I create my simple example Files like this:
example.h
#pragma once
class Die
{
public:
Die();
~Die();
int foo(int a);
};
Die* getDie();
//to test if the issue also appears getting a simple functiom without any class context.
extern "C"
{
bool getFoo();
}
here is the implementation.
example.cpp
#include <iostream>
#include "example.h"
int Die::foo(int a)
{
std::cout << "foo: running fact from simple_ex" << std::endl;
return 1;
}
Die::Die()
{
}
Die::~Die()
{
}
// out of Class Context
Die* getDie()
{
return new Die();
}
extern "C"
{
bool getFoo()
{
return true;
}
}
my Swig interface is as follows:
example.i
%module example
%{
#include "example.h"
%}
%include "example.h"
then i create my example_warp.cxx file. But as actually the 4.0.2 Version of Swig is not compatible with NodeJs v16.0.0 (read SWIG support for NodeJS v12 #1520 and Prepare SWIG for Node.js v12 #1746).
Therefore i needed to build swig from source using master branch with the current version (4.1.0). Please keep that in mind.
Swig Command:
swig -Wall -c++ -javascript -node example.i
Here now some files preparing to create the .node file
package.json
{
"name": "SwigJS",
"version": "0.0.1",
"scripts": {
"start": "node index.js",
"install": "node-gyp clean configure build"
},
"dependencies": {
"nan": "^2.16.0",
"node-gyp": "^9.0.0"
},
"devDependencies": {
"electron-rebuild": "^3.2.7"
}
}
the package.json i got from a mate as an example an edited it to work with my project so there may be some lines not really needed by me.
binding.gyp
{
"targets": [
{
"target_name": "SwigJS",
"sources": [ "example_wrap.cxx" ],
"include_dirs" : [ "<!(node -e \"require('nan')\")" ]
}
]
}
now i build my SwigJS.node file using:
node-gyp configure
node-gyp build
it runs through without any errors.
Now i try to access the node-file in my JavaScript but i always get the error message:
missing symbol called
index.js
const Swigjs = require("./build/Release/SwigJS.node");
console.log("exports :", Swigjs); //show exports
die = Swigjs.getDie(); //try to get the Class
console.log(die.foo(5)); //call a function from the class
the output looks like this:
[Running] node "/Users/rolf/Documents/SwigJS/index.js"
exports : {
getDie: [Function (anonymous)],
getFoo: [Function (anonymous)],
Die: [Function: Die]
}
dyld[49745]: missing symbol called
[Done] exited with code=null in 0.12 seconds
What i have tried to find the error:
tried to build the .node file on an x64 architecture to check if it is an arm topic with NodeJS v16:17 on an Intel x64 Mac from a mate.
installed NodeJS 16.0.0 (first version supporting arm64 on mac)
as the Github Issues suggest NodeJS version 12 tried to build and run this on an x64 Intel Mac with NodeJS v12.13.0
tried to force x64 architecture leading to different error but because of incompatible library (x64) using a arm64 mac
all of it (except last mentioned) ended with the same result "missing symbol called"
help would be appreciated big.
Your question is an interesting take on a FAQ on this site: What is an undefined reference/unresolved external symbol error and how do I fix it?.
The role of SWIG is indeed to generate glue code between Node.js and C++ code. But it does only that. If you inspect the .dylib file that is associated with your NodeJS module using the nm command, you will see that it has Undefined references to the C++ functions it wraps.
This is by design. SWIG expects that the code it wraps is somehow already loaded into memory.
There are three approaches to do so:
Compile example.cpp directly into the SWIG wrapper. Or compile it to a static library first (example.a) and link that statically into the wrapper. I think it suffices to add example.cpp to the sources section of binding.gyp
Compile example.cpp into a library (example.dylib) and dynamically link it to the SWIG wrapper. I have not used GYP myself yet, but I think it means adding the following to your targets entry in bindings.gyp:
'link_settings': {
'libraries': [
'-lexample',
],
},
Compile example.cpp into a library (example.dylib) and use dlopen to load it explicitly. This puts a tremendous burden on your users and is very hard to debug. Do not do this.

How to fix EXCEPTION_ACCESS_VIOLATION when loading c++ dll file?

The Problem
I am writing a simple Hello World program using the Java Native Interface (JNI). When I call System.loadLibrary("libsimple_JNI") to load the dll file containing the C++ hello world function, an EXCEPTION_ACCESS_VIOLATION (0xc0000005) occurs (I am compiling in Netbeans with 64 bit cygwin).
I have tried statically loading the library as well as loading it within the Java helper function which calls the C++ hello world function. I added the path to the cygwin1.dll in the system environment variables which resolved earlier problems with Java not being able to find the dependencies for my dll file.
Here is the exact error message
A fatal error has been detected by the Java Runtime Environment:
EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x0000000180128dd7, pid=19116, tid=0x0000000000003ea8
JRE version: Java(TM) SE Runtime Environment (8.0_131-b11) (build 1.8.0_131-b11)
Java VM: Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode windows-amd64 compressed oops)
Problematic frame:
C [cygwin1.dll+0xe8dd7]
Failed to write core dump. Minidumps are not enabled by default on client versions of Windows
An error report file with more information is saved as:
C:\Users\Kogondo Admin\Desktop\JavaApplication7\hs_err_pid19116.log
Here is the Java Code
public static void main(String[] args) {
thingy my_thingy = new thingy();
my_thingy.hi();
}
public class thingy {
public thingy() {}
public void hi(){
System.loadLibrary("libsimple_JNI");
hello();
}
private native void hello();
}
Here is the C++ code
void JNICALL Java_javaapplication7_thingy_hello (JNIEnv * env, jobject object){
printf("hi from C");
}
When running properly, I expect the code to print "hi from C" to the output window in Netbeans.
Final Solution
7 and half hours I finally managed to find a solution. I followed van dench's advice to generate my dll file using the minGW compiler in Visual Studios instead of the cygwin compiler in Netbeans.
Here is a link to the video and the paper that I found which walk through how to do this. Note that the video is in Portuguese. That being said, the paper which it follows is in English and the video walkthrough shows step by step what commands and configurations you need to do.
I am willing to further elaborate on my solution if others would find that useful.
Good Luck!

How to add an project about idl on redhawk

When I add an idl project in redhawk following the steps below:
$ ./reconf
$ ./configure
$ sudo make
$ sudo make install
I can find the newly added idl file in the REDHAWK Target SDR directory.
REDHAWK Target SDR
When testing whether the idl file can be called, I have added the header file and have already called the function interface, but at compile time, the error is: undefined reference to 'xxxxxxxx'
eg:
#include <redhawk/XH_IDL_TEST/xh_idl_test.h>
class data_t_test_base : public Component, protected ThreadedComponent
{
public:
data_t_test_base(const char *uuid, const char *label);
~data_t_test_base();
void start() throw (CF::Resource::StartError, CORBA::SystemException);
void stop() throw (CF::Resource::StopError, CORBA::SystemException);
void releaseObject() throw (CF::LifeCycle::ReleaseError,CORBA::SystemException);
void loadProperties();
protected:
xh_idl_test::_objref_dataChar *XH;
private:
}
errors:
/home/sca/sca_com/data_t_test/cpp/data_t_test.cpp:21: undefined reference to `xh_idl_test::_objref_dataChar::pushPacket()'
May I ask how to solve this problem?
In REDHAWK, IDL is accessed through ports, either uses (output) or provides (input). Once the IDL project is installed in your system, on component add a Port. Edit the port's interface by clicking on "Browse...". On the "Select and interface" menu, click "Show all interfaces", and then select the interface you want.
In the case of either input or output ports, the appropriate stubs will be generated.

compiling gsoap sample hello service

i downloaded gsoap 2.8.14, configure and install with the following commands:
./configure --disable-ssl --enable-samples --enable-debug
make
make install
im tried to compile gsoap sample "hello". so i took the the wsdl file from the sample and did the following:
wsdl2h -s -o hello.h h.wsdl
soapcpp2 hello.h
i copied the generated files into a new eclipse c++ project and excluded soapClientLib.cpp and soapServerLib.cpp because i was receiving errors like
multiple definition of .....
i then created a helloserver.cpp and here is the content:
#include "soapH.h"
#include "Service.nsmap"
int main()
{
return soap_serve(soap_new);
}
int __ns1__hello(struct soap *soap, char* helloReq, char* &helloResponse)
{
return SOAP_OK;
}
when i build in eclipse, i receives an error:
...soapServer.cpp:77 undefined reference to __ns1__hello(soap*,_ns2_hello*, _ns__helloResponse*)
when i trace to soapServer.cpp, this line is getting the error:
soap->error=__ns1_hello(soap,soap_tmp___ns1_hello.ns2__hello,&ns2__helloResponse);
why am i getting this error? im using the sample hello wsdl from gsoap
Well as you can see from the error message (and the soapServer.cpp code) you are supposed to write a function
int __ns1__hello(struct soap *soap,
_ns2_hello* helloReq,
_ns__helloResponse* helloResponse)
{
return SOAP_OK;
}
not the function you wrote.