Thursday, October 01, 2009

Compiling Boost Regex in Linux

Boost libraries provide many interesting C++ libraries with cool functionalities. I've been using for many years now boost shared pointers and boost regex library (for regular expression). In particular, I'm using them in my software GNU Source-Highlight. These libraries are usually available in LInux distributions, but if they are not, and you need to build them (compile them) from sources... well... here comes trouble :-)

The first time, it is actually not easy to build boost libraries from sources, even though you're used to build programs from sources following the usual "./configure && make && make install"; in particular, boost use their own build system based on bjam. Indeed, I've had many complaints from source-highlight users when it comes to build source-highlight and the boost libraries are not available in their system.

I hope this post sheds some light for building boost libraries from source. In particular, I'll concentrate on regex library (in fact, you're not required to build all the libraries: you can simply select the ones you're interested in). Note that some boost libraries come only in the shape of header files (like shared pointers) and others require also a lib file (like regex). Note, however, that all the header files will be installed anyway (they basically do not require any compilation), while library files require compilation, and compiling all the libraries in boost may require some time; thus, having the chance to select only some libraries is quite useful.

In this post I'll assume that you want to build only the regex library and to install all the built files into $HOME/opt/boost (so that you will not need root access rights).

Once you unpacked the sources into a directory, say ~boost

tar xjvf ~/Downloads/boost_1_39_0.tar.bz2
you get into that directory
cd ~boost
and run the booststrap command
./bootstrap.sh \
--prefix=$HOME/opt/boost \
--libdir=$HOME/opt/boost/lib \
--with-libraries=regex
Hopefully, you will get no error, and then you're ready to build and install the boost headers and the regex library with the following command:
./bjam --build-dir=build --layout=system install
This will install the directories include and lib into $HOME/opt/boost.

You can now test your installation using the example program you find in the boost website, example.cpp:

#include <boost/regex.hpp>
#include <iostream>
#include <string>

int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );

while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}

Since you installed boost in a directory which is probably not in the search path of your compiler, you will need to specify the path for header inclusion and the path for library, as follows:
g++ -I$HOME/opt/boost/include example.cpp -o example \
-L$HOME/opt/boost/lib -lboost_regex
If everything went fine, you'll have your program to test.

If you want to run your program, remember that the boost regex library you built is a dynamic library, thus, again, since it is not installed in a system path where the loader searches for libraries to load at run-time, you'll need to specify that path with the environment variable LD_LIBRARY_PATH, e.g.,
LD_LIBRARY_PATH=$HOME/opt/boost/lib ./example
As I said before, you could have install the library and headers in system path, e.g., /usr; all you have to do is to specify a different bootstrap command
./bootstrap.sh \
--prefix=/usr \
--libdir=/usr/lib \
--with-libraries=regex
and then run bjam as before (but with root privileges).

Hope this helps :-)

Monday, September 28, 2009

Source-Highlight-Qt 0.2

The release 0.2 of Source-Highlight-Qt is out! Source-Highlight-Qt is a library for performing syntax highlighting in Qt documents by relying on GNU Source-Highlight library.

The main new features are (besides some bugfixes):

  • You can customize colors in the color dialog also for elements not defined in the current style
  • A new dialog for source-highlight settings is now provided (see the screenshot)
  • A combo box is provided also for selecting the output format of highlighting
  • Some functionalities are provided for generating a .style file (e.g., starting from the settings of the color and style dialog)
  • Some message boxes can be used to display source-highlight exceptions

Basically, all the new additions were functional for the new software I released (I'll blog about that in the next few days): QSource-Highlight, i.e., a Qt4 graphical interface for source-highlight itself :-)

Tuesday, September 22, 2009

GNU Source-Highlight 3.1.1

In this new version of GNU Source-Highlight some additions were made to the API of the library; the most important are:

  • access to static global LangDefManager, LangMaps is provided through the new class Instances so that there's no need to create continuously instances of these classes;
  • the library also provides now utility functions for accessing .lang and .outlang files;
  • functions are provided for setting the data dir option globally, in order to increase consistency in applications that use the library (changing such value globally makes sure that the change will be visible in all parts of the library).
Moreover, two new language definition files were added: Texinfo and Haskell (thanks to Sergey Astanin); here are two examples:

texinfo

(@ref{Generating References}).
\input texinfo @c -*-texinfo-*-
@c This file uses the @command command introduced in Texinfo 4.0.
@c %**start of header
@setfilename source-highlight.info
@include version.texi
@settitle GNU Source-highlight @value{VERSION}
@finalout
@c @setchapternewpage odd
@c %**end of header

@example
my example
@end example

@set myhomepage @uref{http://www.lorenzobettini.it}

field names, etc.) by relying on the program @emph{ctags},
@url{http://ctags.sourceforge.net}
@xref{My Long
Reference}.

@inforef{Introduction,,source-highlight-info}.

@c All the menus can be updated with the EMACS command
@c texinfo-all-menus-update, which is normally bound to C-c C-u C-a.
@menu
* Introduction:: What's it for?
* Installation:: Download and installation
* Invoking source-highlight:: How to run @command{source-highlight}.
* Language Definitions:: How to define an input language
* Problems:: Reporting bugs.
* Mailing Lists::
* Concept Index:: Index of concepts.
@end menu

This is an escaped @@ at symbol.

This ``is a string''.

@example
@i{/// read the files }FIXME@i{ of a directory }@@param@i{ }@b{<f} foo="bar"@b{>}

@b{#include} <stddef.h>
@b{#include} <stdio.h>
@b{#include} <sys/types.h>
@b{#include} <dirent.h>

int
@b{main} (void)
@{
DIR *dp;
@b{struct} dirent *ep;

dp = @b{opendir} ("./");
@b{if} (dp != NULL)
@{
@b{while} (ep = @b{readdir} (dp))
@b{puts} (ep->d_name);
(void) @b{closedir} (dp);
@}
@b{else}
@b{perror} ("Couldn't open the directory");

@b{return} 0;
@}

@end example



haskell

-- A test snippet based on
-- http://google-code-prettify.googlecode.com/svn/trunk/tests/prettify_test.html
--
-- A comment
These --> are --| not |-- comments
--but this is a comment again

module Foo(bar) where
import Blah
import BlahBlah(blah)
import Monads(Exception(..), FIO(..),unFIO,handle,runFIO,fixFIO,fio,
write,writeln,HasNext(..),HasOutput(..))

{- nested comments
- should {- just -} work -}
instance Thingy Foo where
a = b

data Foo :: (* -> * -> *) -> * > * -> * where
Nil :: Foo a b c
Cons :: a b c -> Foo abc -> Foo a b c

str = "Foo\\Bar"
chars = [ 'x', '\'', '\\', '\"', '\120', '\o100', '\xbeef' ] -- Escapes too
Not.A.Char = 'too long' -- Don't barf. Show that 't is a lexical error.

(ident, ident', Fo''o.b'ar)

(0, 12, 0x45, 0xA7, 0o177, 0O377, 0.1, 1.0, 1e3, 0.5E-3, 1.0E+45)

[1,2..10]

(#) = \x y -> s x y
where s x y = (x+y) `mod` 13

bar (x:_) = putStrLn x

main = do
let foo x = do { spam $ eggs x }
foo >>= bar

Friday, August 07, 2009

Huawei E1692 in Linux Ubuntu Jaunty


I've just tried this Internet device, HUAWEI E1692 with Linux Ubuntu Jaunty, and basically everything worked out fine smoothly. The Internet connection can be established either with the NetworkManager Gnome applet, or using the application that comes with the device. Linux is officially supported with the provided drivers, and basically the installation works fine, thought the instructions provided are not completely correct...

As for the installation, you need to insert the usb device, and the first time, it is recognized as a USB memory storage; you can then access the data stored in the device, where you find a folder with the drivers and the installation script.



Since the installation requires root privileges it is required to run the install script in the Linux folder using the terminal:

cd /media/Alice\ MOBILE\ HW/Linux
sudo ./install


As suggested in this thread, it is better to specify the installation path, even though you're OK with the default one, by simply inserting the full path. The installation should then succeed


Now, you should be able to setup a connection with NetworkManager applet (actually I had another connection setup for the same operator, thus I could simply reused that one).

Now, an icon should be put in your desktop to use the application that ships with the device; however, I couldn't connect using that application link, probably because the application requires root privileges... but it works fine if I start it from a terminal

cd /usr/local/Alice_MOBILE_E1692/
sudo ./MobilePartner



and then you should be able to connect


The nice thing of this application is that you can keep track of your traffic


and you can send SMSs and access your addressbook

Thursday, July 16, 2009

Source-highlight-qt: A library for highlighting Qt documents using GNU Source-highlight

A few days ago I released a new software that uses the new library provided by GNU Source-highlight: Source-highlight-qt, "a library for performing syntax highlighting in Qt documents by relying on GNU Source-Highlight library".

This library provides an implementation of the qt abstract class QSyntaxHighlighter class, and it deals both with Qt3 and Qt4 (you will need to build a separate version of the library for the two different qt frameworks). Please note, the Qt3 version has less features and it is there only for old qt applications; furthermore, QSyntaxHighlighter class in Qt3 has some design problems which make it quite inefficient to use.

The library can be downloaded from the sourceforge site using this link: http://sourceforge.net/projects/srchiliteqt/files.

For standard usage, it's just a matter of executing this code (you can pass the language syntax, i.e., the source-highlight language definition file, to the init method):

QTextEdit *editor = new QTextEdit;
srchiliteqt::Qt4SyntaxHighlighter *highlighter =
new srchiliteqt::Qt4SyntaxHighlighter(editor->document());
highlighter->init("java.lang");
We also suggest to take a look at the example program qeditexample, for some use cases of the library. Please notice that this qeditexample program is not intended to be used as an editor for production, it's just to show how to use source-highlight-qt library (and it uses the qt4 version).

This is a screenshot of the program qeditexample, editing a C file; basically most of the code is taken directly from the library. For instance, it provides the combo boxes for selecting the highlighting language and the output style (and they be connected to the editor, so that all the changes are automatically applied):


This is another screenshot, where we're editing a C++ file, with a different style


The library also provides a dialog to customize the output style:


Happy Qt Highlighting! :-)

Thursday, June 11, 2009

Php-source-highlight

Just after one week I released version 3.0 of source-highlight with the C++ library, I got an email from Pascal Bleser, telling me that he created a php extension using the library, for highlighting code from php without relying on CGI scripts! You can find its plugin extension here: http://code.google.com/p/php-source-highlight. I took a quick look at his code and it's really simple and clean, hopefully this also means that the library of source-highlight can be smooth to use :-).

You can also might want to take a look at his blog post!

Saturday, June 06, 2009

Tutorial: using Autotools with Eclipse (and Linux Tools Project)

As a GNU maintainer, I've always been using autotools (i.e., automake, autoconf, etc.), and I must admit I still haven't found anything such powerful. Now, I started using the plugins of the Linux Tool Project which seem to have improved a lot! They provide plugins for ChangeLog, Valgrind, and in particular for autotools based projects. Since I haven't seen any tutorial for using the autotools plugin, I thought I could write one; well, it's not that difficult to use the autotools plugin, but I hope it can help :-)

Of course, first of all, you need to install the autotools plugin, and I personally prefer the update site method, using http://download.eclipse.org/technology/linuxtools/update. Remember that the linux tools are based on CDT plugin.

For this tutorial, we will use an already autotools based project (for the moment, the autotools plugin can create an autotools based project, but without any templates). We will use the GNU Hello program, so you can download the last version from the web site. Then you can untar the contents in a directory (say tmp).

Now, it's time to create a new project in eclipse, in particular, a GNU C Autotools Project (or a C++ project if you're project uses C++):


Let's call it "hello" (and then you can select your location and accept the other defaults). You will now have an empty autotools based C project.

Now, let's import the hello package from the directory where we had untarred it, by using "import file system":


Now, the hello project is populated with the package of GNU Hello; notice that configure.ac, Makefile.am and .m4 files are handled by the autotools plugins with syntax highlighting, outline, mouse over functionalities, etc.:


Now, BEFORE building the project for the first time, let's take a look at the properties of the project, where you can see we now have an autotools part, where you can customize the autotools versions you might want to use (I take the defaults):


In particular, from "C/C++ Build - Settings" you can configure the arguments passed to the tools, and the build directory (it is often advised to build a package in a different directory, so that you can configure different builds; by default, the build directory is "build", and I'd leave this that way):


Now, let's change some options! For instance, the configure's --prefix option:


The suffix of the installed program


Since we build the program for development and for debugging, let's make sure to turn off C compiler optimizations (which make debugging a pain):


You can then check that debugging is already turned on, as also '-Wall' option (to report all warnings).

Since we downloaded the source package of hello, produced by 'make dist', this provides all the scripts generated by the autotools in order to run 'configure && make'. However, if you get a program from the CVS, it is likely that you need to bootstrap autotools. Now, the autotools plugin relies on an 'autogen.sh' script which calls all the autotools in the right order, which must be provided by the package itself; however, this is kind of an old method, since now, to bootstrap autotools, you can rely on the program 'autoreconf' which is part of autotools; so, in case you need to bootstrap the autotools, you can simply call 'autoreconf -i' (this will install required files from autotools) from a shell, in the directory of the project.

Now, you can build the project, by right clicking on the project and by choosing "Build Project"! Since this is the first time we build it, the build directory will be created, the configure script will be called with all the features we selected, and the files are compiled!


Hey, but wait a minute! We could have used ccache to compile the sources! And we should have also enable maintainer mode specific configuration settings! Well, we can still do that by going back to the settings of the project, enable the maintainer setting check box, and specify the gcc command as 'ccache gcc':


Now, let's clean the project (this will issue a 'make distclean', but you can customize this from the setting), and build it again, so that it will be reconfigured with the new settings.

Now, let's create another build configuration (e.g., for the release version of the package, with optimized compiler options, with a different prefix, removing the suffix '-debug' and without the maintainer-mode option enabled). We just need to go to "Manage Configurations...", create a new one, starting from the previous default one:


and now we can set the optimization level for the compiler, disable maintainer mode and use another --prefix option (e.g., '/usr/local/'). It is also important to specify another build directory, say 'build_release' (but when creating a new configuration, the build directory has already been changed to something different from the previous one, i.e., 'build1'):


Now, we can switch between the two different build configurations:


Well, that's all for now; I hope you find this tutorial useful :-)