Monday, March 12, 2007

Build Python with 4-byte Unicode support

configure --enable-unicode=ucs4

Here's how you check if Python supports ucs2 or ucs4:
sys.maxunicode return 65535 for ucs-2, but 1114111 for ucs-4

Sunday, March 11, 2007

Getting X window back

Ok, I messed up. I wanted to do some upgrade to my kubuntu Desktop Linux but ended up removing some libs, and X window. I got a console after reboot. These steps got it back:

sudo apt-get install x-window-system-core

Then use this to select a resolution:

sudo dpkg-reconfigure xserver-xorg

This should get the desktop back:

sudo apt-get install kubuntu-desktop

After this step, I need to manually apt-get a few missing packages to be able to run startx. After this, things are pretty much back to normal Oh one thing before it became normal, KDE halted when it tried to set up interprocess communication. The solution is:

chmod owner /home/owner/ICEauthority

Somehow, the ownership of this file got changed. So changing it back fixed it.

Calling a C++ library from FreePascal

It took me a few hours to figure this out - well, not totally figured out yet.

I wanted to call a library from FreePascal. It's a static library (.a file) that's written in C++ and exposes some API functions. Here's how I started:

function myfunc(): int; cdecl; external 'mylib';

FPC fails to find the function. I then realize that I need to use the C++ mangled function name. Complination and linking worked but the program seg-faults upon running.

I then give up the idea of directly accessing it. Instead, I write a wrapper shared lib (.so file) in C++, and expose a C API with extern C" {}. I also wrote a tester program in C to test the wrapper with. Then I get some "undefined reference to std::basic_string..." errors. I tried this but it still didn't work:

{$LINKLIB /usr/lib/gcc/i486-linux-gnu/4..2/libstdc++.a}

I then tried adding libstdc++.a to the .so wrapper lib. It worked! Then my linux box crashed. To make things worse, there was an on-going aptitude update that actually removed X window from my kubuntu. So when I booted up, I only got a console. After I got my X window back, my code stopped working again.

Eventually, I figured out that the original /usr/lib/gcc-lib/i486-linux-gnu/3.3.6/libstdc++.a libstdc++.a was removed in the aptitude upgrade. And the file in /usr/lib/gcc/i486-linux-gnu/4.0.2 didn't work. So I tried to get the older version of libstdc++.a with adept, and it finally worked.

After the C test program work, it didn't take long to make FPC code work with the wrapper lib. All I needed to do was:

{$LINKLIB libmylib.a}
function myfunc(): int; cdecl; external 'mywrapper.so';