How to build a shared library .so with Cygwin on Windows

IDE from Here – Cygwin

To make a test for shared library .so (shared library on the Linux) that is built on Windows.

Step 1: Open Cygwin Terminal

Create a folder cpp in the ~ home folder (The home folder on the windows will be “C:\cygwin64\home\<username>” cygwin
Step 2: Create a samples in the cpp folder:
files

files content

Step 3: Build & Make files – shared library.

To get .so shared library, we run some compile commands below:

build

Step 4: To test .so, I had built several times, but it always errors of not found the function in the .so file. The problem is the path to find .so file:

cannot find -lmean

collect2: error: ld returned 1 exit status

I had found the article from serverfault, it’s in serverfault
solution
It took me 4 hours to install and run a test with shared library .so with Cygwin on the Windows. It starts to create a security libraries for mobile applications and server side.

shared library .so with cygwin – can’t find shared library

collect2: error: ld returned 1 exit status

From serverfault (21)

While you can set LD_LIBRARY_PATH to let the dynamic linker know where to look, there are better options. You can put your shared library in one of the standard places, see /etc/ld.so.conf (on Linux) and /usr/bin/crle (on Solaris) for the list of these places

You can pass -R <path> to the linker when building your binary, which will add <path> to the list of directories scanned for your shared library. Here’s an example. First, showing the problem:

libtest.h:

void hello_world(void);

libtest.c:

#include <stdio.h>
void hello_world(void) {
  printf("Hello world, I'm a library!\n");
}

hello.c:

#include "libtest.h"
int main(int argc, char **argv) {
  hello_world();
}

Makefile (tabs must be used):

all: hello
hello: libtest.so.0
%.o: %.c
        $(CC) $(CFLAGS) -fPIC -c -o $@ $<
libtest.so.0.0.1: libtest.o
        $(CC) -shared -Wl,-soname,libtest.so.0 -o libtest.so.0.0.1 libtest.o
libtest.so.0: libtest.so.0.0.1
        ln -s $< $@
clean:
        rm -f hello libtest.o hello.o libtest.so.0.0.1 libtest.so.0

Let’s run it:

$ make
cc  -fPIC -c -o libtest.o libtest.c
cc -shared -Wl,-soname,libtest.so.0 -o libtest.so.0.0.1 libtest.o
ln -s libtest.so.0.0.1 libtest.so.0
cc     hello.c libtest.so.0   -o hello
$ ./hello 
./hello: error while loading shared libraries: libtest.so.0: cannot open shared object file: No such file or directory

How to fix it? Add -R <path> to the linker flags (here, by setting LDFLAGS).

$ make clean
(...)
$ make LDFLAGS="-Wl,-R -Wl,/home/maciej/src/tmp"
(...)
cc   -Wl,-R -Wl,/home/maciej/src/tmp  hello.c libtest.so.0   -o hello
$ ./hello 
Hello world, I'm a library!

Looking at the binary, you can see that it needs libtest.so.0:

$ objdump -p hello | grep NEEDED
  NEEDED               libtest.so.0
  NEEDED               libc.so.6

The binary will look for its libraries, apart from the standard places, in the specified directory:

$ objdump -p hello | grep RPATH
  RPATH                /home/maciej/src/tmp

If you want the binary to look in the current directory, you can set the RPATH to $ORIGIN. This is a bit tricky, because you need to make sure that the dollar sign is not interpreted by make. Here’s one way to do it:

$ make CFLAGS="-fPIC" LDFLAGS="-Wl,-rpath '-Wl,\$\$ORIGIN'"
$ objdump -p hello | grep RPATH
  RPATH                $ORIGIN
$ ./hello 
Hello world, I'm a library!

Install gcc with cygwin on windows

  1. Install Cygwin from cygwin, the default folder will be looked like:

cygwin folder structure

But there will be lack of some package, includes gcc-g++ compiler for linux. To install these packages we have step 2

2. From cmd (Command DOS), we run the command below:

C:\cygwin64>setup-x86_64.exe -q -P wget -P gcc-g++ -P make -P diffutils -P libmpfr-devel -P libgmp-devel -P libmpc-devel

The command will install some packages: wget, gcc-g__, diffutils, libmpfr, libgmp-devel, libmpc-devel

3. Get the lastest gcc from Lastest gcc

$ wget ftp://gnu.mirror.iweb.com/gcc/gcc-5.2.0/gcc-5.2.0.tar.gz
$ tar xf gcc-5.2.0.tar.gz

It will create a subdirectory named gcc-5.2.0. Next, we’ll configure our GCC build. As the GCC documentation recommends, it’s best to configure and build GCC in another directory outside gcc-5.2.0, so that’s what we’ll do.

$ mkdir build-gcc
$ cd build-gcc
$ ../gcc-5.2.0/configure --program-suffix=-5.2.0 --enable-languages=c,c++ --disable-bootstrap --disable-shared

Next, we’ll actually build the new GCC compiler suite, including C, C++ and the standard C++ library.

$ make -j4

The -j4 option lets the build process spawn up to four child processes in parallel. If your machine’s CPU has at least four hardware threads, this option makes the build process run significantly faster.

Once that’s finished, install the new compiler:

$ make install
$ cd ..

Call c function from android using jni

When you have been developing Android applications for a while, there comes a moment that you want to leverage the power of some lib in native c in one of your applications. For doing ocr, for example.
This tutorial describes how to call native c-code in an Android application using the Android Native Development Kit.

JNI

The first thing to learn is how to call native c-code from regular Java through JNI, the Java Native Interface. Most Java developers are aware of the native keyword, but most never have to use it in practice. Therefore basic JNI usage will be discussed first.

Your Java source code has to declare one or more methods with the native keyword to indicate that they are implemented through native code:

native String getJniString();

These native methods obviously dont have a Java implementation. You must provide a native shared library that contains the implementation of these methods. This library must be named according to the standard Unix conventions as lib<name>.so and needs to contain the standard JNI entry point. For our method above, the implementation in c could look like:

#include <string.h>
#include <jni.h>

jstring
Java_com_integratingstuff_jni_HelloJni_getJniString(JNIEnv* env, jobject thiz){
    return (*env)->NewStringUTF(env, "Hello from JNI!");
}

in which case our native method would have to reside in the HelloJni class in the com.integratingstuff.jni package. The naming is obvious if you take a closer look.

Finally, your application must explictly load the library(elsewise you will get an UnsatisfiedLinkError). To load it at application startup, add the following code:

static {
        System.loadLibrary("hello-jni");
}

In this case, the name of the shared library would need to be libhello-jni.so. The lib prefix and so suffix should not be included here.

Note that you are not supposed to be following this tutorial in your IDE right now. If you just want to make this jni part work, and dont care about Android for now, you would need to compile the above c-file into the required so file first, dealing with the jni.h dependency yourself. This is trivial with gcc though, especially on a linux machine(on Windows, you probably need to install something extra: I would use Cygwin myself, but you could use some Windows C compiler like MinGW too).

The Android native development kit

If you’re going down the Android path, you need to download the Android Native development kit now. Basically, the android ndk is a c-compiler/builder that ships with all the important c-libs present on the Android OS. It comes with one important command: ndk-build.

To follow this tutorial, you can just create a new Android project in Eclipse. When the project is created, make a new folder in the project root, called “jni”. You copy the above hello-jni.c file into this folder, together with the following Android.mk file:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)

When the ndk-build command is run, it always looks for the Android.mk file and reads it to know what variables to use and what to build. Linux users are probably familiar with these mk makefile files. If you are not, dont worry about it.
An Android.mk file always has to begin with the LOCAL_PATH definition. It is used to locate the source files. In this case, we are setting this to the directory the Android.mk file is residing in.
Then all local variables are cleared(LOCAL_MODULE and LOCAL_SRC_FILES etc.. this is important because all builds run in a single context and you dont want to keep some variable from another run filled in.)
Then the local module is set – this is going to be the name of the .so file in this case(thus, libhello-jni.so) and then all the source files that belong in the module are declared.
Finally, include $(BUILD_SHARED_LIBRARY) will generate the .so lib.

Now, add the following method to your activity:

public native String getJniString();

, add the static block

static {
	System.loadLibrary("hello-jni");
}

and change your onCreate implementation to:

TextView tv = new TextView(this);
tv.setText(getJniString());
setContentView(tv);

When you try to run this, your new android application will exit with an UnsatisfiedLinkError. This is because we did not yet build the .so native shared library.

The only thing left to do is to run the ndk-build shell script, found in the $ANDROID-NDK folder, in the jni directory you created(the folder with the mk and c file in).
This will generate the output

Building the so shared lib

and create the required so.file.
When you run your application now, the string coming from the c file is displayed on the screen:

JNI Test on Android

Check the other ndk samples too

This tutorial is a wrapup of the hello-jni sample the android ndk ships with. In the samples directory of the ndk, there are a lot of other interesting examples. There is even one that shows you how to write and declare your whole activity in c: the native-activity sample. Whether doing that is a good idea, is something else entirely though.

 

from: http://www.integratingstuff.com/2010/12/12/calling-native-c-code-through-jni-in-android-applications/

http://www.thorntech.com/2012/08/using-jni-with-java-for-android-sawbix-case-study-part-ii/

 

Aspects of Artificial Intelligence

 

Link of book: Aspect of AI

Nilsson mentions two different research strategies in AI. the 1st he calls the “function-follows-form approach” – known as creating solutions in search of problems. The 2nd “form-follows-function approach” – which is the problem-driven approach followed in most AI research. Here, the researcher is motivated by a desire to find a way (any way) of making a computer or computer controlled device solve some specific problem. What the researcher finds or invents to do then is less determined by enthusiasm for a methodology than by an enthusiasm for making things work.

Is the idea from 1983 is suited to this time? And the question now is the future aspect of AI?

 

HTML properties and attributes tag and NodeList

By using jQuery we can easily get DOM element via their properties and attributes. From the HTML properties and attributes tag, jQuery can get a Node, a NodeList to manipulate on them.

From HTML Markup, it will be transferred to the DOM element, such as: $(“img.someClass”) to the an image HTML tag with class = “someClass”.

Or using title of img to get this HTML tag to Object:

$(“img[title=’This is an image’]”)  –> return an img tag list with title=”This is an image”

For each of the properties and attritubes, we can get a Node or a NodeList in DOM to manipulate.

$(“img”) –> return a NodeList of img tag

$(“img #myImage”) –> return a Node of img with id=myImage

On the next post, we will discuss about style, content and form element values.

Hope you get some useful information, give some comments about it please!

Bluemix #2 – Create a sample Nodejs app with IBM Watson services – II

In the fist part of Bluemix #2, we created one nodejs application with IBM Watson TTS service, and deploy it from our computer to the Bluemix server. In this part, we will discuss in detail about its sample code.

The main application file to start is app.js:

The app started with express:

var express = require('express'),  app = express()

and a watson variable that called watson-developer-cloud
watson = require('watson-developer-cloud')

this object stored the watson developer cloud object that will contact with IBM Watson service.

Managing the wrapped element set

When we got a wrapped set that we either identify by using selector to match with existing DOM elements, we can use a set of jQuery commands to handle these elements.

1. Size of a wrapped set – .size()

$(‘a‘).size() -> return a number of links in one page.

When we use .size() jQuery function, it will return the number of matched elements.

$(‘#DivId‘).size() –> return the number of div tag with id = DivId

2. Obtaining elements from  the wrapped set – .get(index)

When  we got a wrapped set of elements, we can use jQuery commands to perform some operations on them. Such as, hide them all with the command .hide(). jQuery treats the wrapped set as an array in javascript. We have get() commands to get all elements when there is no parameter, and get(index) command to get one element from this array.

$(‘img’).get(0) –> return the first tag img in the page.

$(‘img’).get() –> return all img tags in the page.

3. Get the index of an element in an wrapped set of elements – index(element)

We have command index(element) to get the index of the element in one wrapped set of elements. If the element does not exist in the list the value -1 is returned.

4. Add more elements to the wrapped set – .add(element)

 .add(expression)

expression: a selector, an HTML fragment,  a DOM element,  or an array of DOM elements.

Example: $(‘img’).addClass(‘thickBorder’).add(‘img[title]’).addClass(‘seeThrough’)

In this example we add class thickBorder to all images, and class seeThrough to image that have title property. The image with title property will be add to the wrapped set of images.

5. Obtain a new wrapped set based on relationships

jQuery elements relationship

6. Some another way to use a wrapped set

  • find(selector) –> search through the existing wrapped set and returns  a new set that contains all elements that match a passed selector.
  • contains(text) –> returns all elements that contains the passed text anywhere in the body content.
  • is(selector) –> return true if there is any element match the passed selector.

7. jQuery chains

  • end()

Used within  a chain of jQuery  to back up the wrapped set to a previously returned set

$(‘a’).clone().appendTo(‘#somewhere’).end().addClass(‘beenCloned’);
The appendTo()method returns the set of new clones, but by calling end() we back up to the previous wrapped set (the original links), which gets operated on by the addClass()command. Without the intervening end()command, addClass() would have operated on the set of clones.

  • andSelf()

merge the two previous wrapped set in a command chain.

We have written out almost of jQuery commands to manipulate on a wrapped set of elements. Hope you get some useful information and give us some feedback to encourage us to continue writing….

Bluemix #1 – How to create a project with IBM Watson services

In this serie, I will practice the blumemix service and IBM Watson services.

The content will include:

#1 – Create a new app with IBM Watson Services

#2 – Create a sample Nodejs app with IBM Watson services

#3 – Customize the NodeJS app with IBM Watson services.

 

In the first of article, I will show the step by step to create an Bluemix Application with IBM Watson services.

We’ve known what is Bluemix.net. I have create several free trial account for this service, each for only one month (30 days). But I only write these things on my leisure time, so it’s expired, expired, and expired… the bluemix account. I’ve just create this one: Nigel.itech@gmail.com Continue reading Bluemix #1 – How to create a project with IBM Watson services