个人工具

UbuntuHelp:Vala

来自Ubuntu中文

跳转至: 导航, 搜索

Vala is a new programming language that aims to bring modern programming language features to GNOME developers without imposing any additional runtime requirements and without using a different ABI compared to applications and libraries written in C. valac, the Vala compiler, is a self-hosting compiler that translates Vala source code into C source and header files. It uses the GObject type system to create classes and interfaces declared in the Vala source code. [1]

Building and Installing Vala

You can install Vala by adding a repository from the Vala team on Launchpad. This is recommended. There are currently outdated versions in the universe repository. The Vala team PPA also contains:

  • libgee - Libgee is a collection library providing GObject-based interfaces and classes for commonly used data structures.
  • vtg - Vala Toys for gEdit is an experimental collection of plugins that extends the gEdit editor to make it a better programmer editor for the Vala language.
  • valide - Val(a)IDE is an Integrated Development Environment (IDE) for the Vala programming language.
  • gtksourcecompletion - used by the editor packages. Source completion is not working in the PPA builds; there is no library file created.

Install Vala from the Vala team PPA:

# add the GPG key for the vala team PPA
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com 7DAAC99C
# add the PPA to your Software Sources
sudo add-apt-repository ppa:vala-team
# update the package lists
sudo apt-get update
# install vala
sudo apt-get install valac vala-utils vala-doc valac-dbg
valac --version

# optionally install other PPA packages
# libgee - collections library
sudo apt-get install libgee-dev 
# vtg - gedit plugin
sudo apt-get install gedit-vala-plugin vala-gen-project
# valide - IDE for vala
sudo apt-get install valide

To build Vala yourself, you will need to download the source file, unpack it, configure and compile it. The "build-essential" package will install the gcc compiler and related tools. Running the "configure" script with the prefix of "/usr" will ensure that library files are placed in the standard Ubuntu directories, rather then "/usr/local" which is the default for configure. You could build with the ./configure; make; make install commands, but then you can't remove Vala with apt-get or other package managers. Using CheckInstall you can create an Ubuntu package (".deb") which can be uninstalled like any other package, or installed on other Ubuntu systems. Note that CheckInstall is not designed to produce packages suitable for distribution. Example for vala-0.7.9:

#necessary dependencies
sudo apt-get install build-essential flex bison libglib2.0-dev
#recommended
sudo apt-get install checkinstall devhelp libgtk2.0-dev 

wget http://download.gnome.org/sources/vala/0.7/vala-0.7.9.tar.bz2
tar -xvf vala-0.7.9.tar.bz2
cd vala-0.7.9/
./configure --prefix=/usr
#checkinstall asks about package settings; hit enter for defaults
sudo checkinstall
valac --version

Libraries and Bindings

[2] To use a library with Vala you need the Vala API file (".vapi") and the Ubuntu development package. Different naming standards are used for the libraries, the Vala API files, and the Ubuntu packages. Vala comes with a variety of VAPI bindings already created. Look for Vala API files in /usr/share/vala/vapi/

Library Vala API (.vapi) Ubuntu development package (.deb) Vala Namespace
GLib glib-2.0 libglib2.0-dev GLib
Gtk+ gtk+-2.0 libgtk2.0-dev Gtk
Poppler-glib poppler-glib libpoppler-glib-dev Poppler
DBus dbus-glib-1 libdbus-glib-1-dev DBus
GStreamer gstreamer-0.10 libgstreamer0.10-dev Gst
Glade2 libglade-2.0 libglade2-dev Glade
SQLite3 sqlite3 libsqlite3-dev Sqlite
Gnome-desktop gnome-desktop-2.0 libgnome-desktop-dev Gnome
Gnome-menu libgnome-menu libgnome-menu-dev GMenu
GnomeVFS gnome-vfs-2.0 libgnomevfs2-dev GnomeVFS

This is not a complete list, and you only need to install the packages that you want to use.

sudo apt-get install libglib2.0-dev libgtk2.0-dev libpoppler-glib-dev libdbus-glib-1-dev libgstreamer0.10-dev libglade2-dev libsqlite3-dev libgnome-desktop-dev libgnome-menu-dev libgnomevfs2-dev 

See InstallingSoftware for options other then apt-get.

Compiling

The Vala compiler "valac" can be used to directly compile your application to an executable. You can also use valac to generate the C source files without compiling. C source code and header files (".c" and ".h") can then be compiled with gcc into executables or libraries. This means you can write code in Vala, and distribute C source files using all the common tools and not imposing any extra build-time dependencies, such as needing Vala installed. Check that you have valac installed:

$ valac --version
Vala 0.7.9

Make a file called "list.vala" based on http://live.gnome.org/Vala/ListSample Compile and run list. (You may need packages "build-essential" or "libglib2.0-dev")

$ valac list.vala -o list
$ ./list

Use the --ccode flag to generate C and header files without compiling an executable (include directives are in the "c" file):

$ valac --ccode list.vala 
$ ls
list.c  list.vala

Compile the C source file with gcc using pkg-config to include the right development files. When compiling with valac GLib/GObject is imported by default.

$ gcc -o list list.c `pkg-config --libs --cflags gobject-2.0`
$ ./list 

pkg-config generates the flags for gcc based on the Ubuntu development packages you have installed:

$ pkg-config --libs --cflags gobject-2.0 
-I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include  -lgobject-2.0 -lglib-2.0
$ pkg-config --libs --cflags glib-2.0
-I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include  -lglib-2.0

Vala API files can be used to include other development packages. Use valac --pkg <vapi file> like you would use pkg-config with gcc. Make a file called "gtksample.vala" based on http://live.gnome.org/Vala/GTKSample (You may need the package "libgtk2.0-dev")

$ valac gtksample.vala
gtksample.vala:1.7-1.9: error: The namespace name `Gtk' could not be found
using Gtk;
      ^^^
Compilation failed: 1 error(s), 0 warning(s)
$ valac --pkg gtk+-2.0 gtksample.vala
$ ./gtksample

In the example above the executable "gtksample" is linked to the Gtk2.0 library. The valac flag --pkg gtk+-2.0 tells the vala compiler to use the Vala API file "gtk+-2.0.vapi". valac looks for Vala API files in /usr/share/vala/vapi/. The "gtk+-2.0.vapi" file tells gcc to include the C header file "gtk/gtk.h". The development header and source files are present on your system because you installed the "libgtk2.0-dev" Ubuntu development package. If you distribute "gtksample" in executable form, the standard GTK+ library "libgtk2.0-0.so" must be on the system. If you distribute the C source files, then the GTK+ development files must also be present.