how to fix unsatisfiedLinkError when generating dll for JNI in windows platform using g++ - windows

I'm new to native programming. I've been trying to fix the unsatisfiedLinkError past 8-9 hours but got no result. After a lot of googling and stackoverflowing, I got sick of fixing it, I'm posting my problem here. Somebody please please help me.
I'm using g++ compiler in windows 32bit environment.
Here are the files that I've created:
Demo.java
class Demo
{
// Declaration of the native method
public native int methodOfC(int arg1);
/*The native keyword tells the compiler that the implementation of this method is in a native language*/
/*Loading the library containing the implementation of the native method*/
static
{
System.out.println("Control is in Java.......going to call a C program......\n");
System.loadLibrary("try");
System.out.println("Congr8s no prob in CallApi.....\n");
}
public static void main(String[] args)
{
//invoking the native method
int sendToC,getFrmC;
if(args.length!=0) sendToC=Integer.parseInt(args[0]);
else sendToC=999;
Demo ob1=new Demo();
getFrmC=ob1.methodOfC(sendToC);
System.out.println("This is in Java......\n Got "+ getFrmC +" in return from C.");
}//end main
}//end Demo
Demo.h
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class Demo */
#ifndef _Included_Demo
#define _Included_Demo
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: Demo
* Method: methodOfC
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_Demo_methodOfC
(JNIEnv *, jobject, jint);
#ifdef __cplusplus
}
#endif
#endif
DemoImp.c
#include <jni.h>
#include "Demo.h"
#include <stdio.h>
//definition of methodOfC()
JNIEXPORT int JNICALL Java_Demo_methodOfC(JNIEnv* exeenv, jobject javaobj, int getFrmJava)
{
printf("This is in the C program\n Got %d from java",getFrmJava);
printf("\n.......Exiting frm C\n");
return getFrmJava+1;
}
Here is how I compiled and run my prog.: screenshot here
C:\native>javac Demo.java
C:\native>javah -jni Demo
C:\native>g++ -c -l"C:\Java\jdk1.6.0_26\include" -l"C:\Java\jdk1.6.0_26\include\win32" DemoImp.c
C:\native>g++ -shared DemoImp.o -o try.dll
C:\native>java Demo 1234
Control is in Java.......going to call a C program......
Congr8s no prob in CallApi.....
Exception in thread "main" java.lang.UnsatisfiedLinkError: Demo.methodOfC(I)I
at Demo.methodOfC(Native Method)
at Demo.main(Demo.java:23)
C:\native>
I've already added "C:\native" in my system path variable.
I've uploaded all my files in mediafire. Here's the link native.zip
If possible please tell me how can I make 64bit version of dll. Thanks in advance.

You have missed out the package name in the DemoImp.c file.
The naming convention for C function is Java_{package_and_classname}_{function_name}(JNI arguments). The dot in package name shall be replaced by underscore.

Related

Error in Unreal after Xcode Update : PCH file uses an older PCH format that is no longer supported

So I just recently updated Xcode(not too sure if this is causing the problem) to version 10.2. When I compile my code in Unreal Engine 4 version 4.21.2 it gives me this error. "PCH file uses an older PCH format that is no longer supported" Anything I can do to fix it?
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Engine/TriggerVolume.h"
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "OpenDoor.generated.h"
UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UOpenDoor : public UActorComponent
{
GENERATED_BODY()
public:
// Sets default values for this component's properties
UOpenDoor();
protected:
// Called when the game starts
virtual void BeginPlay() override;
public:
void OpenDoor();
void CloseDoor();
// Called every frame
virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
private:
UPROPERTY(EditAnywhere)
float OpenAngle = -90.0f;
UPROPERTY(EditAnywhere)
ATriggerVolume* PressurePlate;
UPROPERTY(EditAnywhere)
float DoorCloseDelay = 1.f;
float LastDoorOpenTime;
float GetTotalMassOfActorsOnPlate();
//UPROPERTY(EditAnywhere)
//AActor* ActorThatOpens; //Pawn inherits from AActor
AActor* Owner; //The owning door
void FindPressurePlate();`enter code here`
};
I expect the game to compile to my newer version, actual result game does not compile.

How to call a method added to the windows native module of Appelerator Platform

We write to ask how to correctly call a method of Titanium Appelerator native module.
Our development environment is as the following:
Titanium SDK :5.2.0
Appcelerator CLI: 5.2.0
TabletPC:Windows10pro
Our module has been developed with the following procedures;
(1) we created a module referring to the document of https://github.com/appcelerator/titanium_mobile_windows#module-development
An additional method aaa() was added to the module.
cd MY_WORKSPACE
ti create -p windows -t module //create a module project
cd MY_MODULE_NAME/windows
ti build -p windows -T ws-local //build my module project
(2) A windows’ application was created to call the above module, and there was no error while we call the module from the application. However, while we call the method aaa(), Titanium error message as the following occurred;
“aaa() method does not exist”.
In the Titanium windows platform for windows10, the dll for module was created correctly. Then, we are just wondering which parts of our source are not correct;
(1) The definition of a method aaa()is not correct.
(2) The source code to call the native module is not correct.
(3) The source code to call the aaa() method is not correct.
We attached our source code as the following and thank you in advance for your comment and advice.
The code is as below.
windows/src/JpNativeModuleExample.cpp
#include "JpNativeModuleExample.hpp"
#include "Titanium/detail/TiImpl.hpp"
namespace Jp
{
NativeModuleExample::NativeModuleExample(const JSContext& js_context) TITANIUM_NOEXCEPT
: JSExportObject(js_context)
{
TITANIUM_LOG_DEBUG("NativeModuleExample::ctor Initialize");
}
void NativeModuleExample::postInitialize(JSObject& js_object)
{
}
void NativeModuleExample::postCallAsConstructor(const JSContext& js_context, const std::vector<JSValue>& arguments)
{
}
//add new method
void NativeModuleExample::aaa() TITANIUM_NOEXCEPT
{
}
void NativeModuleExample::JSExportInitialize()
{
JSExport<NativeModuleExample>::SetClassVersion(1);
JSExport<NativeModuleExample>::SetParent(JSExport<JSExportObject>::Class());
}
}
windows/include/JpNativeModuleExample.hpp
#ifndef _JPNATIVEMODULEEXAMPLE_HPP_
#define _JPNATIVEMODULEEXAMPLE_HPP_
#include "JpNativeModuleExample_EXPORT.h"
#include "Titanium/detail/TiBase.hpp"
#include "Titanium/Module.hpp"
namespace Jp
{
using namespace HAL;
class JPNATIVEMODULEEXAMPLE_EXPORT NativeModuleExample : public JSExportObject, public JSExport<NativeModuleExample>
{
public:
NativeModuleExample(const JSContext&) TITANIUM_NOEXCEPT;
void aaa() TITANIUM_NOEXCEPT;
virtual void postInitialize(JSObject& js_object) override;
virtual void postCallAsConstructor(const JSContext& js_context, const std::vector<JSValue>& arguments) override;
virtual void aaa(); //add new method
virtual ~NativeModuleExample() = default;
NativeModuleExample(const NativeModuleExample&) = default;
NativeModuleExample& operator=(const NativeModuleExample&) = default;
#ifdef TITANIUM_MOVE_CTOR_AND_ASSIGN_DEFAULT_ENABLE
NativeModuleExample(NativeModuleExample&&) = default;
NativeModuleExample& operator=(NativeModuleExample&&) = default;
#endif
static void JSExportInitialize();
};
}
#endif // _JPNATIVEMODULEEXAMPLE_HPP_
sample code
var nativemoduleexample = require('jp.NativeModuleExample');
Ti.API.info("module is => " + nativemoduleexample); //no problem
var aaa = nativemoduleexample.aaa(); //titanium error is displayed
$.index.open();
In order to enable your function, you need to register it using TITANIUM_ADD_FUNCTION at JSExportInitialize(). You might want to check out working examples under TitaniumKit such as Ti.UI.Button and Ti.UI.Window.
void NativeModuleExample::JSExportInitialize()
{
JSExport<NativeModuleExample>::SetClassVersion(1);
JSExport<NativeModuleExample>::SetParent(JSExport<JSExportObject>::Class());
TITANIUM_ADD_FUNCTION(NativeModuleExample, aaa);
}
And then use TITANIUM_FUNCION to define function like this...
TITANIUM_FUNCTION(NativeModuleExample, aaa)
{
aaa();
return get_context().CreateUndefined()
}

How to wrap a c++ class with an opencv object with c++/cli

I would like to wrap a complex c++ class which used opencv object with c++/cli for using in c# and vb. I'm using Visual Studio 2015. I'm using the x64 versions of of the OpenCV library and compiling also to x64.
Following is the header.h file of a small test class, just as an example:
#pragma once
#include <vector>
#include "opencv2/opencv.hpp"
using namespace std;
class compC
{
public:
compC(int * pInt, int arrSize);
int sumArray();
private:
vector<int> vec;
cv::Mat img;
};
This is going to be compiled as a win32-application to a .lib file without any problems.
The example wrapper class (wrapper.h) would look like:
#pragma once
#include "../ConsoleApplication1/header.h"
#include "../ConsoleApplication1/body.cpp"
using namespace System;
namespace WrapperLibrary {
public ref class WrapperClass
{
public:
WrapperClass(int * pInt, int arraySize);
int getSum();
private:
compC * pcC;
int sum;
};
}
The second class would be compiled as a dll with /clr (as c++/cli project).
First I got an error that opencv.hpp could not be find and had to add the include directory of opencv in to the wrapper project. But I cannot compile it and get a lot of errors, mainly pointing to linking problems "unresolved external problems ..." in connection with the opencv-mat object.
If I remove the lines
#include "opencv2/opencv.hpp"
and
cv::Mat img;
in the test class (header.h) all will be fine.
What did I made wrong? How can I wrap those c++ class with c++/cli?

Map File contains obsalutely identical addresses for different functions

I am trying to get stack trace in release (optimized) build without pdb-s.
Currently I am trying to retrieve function addresses during my sample program execution using StackWalk64 function and then map generated addresses to the actual function names using map file generated during linking stage. Please note that optimization is turned on.
I see absolutely identical addresses for two different functions in the generated map file.
0001:00000000 ?static_function_call#MyTest##SAXXZ 00401000 f i main.obj
0001:00000000 ?call_1#MyTest##QAEXXZ 00401000 f i main.obj
What could be the reason of such a thing, can it be due to optimization ? Then how can this functions be distinguished ?
EDIT:
Here is the function bodies
#include <iostream>
#include <windows.h>
#include <dbghelp.h>
class __declspec(dllexport) MyTest
{
public:
static void static_function_call()
{
}
void call_1()
{
static_function_call();
};
};
int main( void )
{
try
{
MyTest obj;
obj.call_1();
}
catch( ... )
{
}
return ( 0 );
}
Thank you,
-Grigor

Error "C3145" and "C2061" in C++ Visual Studio

EDIT: What is C++/CLI? I am programming in Visual studio, and as far as I know using C++... Also, The first error was solved by Peter's comment, but I am still stuck on the second.
I am brand new to the world of C++, and have previously done all my work in Java. I am unfamiliar with the use of pointers and garbage collection (though I believe I understand the concept) and I believe that may be the source of my problems. I am getting the following error messages:
1>Runner.cpp(6): error C3145: 'formOutOfTime' : global or static variable may not have managed type 'System::Windows::Forms::Form ^'
1> may not declare a global or static variable, or a member of a native type that refers to objects in the gc heap
1>Runner.cpp(22): error C2061: syntax error : identifier 'FormOutOfTime'
My code is like this:
PurpleHealth.cpp (This is the file I believe the system calls to start it all off):
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"
#include "Runner.h"
using namespace PurpleHealth;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
//Application::Run(gcnew FormOutOfTime());
Runner* runner = new Runner();
//delete runner;
return 0;
}
Runner.h (this is the header file I want to run all my main code, and launch the forms. I also struggle with the purpose behind the header files)
#include "stdafx.h"
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"
class Runner
{
public:
Runner();
~Runner();
// functions
private:
void Go();
// member variables
};
And Finally Runner.cpp:
#include "stdafx.h"
#include "Runner.h"
#include "FormOutOfTime.h"
#include "FormParentalOverride.h"
//Variable Dclaration
System::Windows::Forms::Form^ formOutOfTime;//Error Here***************************
Runner::Runner()
{
// Do stuff if you need to
this->Go();
}
Runner::~Runner()
{
// Clear memory if you need to
}
void Runner::Go()
{
formOutOfTime = gcnew FormOutOfTime();//Error Here***************************
formOutOfTime->ShowDialog();
}
Please help me solve these messages, and even critique on form is appreciated. Thanks.
managed pointers cannot be declared at static or global scope. They can only be declared at function scope. Move the declaration of formOutOfTime from the top of the runner.cpp file to within the Go method

Resources