You are looking at historical revision 30202 of this page. It may differ significantly from its current revision.

Minimal example of embedding CHICKEN in a regular C program

From the Chicken-users mailing list, a post by Felix.

This shows a minimal example of embedding (you don't need CHICKEN_initialize, unless you want to set specific buffer sizes):

/* gcc x.c -lchicken -o x */

int main()
{
 C_word x;

 CHICKEN_run(CHICKEN_default_toplevel);
 CHICKEN_eval_string("(print (+ 3 4))", &x);
 return 0;
}

Embedding CHICKEN in an Android application

A quick way to make sense of all the moving parts is to copy samples/hello-jni from your NDK directory.

Then you can alter hello-jni.c so it looks like this:

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

jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz )
{
  C_word res;
  char foo[100];

  CHICKEN_run(CHICKEN_default_toplevel);
  CHICKEN_eval_string("(+ 2 5)", &res);
  snprintf(foo, 100, "Evaluation result: %d", C_unfix(res));
  return (*env)->NewStringUTF(env, foo);
}

Then, build CHICKEN using PLATFORM=android for your target architecture (see the CHICKEN README for instructions). You'll have to build a libchicken.so for each architecture you want to support.

For simplicity, let's say for now that we only want to build for armeabi.

In your copy of the sample program, alter jni/Application.mk to read APP_ABI := armeabi. Copy libchicken.so to jni/chicken/armeabi and copy chicken.h to jni/chicken/include.

Then, create a file jni/chicken/Android.mk containing the following:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := chicken
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libchicken.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include

include $(PREBUILT_SHARED_LIBRARY)

Finally, add a line LOCAL_SHARED_LIBRARIES := chicken to jni/Android.mk and modify com/example/hellojni/HelloJni.java to load the chicken module before hello-jni is loaded:

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

You're all set!

To build, run /path/to/ndk/ndk-build and then ant debug to package up the program. You can

Extending this build to other ABI versions is simple: just add it to Application.mk and build different libchicken.so versions and install them under the correspondingly named subdirectory under jni/chicken/<ABI-NAME>/libchicken.so.