2010-02-17

Build Qt as Static Library

Your Qt applications could be smaller and most importantly can be distributed as a single executable, if it is statically linked with Qt library.

A static built Qt application is faster. According to the artical "Qt Performance Tuning":

"A lot of CPU and memory is used by the ELF (Executable and Linking Format) linking process. Significant savings can be achieved by using a static build of the application suite; rather than having a collection of executables which link dynamically to Qt's libraries, all the applications is built into into a single executable which is statically linked to Qt's libraries."

First of all, you need to build Qt as a static library. See the thread http://lists.trolltech.com/qt-interest/2003-05/thread00396-0.html
./configure -thread -static  (makes libqt-mt.a)
./configure -static (makes libqt.a)
./configure (makes libqt.so)
./configure -thread (makes lib-mt.so)
Then, add the necessary plugins. See the artical http://doc.trolltech.com/4.6/plugins-howto.html#static-plugins.

Finally, deploy your application. See the artical http://doc.trolltech.com/4.6/deployment.html.

References:

2010-02-16

Useful Tar Commands

List the contents of tar file
>tar -tvf file.tar

List the contents of a tar.gz file
>tar -ztvf file.tar.gz

tar all .cc and .h files into a tar file named foo.tgz use
>tar cvzf foo.tgz *.cc *.h

extract only one (or several) files
>tar xvzf foo.tgz

extract only one (or several) files
>tar xvzf foo.tgz anagram.cc

extract bz2 tar ball
>tar -jxvf filename.tar.bz2

extract bz2 
>bunzip filename.bz2

Virtualization vs. Emulation

Virtualization and Emulation are two concepts that quite often used in the context of virtual machine. Some softwares can emulate the machine, some can virualize the machine and some can both (e. g. QEMU). After googling for some time, Max found some comparison on them:

"Emulation involves emulating the virtual machines hardware and architecture. Microsoft's VirtualPC is an example of an emulation based virtual machine. It emulates the x86 architecture, and adds a layer of indirection and translation at the guest level, which means VirtualPC can run on different chipsets, like the PowerPC, in addition to the x86 architecture. However, that layer of indirection slows down the virtual machine significantly."

"Virtualization, on the other hand, involves simply isolating the virtual machine within memory. The host instance simply passes the execution of the guest virtual machine directly to the native hardware. Without the translation layer, the performance of a virtualization virtual machine is much faster and approaches native speeds. However, since the native hardware is used, the chipset of the virtual machine must match. Usually, this means the Intel x86 architecture. VMWare is an example of this type of application for Windows."

Reference:

2010-02-15

Setup sb2 enviroment for mini2440

1) download CodeSourcery cross-compiler toolchain from http://www.codesourcery.com/sgpp/lite/arm/portal/subscription?@template=lite Note: Select the toolchain of Linux/GNU.

2) Extract the tar ball and arm-2009q3 is generated
>tar -jxvf arm-2009q3-67-arm-none-linux-gnueabi-i686-pc-linux-gnu.tar.bz2

3) Extract the root_qtopia root
>mkdir root_qtopia
>cd root_qtopia
>tar xvfz ../root_qtopia.tar.gz

4) Copy following directory to root_qtopia
>cp -a $HOME/arm-2008q3/arm-none-linux-gnueabi/libc/{lib,etc,usr} ${root_qtopia}

5) Generate sb2 target
>cd ${root_qtopia}
>sb2-init mini2440_qtopia /disk1/packages/arm-2009q3/bin/arm-none-linux-gnueabi-gcc
>sb2-config -d mini2440_qtopia

6) Compile an example code test.c

#include <stdio.h>
int factorial(int n)
{
if (n == 0) return 1;
return n * factorial (n - 1);
}

int main () {
int i;
int n;
for (i = 0; i < 10; ++i) {
n = factorial (i);
printf ("factorial(%d) = %d\n", i, n);
}
return 0;
}


>sb2
>gcc test.c -o test

7) To execute it, you need install qemu-arm
>git clone git://git.savannah.nongnu.org/qemu.git
>cd qemu
>./configure --target-list=arm-linux-user
>make
>make install
>make && make install

Then /usr/local/bin/qemu-arm is installed.




8) Try to execute
>sb2 test
/bin/bash: ./factorial: No such file or directory

9) run sb2-show to find out a library is missing in path
>sb2-show
sb2-show: error while loading shared libraries: libsb2.so.1: cannot open shared object file: No such file or directory,
due to the bug of sb2 package in debain

Solution: download the newest version and reinstall sb2
... to be continued

Filesystem Hierarchy Standard (FHS)

===========================================
A FHS standard compatible root has the following structure:
===========================================
binEssential command binaries
bootStatic files of the boot loader
devDevice files
etcHost-specific system configuration
libEssential shared libraries and kernel modules
mediaMount point for removeable media
mntMount point for mounting a filesystem temporarily
optAdd-on application software packages
sbinEssential system binaries
srvData for services provided by this system
tmpTemporary files
usrSecondary hierarchy
varVariable data
===============================
/usr hierarchy
===============================
binMost user commands
includeHeader files included by C programs
libLibraries
localLocal hierarchy (empty after main installation)
sbinNon-vital system binaries
shareArchitecture-independent data


Reference:
http://proton.pathname.com/fhs/pub/fhs-2.3.html

Buildroot vs OpenEmbedded

Slowly, buildroot is going to be replaced by OpenEmbedded. Here the citation from http://docs.openembedded.org/usermanual/

"Writing of BitBake recipes is more easy and more intuitive than writing Makefiles while providing higher flexibility. This allows you to tweak specific recipes for your very special needs and to add new recipes very fast. You can build toolchains, Software Distribution Kits (SDKs), complete Distributions or just single packages. The flexibility of OpenEmbedded allows you to reuse the once written recipes for many different purposes. OpenEmbedded provides everything buildroot will be able to provide. But in contrast to buildroot OpenEmbedded will allow you to achieve what you really want to achieve. You can add new package formats, new filesystems, new output formats easily. OpenEmbedded will suit your need."

Evolution of build tools

  • The process of building a computer program is usually managed by a build tool
  • Build Tool: program that coordinates and controls other programs
  • The build utility needs to compile and link the various files, in the correct order
Evolution of build tools:
Make->Ant->Maven->BuiltRoot->BitBake

Nice introduction:
slurchin.googlecode.com/files/OpenEmbeddedv1.ppt

pre-built toolchains for arm

We have a number of options for cross-compiler toolchain.
  • Pre-built toolchains:
    • CodeSourcery: This cross-compiler is used by almost all developers/companies using ARM devices. However, they only provide cross-compilers that work on x86/i386.
    • Emdebian: This cross-compiler is used by Debian. Available for amd64/x86_64 and i386/x86. 
    • Angstrom
  • Tools for creating cross-compilers:
    • Crosstool-ng: Builds a cross-compiler from source. Non-distribution specific.
    • Crossdev: Gentoo's cross-compiler builder. Needs Gentoo.
    • Emdebian: Emdebian also provides ways to build your own toolchain.

Reference:
http://www.plugcomputer.org/plugwiki/index.php/Scratchbox2_based_cross_compiling

Check needed Dynamical Libraries in Linux

> ldd sb-arm-glib-hello
libglib-2.0.so.0 => /usr/local/lib/libglib-2.0.so.0 (0x00000000)
libc.so.6 => /lib/libc.so.6 (0x00000000)
libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00000000)
/lib/ld-linux.so.2 => /lib/ld-linux.so.2 (0x00000000)

2010-02-12

Scratchbox2

Scratchbox2 is exactly what Max is looking for. Many beginners, like Max, have been tired of problems about conflicts between native toolchain in host system and cross-compiler toolchain for target system. Scratchbox2 provides a natural way to solve this issue.

"Scratchbox provides a sandboxed build environment which offers a controlled set of tools and utilities needed for cross-compilation"


As shown in above, sb2 is a virtual execution environment, which requires two things:
fulfills three tasks:
  • build source code for target system
  • execute code in target system
  • manage pacakges in target system.
The wiki artical Scratchbox2 based cross compiling shows general usage of scratchbox2. Nice post on cross-compiling Qt/X11 using scratchbox2. Note: Scratchbox2 is incompatible with Scratchbox1.

Reference:

2010-02-10

Add Code to Blogger

A nice tutorial paste code (xml, java, cshape...) with highlighted syntax in bloger. See the reference.
XML


<target name="compile" depends="init"
description="compile the source " >
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="classpath"/>
</target>

CPP

ElementType FrontAndDequeue(Queue Q) {
ElementType X = 0;
if (IsEmpty(Q)) {
Error("FrontAndDequeue Error: The queue is empty.");
} else {
Q->Size--;
X = Q->Array[Q->Front];
Q->Front = Succ(Q->Front, Q);
}
return X;
}


SHELL

ls ./include/*.h | while read file; do
echo ${file} ...
( cat header.in; cat ${file} ) > ${file}.new
sed "s/\[filename\]/$file/g" ${file}.new
rm ${file}
mv ${file}.new ${file}
done

Remove the Kernal Package is Dangerous

Max occasionally removed the kernal package from his vm-box by

> apt-get remove kernel-image-2.4.27-2-386

Afterwards, the Debain cannot startup any more. :( If you check /boot/* and find out that vmlinux disappeared. Thus gnub cannot load kernel and the following error showed up:

grub error 8 kernel must be loaded before booting

2010-02-05

rename prefix of files

>rename -n 's/^(co)*/da/' *.h (-n for testing purpose)

coDbRetrieverBase.h renamed as daDbRetrieverBase.h
coDbRetrieverById.h renamed as daDbRetrieverById.h
coDbRetrieverHeaderInfo.h renamed as daDbRetrieverHeaderInfo.h
coDbRetrieverImage.h renamed as daDbRetrieverImage.h
coDbRetrieverIngredients.h renamed as daDbRetrieverIngredients.h
coDbRetrieverProcedures.h renamed as daDbRetrieverProcedures.h
coDbRetrieverSortedNameIdPairs.h renamed as daDbRetrieverSortedNameIdPairs.h
coDbSqlReader.h renamed as daDbSqlReader.h
coDbTextSearchEngine.h renamed as daDbTextSearchEngine.h

2010-02-04

Recursively delete .svn directories

rm -rf `find . -type d -name .svn`

Reference:
http://www.anyexample.com/linux_bsd/bash/recursively_delete__svn_directories.xml

Install Correct Kernel Header for insmod

Max is learning kernal module programming. And he found a nice online tutorial for beginer
http://tldp.org/LDP/lkmpg/2.6/html/lkmpg.html#AEN128. However, the HelloWorld module is successfully built, but insmod failed.

> insmod hello-1.ko
insmod: error inserting 'hello-1.ko: -1 Invalid module format

After googling some time, Max figured out his Debian may have installed incorrect kernel header.
Check the running kernel by
>uname -r
2.6.26-2-686

First remove incorrect kernel header package by 'apt-get remove'
Install the following three packages:

  • linux-headers-2.6.26-2-common
  • linux-kbuild-2.6.26
  • linux-headers-2.6.26-2-686
They can be found at http://ftp.us.debian.org/debian/pool/main/l/linux-2.6/. Max download these three deb manually, and installed them. Finally, check whether they are installed by

>dpkg-query --show | grep -i linux-
linux-headers-2.6.26-2-686 2.6.26-21
linux-headers-2.6.26-2-common 2.6.26-21
linux-kbuild-2.6.26 2.6.26-3

Now the hello-1.c can be build and loaded.

Reference:

2010-02-03

Build Qt State Machine Framework as a Static Library

If qt state machine package is by default built as dynamical library, the steps below shows how to build its static version.
  • >wget ftp://ftp.trolltech.com/qt/solutions/lgpl/qtstatemachine-1.1-opensource.tar.gz
  • >tar xvfz qtstatemachine-1.1-opensource.tar.gz
  • >edit buildlib/buildlib.pro change CONFIG += dll to CONFIG +=staticlib
  • >qmake qtstatemachine.pro
  • >make
  • static library is build at lib/libQtSolutions_StateMachineFramework-1.1.a.

2010-02-02

build yaffs2

Max tried to build yaffs2 on his debian vm.

make -C /lib/modules/2.6.26-2-686/build M=/opt/yaffs2 modules
make: *** /lib/modules/2.6.26-2-686/build: No such file or directory. Stop.
make: *** [modules] Error 2

The reason is the installation of kernel header didn't create a symbol link.
  • >apt-get install build-essential (first check if your kernal header is installed)
  • >ln -s /usr/src/linux-headers-2.6.26-1-686 /lib/modules/2.6.26-2-686/build (Then create a symbol link by)
  • >make (build yaffs2 again)
References