Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

Wednesday, April 30, 2008

Creating and Using .so Files With gcc

And what is an .so file you might ask? Those from a Windows background might know them as .dll files, but that still doesn't answer the question.

Normally, when you compile a program, the system takes all of the pieces of code that you've written, converts them into something the computer can understand (machine code) and glues them together. You might compare the process to building a pyramid. All of the stones that you've sculpted are joined together and stacked up. Just like with a pyramid, if you want to change one of the stones, everything that sits on top of it needs to be adjusted. With a C program, making changes in a piece of code means lots of pieces need to be recompiled and reglued together.

If you use dynamic linking, shared libraries, whatever you'd like to call them, things work a bit differently. With dynamic linking, the pieces are not all glued together or stacked up, instead they are plugged in when they are needed. As a result, changes to a piece of code don't require that lots of other pieces get reshaped (recompiled) to fit. With dynamic linking, it is possible to change a piece of code while the program is running. Funny, that sounds a bit like a scripting language.

Here's an example. Let's say I wrote a library with a function called SpecialPrint. (It's like printf but different!) Here is the code for speciallib.c:
#include<stdio.h>

void SpecialPrint(char* payload) {
printf("Special Print:%s\n", payload);
}
Now I write a program that uses this library. Here's the normal, non-dynamic, static linking, built like a pyramid way of using the library.
#include<stdio.h>
#include"speciallib.h"

int main() {
printf("normal printf argument\n");
SpecialPrint("special print's argument");
}
With the above code, I compile the SpecialPrint library with my main code and link them together, producing a single executable file. However, I can use the same speciallib file with code that loads the library dynamically while the program is running. The code might look something like this:
#include<stdio.h>
#include<dlfcn.h>

void ShowError() {
char *dlError = dlerror();
if(dlError) printf("Error with dl function: %s\n", dlError);
}

int main() {
void *SharedObjectFile;
void (*SpecialPrintFunction)(char*);

// Load the shared libary;
SharedObjectFile = dlopen("./speciallib.so", RTLD_LAZY);
ShowError();
// Obtain the address of a function in the shared library.
SpecialPrintFunction = dlsym(SharedObjectFile, "SpecialPrint");
ShowError();

printf("normal printf argument\n");
// Use the dynamically loaded function.
(*SpecialPrintFunction)("special print's argument");

dlclose(SharedObjectFile);
ShowError();
}
In the above, the show error function is optional, I added it so that any errors that occur would be displayed. When you compile the above code, you'll need to make the speciallib into an .so file and make sure that the speciallib's directory (the current directory in my example) is listed as one of the places that we should look for shared object files. Here's what the steps to compile look like:
export LD_LIBRARY_PATH=`pwd`
gcc -c -fpic speciallib.c
gcc -shared -lc -o speciallib.so speciallib.o
gcc main.c -ldl
For an explanation of what the above compiler options mean, and further explanation on .so files, see these two IBM articles on using shared object files on Solaris and linux.

Learning about .so files has been very interesting to me, because they bring a new level of flexibility to a traditionally rigid environment like C.

Monday, April 21, 2008

Ubuntu Hardy Heron

Last week, I downloaded the beta release of Ubuntu 8.04 (Hardy Heron) to give it a try. I've been meaning to migrate our last Windows XP machine over to Linux for some time now (the other three computers I use regularly are Linux machines), but I've been reluctant to backup, repartition, and take the plunge. It seems like this is a common feeling among computer owners, but I think the Ubuntu community may have found an effective solution.

And the name of this new innovation: Wubi. Pop in the Ubuntu CD while running in Windows, and an auto-run installer opens which allows you to install Linux alongside Windows. When you reboot your computer, you see a menu of which operating system to boot into: Windows or Ubuntu. This means you can try out Ubuntu on your computer with zero risk to your existing files. In fact, you can access the files on your Windows installation from within Ubuntu. And if you decide Ubuntu is not your you, you can uninstall it as you would any other Windows program. Pretty slick.

Managing in the installation is just the beginning of the improvements the team has made. After I installed the latest version and booted into Ubuntu, it told me that there were propriety drivers for my NVIDIA graphics card and asked if I wanted to install them. I clicked the button, the download started, and I was up and running with 3D accelerated graphical desktop effects. I think I could sit there opening and closing windows all day. I'm looking forward to the upcoming official release.

Monday, March 24, 2008

XO Laptop

My XO laptop arrived in the mail recently and it is quite an amazing little machine. Conclusion up front: I'm extremely satisfied with it and in some ways this laptop computer is better than ones that sell for ten times the price.

You might recall from a previous post that I had downloaded the XO's operating system and taken it for a test drive in an emulator. Now I have the real thing in front of me, and it's safe to say that it is even better. After all, some of the most innovative features of this computer are in the hardware. My favorite feature is the screen. It is viewable in direct sunlight which makes it usable outdoors. Second up would be the wireless networking. The graphical network selector is fun to use and the connection tends to be more reliable than any of the other computers I've used with my home wireless router. The battery life is also impressive, easily five hours on a charge of its small battery. It even has a built-in camera and microphone.

It runs all of the software I need too. I used the instructions I wrote up when I installed firefox on the emulated operating system. Everything went smoothly and I was browsing the web using firefox in a few minutes (The XO laptop comes with a perfectly good web browser, but I wanted to use my favorite plugins and have more control over downloads).

I'm quite taken with the little machine. I've been using it as my primary computer at home, using it for all of the tasks I normally do (mostly browsing the web and programming). There are a couple of things that I would change if I had the chance. The first is the keyboard. It is an interesting design, made of a flexible rubber-like substance, and it works much better than other flexible keyboard that I've tried, but it took a while to get used to the shift key (I have to press in the corner of the key). The other difficulty is presented by the slower processor, but it doesn't get in my way most of the time. The only time I notice any slowness is when playing flash videos (like on YouTube). Perhaps part of the problem is flash for Linux, but I'm not sure. In any case, I don't really mind as I don't watch that much video, and if I want to, I have other computers that I can use.

It will probably come as no surprise that I wrote this post using the little green computer. I'm saddened by the end of the "give one get one" program, as I think there is still the opportunity for more people to donate and receive their own XO. If anyone is interested, it might be possible to order a batch of one hundred or more through the "give many" program.

Tuesday, March 04, 2008

(Portable) Ubuntu for Programmers

I've been trying over the past several weeks to find the best fit for Linux on a USB pen drive so that I can boot into my own operating system and get to my files no matter which computer I'm using. As you might notice from my other posts, I tend to spend quite a bit of my computer time in programming and browsing the Web, so the things I'm most interested in are a web browser (Firefox), support for wireless cards in several computers, and a variety of command line programming tools (gcc, python, vim, etc.). It should be possible to take one of the standard Linux distributions and install it on a USB drive (provided the drive is large enough), but I wanted to use a one gigabyte drive that I had, and with my simple needs I should really be able to get all of the necessities in under one gig. Along the way I've tried Puppy Linux, Slax, Feather Linux, DSL, and others, but I decided in the end to roll my own solution based on Ubuntu.

I'm a big fan of Ubuntu, but the standard desktop install is far too large for installation on a one gig drive. For a while I was using the live CD booting from a pen drive with a partition for my files. I used the instructions I found on Pen Drive Linux to set up the pen drive with the image from the live CD (only 750 megabytes). The only problem with this set-up was that all of my files were in a seperate partition and my home directory was wiped out each time. Since many Linux programs store settings in your home directory, this turned out to be a bit incovenient. I tried a few different options, but finally decided to go with a stripped down Ubunutu foundation and add the things I wanted.

I began with Ubuntu Server 7.10 and installed it on my USB drive using some of the recommendations in the installation instructions for low memory systems. During the installation process I selected guided partioning and I did not choose to install any of the software configurations in the "software to install" menu. After installing, I rebooted and added the following packages using sudo apt-get install:
lynx (optional)
screen (optional)
gcc (optional)
xorg
x-window-system-core
firefox
If you are using a laptop, you will likely want to install the following modules:
acpi
acpid
With the above installed you can check the battery's charge, remaining time, etc. by running acpi on the command line. For the graphical desktop window manager, I chose iceWM. I installed it by adding:
icewm
iceconf
icewm-themes
In the past I've worked quite a bit with Fluxbox as a window manager, but it seems like iceWM is easier to configure, especially under Ubuntu. The liQuid theme looks quite nice.

This set up boots into a text only command line mode because it is based on Ubuntu Server, to enter graphics mode, you simply run startx. I connected to my wireless network using wpa_supplicant and running iwconfig.

One of the benefits of working on a lightweight system on a flash drive is the bootup speed. In twenty seconds the computer boots from a cold start, connects to my wireless network, and enters the graphical desktop. I'm quite happy with my little portable operating system, and you probably won't be suprised to hear that I wrote this post using it.

Monday, November 12, 2007

Fluxbuntu 7.10

Long time readers may remember the saga of my old laptop (a Compaq Presario 1700T circa 2000). I had ditched openSUSE several months ago in favor of Fluxbuntu, a variant of Ubuntu which used the light weight Fluxbox windowing system in place of the Gnome desktop. My old computer has only 128 megabytes of RAM, so memory is at a very high premium. With the release of Gutsy Gibbon, Fluxbuntu picked up a few new features, so I upgraded and gave it a try. I have been extremely pleased. All the good stuff is there that I enjoyed before (installing new free software using Synaptic or Aptitude, Firefox, XMMS, etc.) but there were a couple of great new additions. For one, automounting of USB drives. I have a small pen drive that I carry around to hold many of my files: music, programming projects, etc. Mounting had always been a bit of a pain with my laptop's OS. Now I just plug it in, it mounts, and an icon for the drive appears on my desktop.

The discovery came at a perfect time. My in-laws decided they wanted to resurrect an old PC so they could browse the web side by side. All they really needed was Firefox, Open Office, and Picasa and their computer had the same amount of RAM as my old laptop (probably from about the same time frame). Fluxbuntu to the rescue ;-) Can you believe it, my in-laws are running Linux.

Sunday, September 03, 2006

Reviving my laptop

I have an old laptop which I would hate to see go to waste, an Intel 796 megahertz processor with 128 megabytes of ram and the weight of Windows XP has become too much for it to bear. I want a system that will run quickly and smoothly. I need a web browser and programming tools (gcc, make, python, perl, svn, etc.) and an mp3 player might be nice too. I had been running OpenSUSE, but the performance was still a bit sluggish. Then I tried Damn Small Linux (DSL) and it had almost everything I need. Fluxbox is a great windowing system and it ran extremely well. Things started to break down when I tried to install make, a series of dependencies and library downgrades prevented me from being able to get everything I needed. The problems continued the more I tried to modify the system. So I've tried others, five distributions so far, but none seem to work just right. This is turning into quite the weekend project.