How to use third part dll from Haxe - c++

I'm starting project, and I will have to use external dll written on pure C. How to load external dll (NOT.Net libtaty, if it is important) to use it from Neko or Cpp target in Haxe?

I found out the answer on my question. It is trivial, no nedded special settings for the compiller, no special list of dynamic loaded libraries at compile time.
Just load library at runtime using haxe cpp API like below:
static var sum:Int->Int->Int = cpp.Lib.load("test","sum",2);
or haxe neko API according to your target platform:
static var sum:Int->Int->Int = neko.Lib.load("test","sum",2);
This lines load sum function from test library which located in the same directory with executable file.
Read more about it in old haxe documentation.
[1]: http://old.haxe.org/doc/cpp/ffi C Foreign Function Interface

Related

How to Export classes/functions from a static library that was imported in a DLL

For example, I have 3 modules : Module1.cpp/h Module2.cpp/h and Module3.cpp/h, Now I will build it as a static library named as Modules.lib
Then I will create another project that will be built as a DLL. Modules.lib will be imported to the project since some source code have dependencies on Modules.lib. I will build it as Library.dll
Now this Library.dll will be used by a Windows App. How can I use the functions/classes in Modules.lib that was imported in Library.dll? How can I also export the header files(Module1.h, etc). When I add them in the header file of the dll, there is an error. The dll header file cannot find the included header file.
I can't find a specific example for this. Please show me an example. Thank you very much.

Matlab DLL Integration via C++ or VB.NET

I haven't found a solution to my problem, because of limited knowledge about dll Libraries.
I am using Matlab (R2017a) to integrate communication to a device through a dll, which is provided by the manufacturer (files - Xemo-DLL (64Bit) mit Header-Dateien (2.40)).
As far as I understand there is precompiled .dll in C and a header File in C++, beside a VB and VB.NET wrapper.
Question is: What is the best way to integrate the dll into Matlab?
A) I tried loadlibrary(XemoDll). It throws a lot of errors, iostream wasn't found. So I added extern "C" {} to the whole file, which created new errors at every function definition. Where do I have to insert it?
B) I tried to add NET.addAssembly(path\XemoDll.vb) of the VB.NET library. There is a Module with all wrapper functions. Matlabs gives the error "assembly manifest missing" Source: mscorlib.
I found a Tutorial on loading Dlls with the LoadLibrary() command from windows.h, and got it working.
I use a a DllInterface_mex function that gets the function name and calls the approriate function in the Dll.

How to prevent static library from getting duplicated?

I have a 3rd party static library L (pjsip.lib). This static library depends on many static variables and functions.
I created a DLL D using L.
I created an application A that uses both D and L.
Here I have a problem. It looks like there multiple instances of static functions and variables that L uses, and they have different values depending on whether it is accessed by directly A or through L. I am not able to build L as DLL. Is there any way that both A and D can use the same code segment?
I am using Visual C++ (2015).
Place the static lib in its own dll and export all the symbols you need (use a DEF) file it necessary. Now both your exe and other dll can link to it dynamically.

How to load and use native c code in a lein project?

ProblemI'm unable to load and call methods in a compiled c class into a leiningen project. My basic approach is to load a Java class, JavaWrapper.java, that uses JNI to call some native methods in the native code, wrapper.o and then call the methods through this java wrapper class.
I imagine there are classLoader issues with loading a java class which loads the native code from a clojure project, but given that I can't seem to directly get clojure code to find the wrapper.o on the library path, I'm not sure how to handle this.
lein project file
(defproject lein-native-test "0.1.0-SNAPSHOT"
...
:java-source-paths ["java-path"]
:jvm-opts ["-Djava.library.path=.:./native:/absolute/path/to/native"] ;;not sure what format it wants
)
clojure file with main method
I've tried it slightly modified with four approaches, all included in code below along with respective error in comments.
(ns lein-native-test.core
(:import (com.test JavaWrapper)))
(def -main []
;;four things I've tried and their errors
(clojure.lang.RT.load "/abs/path/to/wrapper.o") ;;could not find file /abs/path/wrapper.o_init.class or wrapper.o.clj
(clojure.lang.RT.loadLibrary "wrapper.o") ;;UnsatisfiedLinkError no wrapper.o in java library path
(JavaWrapper/load "/abs/path/to/wrapper.o") ;;UnsatisfiedLinkError com.test.JavaWrapper.setup()
(assembly-load "/abs/path/to/wrapper.o") ;;unable to resolvesymbol: assembly-load
)
Java code with native methods that uses JNI, JavaWrapper.java
public class JavaWrapper{
public native void setup();
public static void load(String lib){ System.load(lib);}
}
Before trying to get this to work with clojure and lein I did successfully load and use the native methods in wrapper.o via JavaWrapper and JNI.
Possibly related: I'm also unable to load wrapper.o in JavaWrapper.java via
System.loadLibrary("wrapper.o");
I have to use
System.load("/absolute/path/to/wrapper.o");
Versions of tools
clojure version: 1.5.1
lein version: 2.3.4
jdk: 1.7
os: debian7
A better understanding of ClassLoaders or especially a working simple example would be very useful, thanks.
The problem was due to an naming error in my method in the C header and source files per the jni standard. The correct way to use jni with clojure is to create a Java wrapper class as I have done and load the dynamic library with the method
clojure.lang.RT.loadLibrarySince I've had trouble finding good examples for this I've made a demo on github
Errors
1) (clojure.lang.RT.load "/abs/path/to/wrapper.o") ;;could not find file /abs/path/wrapper.o_init.class or wrapper.o.clj
This load method is not meant to be used for native code, its expecting a java class or a clj file
2) (clojure.lang.RT.loadLibrary "wrapper.o") ;;UnsatisfiedLinkError no wrapper.o in java library path
Clojure is unable to find the library at link time, hence UnsatisfiedLinkError --- this is due to a naming error
first the library should be compiled as a dynamic shared library, i.e. for the gcc compiler use the -shared flag (I actually did but didn't name the output file with the normal .so extension)
java and thus clojure expect the native library to be named in a very specific way: libwrapper.so (or .jnilib for mac or .dll for windows but always with "lib" prefix)
3) (JavaWrapper/load "/abs/path/to/wrapper.o") ;;UnsatisfiedLinkError com.test.JavaWrapper.setup()
This time the error is on a method within JavaWrapper in the file or library, thus you know that at least it found the file. An UnsatisfiedLinkError that specifies a specific method in the Java class, like this one, should always be due to a naming error between what is declared a native method in the Java file and what is actually present in the c source or header file.
Note the namespace "com.test"
When declaring a jni method in c the method name must follow a specific format,
from http://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/design.html
"Dynamic linkers resolve entries based on their names. A native method name is concatenated from the following components:"
the prefix Java_
a mangled fully-qualified class name
an underscore (_) separator
a mangled method name
for overloaded native methods, two underscores (__) followed by the mangled argument signature
In this case the full c source method signature would be
void Java_com_test_setup(JNIEnv *env, jobject obj)
4) (assembly-load "/abs/path/to/wrapper.o") ;;unable to resolvesymbol: assembly-load
This method too is not meant to load native code

utilizing a static library to create an import library

I am interested in using my static lib to create a dll (implicitly linking). which means I need to (in vs2008)
create a dll project that should generate the following:
header file (which have export function declarations. These are simple wrappers to actual functions in the static lib using __declspec(dllexport) which are in the .cpp )
import lib which will be made as a result of creating the dll
the actual dll which is created.
I have made a test program that will utilize the above dll(including the import lib/header files) to test it.
in this I have included all the three items. now the exe compiles/links without issue.
however in the main.cpp when i call the exported functions (with the associated __declspec(dllimport) call it never seems to execute. I am uncertain why this is?
Its almost like the even though the exe can see the exported function in in the dll...the dll cannot call on the code that is in the static lib?
i just cannot answer why my exe can't see the code in the static lib? do i need an archiver/librarian for vs2008 to include all those obj files as part of the import lib?
I am at a loss and am not sure how to test this?
other than just making my static lib directly into a dll. I wanted to try this method. I know i am missing something...i have read all over the place and i am just stuck. There were some threads here that had some people posting something similar but i can't seem to get it. please be as detailed as possible as I am new to this. thanks again.
update 1:
ok so currently i added the extern line to the function prototype and now it sees the exported function from the dll. however, now the only issue left is that:
i can't invoke the function that this exported function (aka wrapper) is trying to call. which happens to be in the static library. how should my exe get visibility to that static library function. I know it can be done because I think there was one other person on this board who was able to make this work.
update 2: my setup is exactly like this questioner...
How to force inclusion of an object file in a static library when linking into executable?
but i am not using explicit linking. i am using implicit linking. my real issue is how to call a static lib function in my dll wrapper which is exported to the exe?
If the app and DLLs are MFC app/dlls, then make sure that the application and all dlls are either "Debug" versions or "release" versions and not a mix.