source: project/wiki/embedding @ 37686

Last change on this file since 37686 was 31126, checked in by Mario Domenech Goulart, 6 years ago

Properly capitalize CHICKEN on the wiki directory (only first level).

I used the following shell script to change things:

while IFS= read -d $'\0' -r file ; do

sed -i 's/Chicken/CHICKEN/g' "$file"

done < <(find wiki -maxdepth 1 -type f -print0 )

Some files have been manually reverted after that, since some
substitutions don't apply:

  • friedly-chicken (repl banner)
  • survey2011 (Chicken in URI paths)
  • chickenista-guide (Chickenista)

I hope the link canonicalization thing will be on my side.

File size: 3.4 KB
Line 
1== Examples of embedding CHICKEN
2
3=== Minimal example of embedding CHICKEN in a regular C program
4
5From the CHICKEN-users mailing list, a post by Felix.
6
7This shows a minimal example of embedding (you don't need
8CHICKEN_initialize, unless you want to set specific buffer sizes):
9
10<enscript highlight="c">
11/* gcc x.c -lchicken -o x */
12
13int main()
14{
15 C_word x;
16
17 CHICKEN_run(CHICKEN_default_toplevel);
18 CHICKEN_eval_string("(print (+ 3 4))", &x);
19 return 0;
20}
21</enscript>
22
23=== Embedding CHICKEN in an Android application
24
25A quick way to make sense of all the moving parts is to start by copying {{samples/hello-jni}} from your NDK directory. Then you can alter {{hello-jni.c}} so it looks like this:
26
27<enscript highlight="c">
28#include <string.h>
29#include <jni.h>
30#include <chicken.h>
31
32jstring
33Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
34                                                  jobject thiz )
35{
36  C_word res;
37  char foo[100];
38
39  CHICKEN_run(CHICKEN_default_toplevel);
40  CHICKEN_eval_string("(+ 2 5)", &res);
41  snprintf(foo, 100, "Evaluation result: %d", C_unfix(res));
42  return (*env)->NewStringUTF(env, foo);
43}
44</enscript>
45
46Then, build CHICKEN using {{PLATFORM=android}} for your target architecture. 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}} (which has no apply-hack at the time of writing, so we disable autodetection via "ARCH="):
47
48  $ make PLATFORM=android ARCH= C_COMPILER='/path/to/ndk/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc --sysroot=/path/to/ndk/platforms/android-16/arch-arm'
49
50The above is according to "Invoking the compiler (the hard way)" from the {{docs/STANDALONE-TOOLCHAIN.html}} in the NDK's root. The easy way involves more steps and is harder to explain here.  See the aforementioned document for more information.
51
52In 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}}.
53
54Then, create a file {{jni/chicken/Android.mk}} containing the following:
55
56<enscript highlight="makefile">
57LOCAL_PATH := $(call my-dir)
58
59include $(CLEAR_VARS)
60
61LOCAL_MODULE    := chicken
62LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/libchicken.so
63LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
64
65include $(PREBUILT_SHARED_LIBRARY)
66</enscript>
67
68Change{{jni/Android.mk}} so that the application will depend on libchicken, and pull in the new Makefile so it knows how to build it:
69
70<enscript highlight="makefile">
71LOCAL_PATH := $(call my-dir)
72
73include $(CLEAR_VARS)
74
75LOCAL_MODULE    := hello-jni
76LOCAL_SRC_FILES := hello-jni.c
77LOCAL_SHARED_LIBRARIES := chicken
78
79include $(BUILD_SHARED_LIBRARY)
80
81include $(LOCAL_PATH)/chicken/Android.mk
82</enscript>
83
84
85Finally, modify {{com/example/hellojni/HelloJni.java}} to load the chicken module ''before'' {{hello-jni}} is loaded:
86
87<enscript highlight="java">
88    static {
89        System.loadLibrary("chicken");
90        System.loadLibrary("hello-jni");
91    }
92</enscript>
93
94You're all set!
95
96To build, run {{/path/to/ndk/ndk-build}} and then {{ant debug}} to package up the program.
97
98Extending 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}}.
Note: See TracBrowser for help on using the repository browser.