Use annotations to generate multilingual bindings through gobject Introspection

Source: Internet
Author: User

Reprinted please indicate the source and the author contact: http://blog.csdn.net/mimepp

Contact information: Yu Tao <yut616 at Sohu dot com>

Keywords: gobject introspection, gir, typelib, Python binding, JavaScript binding, G-ir-encoding, G-ir-Compiler

Here we will record how to generate multilingual bindings using annotations for future reference.

Preparations:

Install gobject Introspection

sudo apt-get install libgirepository1.0-dev
C code to be bound:

Foo. C, foo. h

Foo. h

#ifndef FOO_H#define FOO_Hvoid foo_hello (void);#endif

Foo. c

#include <stdio.h>#include "foo.h"/** * foo_hello: * * This function just for test. */voidfoo_hello (void){        printf ("hello foo!\n");}

Pay attention to the comments of foo_hello in Foo. C. This part must comply with the GTK-Doc annotations syntax. For details, refer:

Https://live.gnome.org/GObjectIntrospection/Annotations

Compile the library file:

gcc -fPIC -shared foo.c -o libfoo.so.0

G-ir-records generate gir:

g-ir-scanner --namespace=Foo --nsversion=1.0 --library=libfoo.la foo.h foo.c -o Foo-1.0.gir

Output Information:

$ g-ir-scanner --namespace=Foo --nsversion=1.0 --library=libfoo.la foo.h foo.c -o Foo-1.0.girMust specify package names on the command lineg-ir-scanner: compile: gcc -Wall -pthread -I/usr/include/glib-2.0 -I/usr/lib/i386-linux-gnu/glib-2.0/include -c -o /work/test/tmp-introspect6jSTu2/Foo-1.0.o /work/test/tmp-introspect6jSTu2/Foo-1.0.cg-ir-scanner: link: libtool --mode=link --tag=CC gcc -o /work/test/tmp-introspect6jSTu2/Foo-1.0 -export-dynamic /work/test/tmp-introspect6jSTu2/Foo-1.0.o -L. libfoo.la -pthread -Wl,--export-dynamic -lgio-2.0 -lgobject-2.0 -lgthread-2.0 -lgmodule-2.0 -lrt -lglib-2.0libtool: link: gcc -o /work/test/tmp-introspect6jSTu2/.libs/Foo-1.0 /work/test/tmp-introspect6jSTu2/Foo-1.0.o -pthread -Wl,--export-dynamic -Wl,--export-dynamic  -L. ./libfoo.so -lgio-2.0 -lgobject-2.0 -lgthread-2.0 -lgmodule-2.0 -lrt -lglib-2.0 -pthread

Note that the parameter "-- library = libfoo. La" of G-ir-example requires libfoo. La. If "-- library = foo" is used, an error occurs:

ERROR: can't resolve libraries to shared libraries: foo

The error cause is pending.

A typical example of libfoo. La is:

# libfoo.la - a libtool library file# Generated by libtool (GNU libtool) 2.4.2 Debian-2.4.2-1ubuntu1## Please DO NOT delete this file!# It is necessary for linking the library.# The name that we can dlopen(3).dlname='libfoo.so.0'# Names of this library.library_names='libfoo.so.0.0.0 libfoo.so.0 libfoo.so'# The name of the static archive.old_library='libfoo.a'# Linker flags that can not go in dependency_libs.inherited_linker_flags=''# Libraries that this one depends upon.dependency_libs=''# Names of additional weak libraries provided by this libraryweak_library_names=''# Version information for libfoo.current=0age=0revision=0# Is this an already installed library?installed=no# Should we warn about portability when linking against -modules?shouldnotlink=no# Files to dlopen/dlpreopendlopen=''dlpreopen=''# Directory that this library needs to be installed in:libdir='/usr/local/lib'

You can obtain this file from *. La compiled in any open source file.

Of course, you can use automake/Autoconf to generate libfoo. La to compile and generate gir above, which is more convenient.

G-ir-compiler generates typelib:

g-ir-compiler --includedir=. -o Foo-1.0.typelib Foo-1.0.gir

Installation:

sudo cp Foo-1.0.gir /usr/share/gir-1.0/sudo cp Foo-1.0.typelib /usr/lib/girepository-1.0/

Preparation:

sudo vi /etc/ld.so.conf

Add the path of your libfoo. So, such as/work/test.

sudo ldconfig

Reload.

Use gjs to run:

Compile a test. js file as follows:

const Foo=imports.gi.Foo;Foo.hello();

The running result is as follows:

$ gjs test.js hello foo!

Use python to run:

Compile a test. py file as follows:

from gi.repository import FooFoo.hello()

Running result:

$ python test.py hello foo!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.