How can you access the ADL-SDK from clojure? - clojure

I am new to JNI and swig and I'm trying to access AMD's ADL-SDK v6 from clojure but am running into some errors.
;; Clojure 1.5.1
=> (com.vnetpublishing.swig.adl.AdapterInfo.)
UnsatisfiedLinkError com.vnetpublishing.swig.adl.adlsdkJNI.new_AdapterInfo()J com.vnetpublishing.swig.adl.adlsdkJNI.new_AdapterInfo (adlsdkJNI.java:-2)
=> (System/loadLibrary "libatiadlxx")
UnsatisfiedLinkError no libatiadlxx in java.library.path java.lang.ClassLoader.loadLibrary (ClassLoader.java:1886)
The swig template is as follows
/* File jadl-sdk.i */
%module adlsdk
%{
#include "adl_sdk.h"
%}
%include "adl_defines.h"
%include "adl_structures.h"
%include "adl_sdk.h"
The classes were generated with the following command
swig -java -package com.vnetpublishing.swig.adl -v -DLINUX -I${JAVA_HOME}/include -I${JADL_PATH}/ADL_SDK/include -outdir ${PACKAGE_PATH} jadl-sdk.i
Does anyone know how to get this to function?
-edit-
I have been able to get this to partially function using
(clojure.lang.RT/loadLibrary "atiadlxx")
(clojure.lang.RT/loadLibrary "jadlsdk")
The only problem is the jadlsdk library isn't on the java.library.path , it is inside a jar, and I can't get it to load from leiningen.

I have solved the problem. The solution was to run the loadLibrary call as part of the static constructor and to install the swig generated wrapper library as a system library (On linux under /usr/lib).
The complete code is available on github at https://github.com/rritoch/WarpCTL/tree/master/extra/JADL-SDK
Once the wrapper library has been installed, and the java wrapper code has been compiled to a jar it can be loaded in the project.clj under :resource-paths
The clojure side code can be accessed # https://github.com/rritoch/WarpCTL

Related

How to load this Clojure project in my cider REPL? Why I am receiving the message "The clojure executable isn’t on your ‘exec-path’" (NixOS)?

Recently, I started learning Clojure. I have been using Emacs, and cider (REPL). Also, I have NixOS as OS.
I have been able to successfully program in Clojure using this environment. Usually, I create the files, then I go to the directory where the .clj are, I execute cider-jack-in, a REPL is created, and interactive programming magic happens. Thus, I am able to load the file, load new versions of old functions, etc.
It also works if I just start Emacs and execute `cider-jack-in. The mini-buffer asks:
Are you sure you want to run `cider-jack-in' without a Clojure project? (y or n)
After answering y, everything works fine.
While reading the book Clojure for the Brave and True, I have also successfully used:
$ lein run
For learning purposes, I decided to explore some open source projects. Thus, I did a git clone of this open-source project.
Being on the folder, I decided to reproduce the steps that have worked so far. However, it does not work. After executing cider-jack-in, the mini-buffer echoes the following:
The clojure executable isn’t on your ‘exec-path
Also, if I try with lein I get:
[pedro#system:~/projects/http-cron]$ lein run
No :main namespace specified in project.clj.
Obs.: If I type clojure on the shell, this is what I get:
[pedro#system:~]$ clojure
The program 'clojure' is not in your PATH. You can make it available in an
ephemeral shell by typing:
nix-shell -p clojure
Why is this happening?
How can I solve this (considering that I am using NixOS)?
Going the imperative route, depending on whether you use NixOS with channels (default) or flakes, you can permanently install clojure into your environment with:
channels: nix-env -f '<nixpkgs>' -iA clojure
flakes: nix profile install nixpkgs#clojure
The preferred (declarative) way would however be adding clojure to environment.systemPackages in /etc/nixos/configuration.nix and running sudo nixos-rebuild switch.

GHCi could not load .dll lib (C++ lib) on Windows

I've installed a C++ lib successfully via vcpgk, named: lmdb:x64-windows
I also installed lmdb binding package for Haskell by Cabal install
And when trying to test the lmdb package:
module Persistence where
import Database.LMDB.Raw
GHCi compiled and loaded it:
[1 of 1] Compiling Persistence ( Persistence.hs, interpreted )
Ok, modules loaded: Persistence.
But it throws error when I try to call any functions in lmdb Raw lib:
*Persistence> lmdb_version
ghc.exe: addDLL: lmdb.dll (Win32 error 126): The specified module could not
be found.
ghc.exe: Could not load `lmdb.dll'. Reason: addDLL: could not load DLL
ghc.exe: C:\Users\thanhdo\AppData\Roaming\cabal\x86_64-windows-ghc-
8.0.2\lmdb-0.2.5-1uQhV16mebP51rtMgitOcY\HSlmdb-0.2.5-
1uQhV16mebP51rtMgitOcY.o: unknown symbol `mdb_dbi_close'
ghc.exe: unable to load package `lmdb-0.2.5'
I was searching around and was trying several approaches but still can't resolve this error. I'm using Windows 7 64bit, Haskell platform 8.0.2. Thanks in advance.
As per discussion in comment section, I was trying to tell GHCi the location of lmdb's dll. It works after I modify %path% env.
In my case, the path was: vcpkg\installed\x64-windows\bin

leiningen - clojurescript equivalent of lein run?

In clojure I can run:
lein run -m my.namespace # run the -main function of a namespace
and this will run my code from the command line.
Is there an equivalent for Clojurescipt to run the generated code in node.js? (In leiningen)
(I have read the doco for starting the Clojurescript REPL, for the running on node.js and the reply integrated into my application. I was just looking for a one-line command line solution.)
Depending on what your goal is, you might find it useful to use 'cljsbuild test'. You can specify a test context on your project.clj that uses node.js/v8/phantomjs.
Example:
:cljsbuild {
:test-commands {
"v8" ["v8"
"target/generated-sources/private/js/client.js"
"tests/v8-test-runner.js"]}}
v8-test-runner.js:
path.to.your.entrypoint.start()
You can also leave 'lein cljsbuild auto' running and start the javascript application directly:
v8 target/generated-sources/private/js/client.js tests/v8-test-wrapper.js
That's probably the best option if you are building a node.js application using clojurescript. You don't need to worry about classpaths in javascript (which is the major thing that lein run provides), you can just run your application directly from the assembled javascript file.

(Swig to python) import error:dynamic module does not define init function

I am trying to port my c++ code to python by swig.
When I finish building the py, pyd, cxx and lib files, under Python (command line), I key in "module Dnld", it shows-> import error:dynamic module does not define init function.
The following is my code,
Further: Add my build step to avoid misunderstanding, thank you Mark Tolonen
File->New->Project->Windows Console Application-> Select DLL and empty project(no unicode)
Add my SerialComm folder to the project(include DownloaderEngine.h Serial.h PortEnumerator.h,etc).
Configuration properties->c/c++->Additional include directories->C:\Python27\include
Configuration properties->Linker->General->Output File->$(OutDir)\Dnld.pyd
Configuration properties->Linker->Input->Additional include directories->C:\Python27 \libs\python27.lib and .\SerialComm\setupapi.lib
Add Dnld.i , do custom build
Dnld.i property page->Command line->swig -c++ -python $(InputPath)
Dnld.i property page->Output->$(InputName)_warp.cpp
build, create Dnld_wrap.cxx, Dnld.py
Add Dnld_wrap.cxx in my project, rebuild all, create Dnld.pyd, that's it
(Enviroment:XP SP3 with VC2008)
//DownloaderEngine.h
class __declspec(dllexport) CDownloaderEngine
{
public:
CDownloaderEngine();
virtual ~CDownloaderEngine();
signed char OpenPort(signed char _ucPort, unsigned long _ulBaudRate, unsigned char _ucParity,
unsigned char _ucStopBits,unsigned char _ucData);
....
};
//DownloaderEngine.cpp
CDownloaderEngine::CDownloaderEngine()
{
....
}
CDownloaderEngine::~CDownloaderEngine()
{
....
}
//DownloaderEngine.i
%module Dnld
%include <windows.i>
%include <std_vector.i>
%include <std_map.i>
%{
#define SWIG_FILE_WITH_INIT
#include ".\SerialComm\DownloaderEngine.h"
%}
/* Parse the header file to generate wrappers */
%include ".\SerialComm\DownloaderEngine.h"
Not really enough information, because the problem is likely in how you are building it. for example, with the files you've specified, building from a VS2008 command prompt should be something like:
swig -python -c++ DownloaderEngine.i
cl /LD /W4 /Fe_Dnld.pyd /Ic:\Python27\include downloaderEngine_wrap.cxx -link /LIBPATH:c:\Python27\libs DownloaderEngine.lib
Edit: Your build steps look about right, but one thing is the .pyd file is expected to be named _Dnld.pyd (note the underscore).
The generated Dnld.py calls import _Dnld (the .pyd), so you will import Dnld (the .py) in your Python script.
Example:
>>> import Dnld
>>> engine = Dnld.CDownloaderEngine()
>>> result = engine.OpenPort(...)
This is the error I get if I rename the .pyd without an underscore:
>>> import Dnld
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: dynamic module does not define init function (initDnld)
So I'm sure this will fix your issue. 我很高興幫助你!
For the record, here another possible cause of the error message
ImportError: dynamic module does not define init function (init<mylibrary>):
Running Python2 while Swig was set up for Python3, or vice versa.
This one took me a while to figure out. From the python.org mailing lists here, it seems the problem is that python expects module Foo to provide a function initFoo. The question then, is why doesn't Dnld provide initDnld. Since swig should handle most of that, it is probably because swig doesn't expect the finished library to be called Dnld (if it expects dnld or D_nld or anything else, it will fail, but renaming the file fixes it.) Note that this applies to any C extension for python, including those generated by pyrex/cython and boost.

Which clojure jar contains clojure.contrib.io.Streams?

I am trying to compile some Clojure code to Java using the Clojure 1.2 compiler. I am including the Clojure and Clojure-contrib 1.2 jars in the classpath. When I do the compile it fails and I get the folowing error:
Exception in thread "main" java.lang.ClassNotFoundException: clojure.contrib.io.Streams (io.clj:121)
: Has anyone seen this before?
Update:
Here is the line I use to compile my clj files:
java -cp "..\lib\h2.jar;..\lib\vaadin-6.4.4.jar;..\lib\clojure.jar;..\lib\clojure-contrib.jar;..\lib\wlfullclient-10.3.0.0.jar;..\..\..\..\..\jre1.6windows32bit\lib\rt.jar;." -Dclojure.compile.path=cljclasses clojure.lang.Compile oe.main.oe-main
clojure.contrib.io.Streams is a protocol defined in clojure.contrib.io
To help you more you have to share more details about how and what you are trying to compile.
use (compile 'oe.main.oe-main) from a clojure REPL using the same class path, and this works