Emacs: Inserting Ruby "Code Blocks"

| No Comments
1. Highlight the material you wanted commented out.
2. M-;

Repeat above on the same code to uncomment it all.

Quick Bandwidth Limiting in Linux with Trickle and Wget

| No Comments

What the Heck?

| No Comments

gcc/g++: Mixed static and dynamic linking

| No Comments
I couldn't find any good guide on how to do mixed static and dynamic linking with gcc or g++, so I just figured it out by trial and error. This is what I came up with. I hope it is technically accurate.

First of all, you need to decide what libraries you want statically linked (leaving, of course, the rest to be dynamically linked). You have to decide on your own strategy, but generic libraries, like your game engine, or your regular expression handler, are likely candidates. But you'll probably want to use dynamic linking for implementation specific functionality. For example, you don't want to statically link opengl libraries, because there will be a different implementation of that interface on each computer depending on what graphics card drivers have been installed.

Then, you will need to get the code archives for the libraries you want to statically link. These are *.a files, such as libclanCore.a. You get these when you compile the library from source, usually by specifying  --enable-static on the configure command line. When compiling these libraries, you'll generally want to be using the most generic compiling options you ca (-O2 optimization is usually fine) since the code may be run on a very wide variety of processors. Copy the *.a files you need into your source code directory.

Next, compile local source code files into object code. E.g.:

$ g++ -O2 -c *.cpp
Now, you need to do the linking. This part can be a bit tricky, and it will all be done in one command. For comparison, I'll first show you how I would normally link my code, using only dynamic linking:

$ g++ -O2   -o lusus_stack data.o lusus_stack.o     \
lusus_stack_funcs.o lusus_fallingleaf.o \
lusus_explodinglogo.o lusus_stacktitle.o \
lusus_wstackgraphic.o  -lXxf86vm -lclanApp \
-lclanCore -lclanDisplay -lclanSound -lclanVorbis \
-lfontconfig -lfreetype -lclanGDI

This produces an executable called lusus_stack. But I want to statically compile in all the clanlib game engine files. So I copy the libclan*.a files I needed into my source directory, and use this command:

$ g++ data.o lusus_stack.o lusus_stack_funcs.o      \
lusus_fallingleaf.o lusus_explodinglogo.o \
lusus_stacktitle.o lusus_wstackgraphic.o -o statapp \
libclanVorbis.a -lvorbis libclanSound.a -lasound \
libclanGDI.a libclanDisplay.a -lpng12 -ljpeg \
libclanApp.a libclanCore.a -lXxf86vm -lfontconfig \
-lpthread
You'll notice four elements here:
  • My local object code (*.o files)
  • Imported code archives (*.a files)
  • dynamically linked libraries (normal -l syntax)
  • the file name to be given to the executable (-o)
I did not use the -static option, because -static prevents any dynamic linking from being used.

You will also see that I had to specify more libraries this time to be dynamically linked. These are the dependencies of the statically linked archives. You have to specify these manually now, because the system won't be able to dynamically figure them out later (from the shared object files).

How do you figure out what the dependencies are? One way is to use the ldd tool on the shared object (.so) version of the library, like so:

$ ldd /usr/local/lib/libclanDisplay.so 
    linux-vdso.so.1 =>  (0x00007fff7bbff000)
    libpng12.so.0 => /usr/lib/libpng12.so.0 (0x00007fd1174b5000)
    libjpeg.so.7 => /usr/lib/libjpeg.so.7 (0x00007fd117277000)
    libz.so.1 => /lib/libz.so.1 (0x00007fd117061000)
    libstdc++.so.6 => /usr/lib/gcc/x86_64-pc-linux-gnu/4.3.2/libstdc++.so.6 (0x00007fd116d5e000)
    libm.so.6 => /lib/libm.so.6 (0x00007fd116ada000)
    libc.so.6 => /lib/libc.so.6 (0x00007fd116785000)
    /lib64/ld-linux-x86-64.so.2 (0x00007fd117a5d000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007fd11656f000)

Some of these libraries you won't need to specify, like libc, because they are automatically included by gcc/g++, or because they are dynamically loaded by some other library to which you are dynamically linking. But you can see here some obvious ones like libpng and libjpeg.

Now, important note: It does matter in which order you place the files on the command-line when you go to do the linking! The reason for this: when gcc/g++ looks to resolve undefined references to functions, it will only resolve those references for files that have already been processed.

What that means: if FILE A depends on functions from FILE B, put FILE A before FILE B on the command-line.

How will you know if you failed to link in a library needed by one of your static libraries? Simple: gcc/g++ will spit out about 300 pages worth of complaints, looking something like this:

libclanDisplay.a(jpeg_provider.o): In function `CL_JPEGProvider::save(CL_PixelBuffer, CL_StringContainer<char, CL_StringReference<char, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > const&, CL_VirtualDirectory&, int)':
/scratch/choward/ClanLib-2.0.4/Sources/Display/ImageProviders/jpeg_provider.cpp:114: undefined reference to `jpeg_std_error'
/scratch/choward/ClanLib-2.0.4/Sources/Display/ImageProviders/jpeg_provider.cpp:116: undefined reference to `jpeg_CreateCompress'
/scratch/choward/ClanLib-2.0.4/Sources/Display/ImageProviders/jpeg_provider.cpp:137: undefined reference to `jpeg_stdio_dest'
/scratch/choward/ClanLib-2.0.4/Sources/Display/ImageProviders/jpeg_provider.cpp:152: undefined reference to `jpeg_set_defaults'
/scratch/choward/ClanLib-2.0.4/Sources/Display/ImageProviders/jpeg_provider.cpp:156: undefined reference to `jpeg_set_quality'
...

You can figure out what library is missing its dependencies from the first line here: libclanDisplay. And you have to guess which library you need from the complaints about what functions are missing. It's pretty obvious here that we need libjpeg.

Anyway, that's all folks. If I have posted anything inaccurate or incomplete, let me know in the comments.

[Shameless plug: Be sure to checkout out my programming web site at linuxprogrammingforums.com!]

New Website: Linux Programming Forums

| No Comments
I have started a new web site:

linuxprogrammingforums.com

The web site provides online forum resource for a wide variety of languages and programming topics. The layout and organization of the forums is geared toward the needs and mindset of programmers with a Linux background.

The board motto is

"May the source be with you."

Java BufferedImage: Beware of getSubimage!

| No Comments
Wow, I just spent way too long trying to figure out a bug in some of my gaming code. Nothing made sense no matter how many times I looked at the problem and how many angles I considered. The weirdest things were happening in my graphical output, and I was getting very frustrated.

I got desperate and decided to look over the APIs of all the graphical functions I was using. Then I looked at the BufferedImage.getSubimage function API and it was painfully obvious:

getSubimage

public BufferedImage getSubimage(int x,
                                 int y,
                                 int w,
                                 int h)

    Returns a subimage defined by a specified rectangular region. The returned BufferedImage shares the same data array as the original image.

    Parameters:
        w - the width of the specified rectangular region
        h - the height of the specified rectangular region
    Returns:
        a BufferedImage that is the subimage of this BufferedImage.
    Throws:
        RasterFormatException - if the specified area is not contained within this BufferedImage.

I had assumed that getSubimage would return a new BufferImage array, but it actually is really just a map to part of the original BufferImage array. Consequently, whenever I was painting in the new BufferImage object, I was actually painting all over my original BufferImage!

Well, that was a lesson learned the really hard way.

Java: Making Ambiguous References Unambiguous

| No Comments
// *** Key.java ***
interface Key {}

// *** Card.java ***
interface Card {}

// *** KeyCard.java ***
public class KeyCard implements Key, Card {}

// *** Door.java ***
public class Door
{
    public void openWith(Key key) { System.out.println("Opened door with key"); }
    public void openWith(Card card) { System.out.println("Opened door with card"); }
}

// *** Scenario.java ***
public class Scenario
{
    public static void main(String[] args)
    {
       KeyCard keycard = new KeyCard();
       Door door = new Door();
door.openWith(keycard);
    }
}

What's wrong with this Java code? It won't compile:

$ javac Scenario.java
Scenario.java:15: reference to openWith is ambiguous, both method openWith(Key) in Door and method openWith(Card) in Door match
door.openWith(keycard);
^
1 error

There are two functions called openWith, which is fine, because Java supports overloaded functions. And we are passing an object to a function on the basis of which interface it implements, which is fine, because Java supports polymorphism. We have also implemented more than one interface in the door class, which normally is fine, because Java allows you to implement more than one interface per class, as a replacement for multiple inheritance.

The problem, though, is that in this case we have an overloaded function, openWith, capable of taking an object that implements either the key interface or the card interface, but we passed it an object that implements both the key interface and the card interface, and the compiler does not know what you want it to be passed in as. So it is an ambiguous function call.

I ran into this error in some code at work today. (Real code, not the silliness above.) I wasn't quite sure what to do. One option was to change the names of the functions in the Door class, to something like openWithKey(Key key) and openWithCard(Card card). But that would be effectively abandoning function overloading, which is a really convenient and tidy feature in any language.

Another option would be change the KeyCard class to support only one interface or the other. But I want KeyCard to have more than one interface implemented... that is what makes it such a cool object.

It took me a few minutes of staring at my screen to recognize the simplest, easiest, and most obvious choice. Every object variable in Java is really just a reference to the object right? So why don't we just add a few lines to the Scenario class, like so?:

// *** Scenario.java ***
public class Scenario
{
    public static void main(String[] args)
    {
       KeyCard keycard = new KeyCard();
       Door door = new Door();
Key key = keycard;
Card card = keycard;
door.openWith(key);
door.openWith(card);
    }
}
Now this compiles and runs:

$ javac Scenario.java
$ java Scenario
Opened door with key
Opened door with card

The variables key, card, and keycard all refer to the same object. That's fine with the compiler... it just needs to know which interface the object is supposed to look like when you pass it in.

So, in summary, make an ambiguous function call unambiguous by aliasing it before-hand to be the specific kind of interface you need at that moment.

First Gentoo Router

| No Comments
I stayed up late last night setting up my first Gentoo-based home router out of an old PC (500 Mhz Pentium III, 256 MB RAM). It has three interfaces (two LAN, one WAN), is fully optimized for the processor, and provides DHCP, DNS, and NAT services.

It was overall fairly easy following the online Gentoo documentation:

http://www.gentoo.org/doc/en/home-router-howto.xml

The longest part was all the time I spent trimming down the kernel (not strictly necessary but a lot of fun) and then it takes a long time for the kernel to build on a 500 Mhz box. It took probably 3-4 of my attention, including putting the PC together and testing the services, and about 7+ hours of computer time.

Uh, no... Very Different.

| No Comments
So I'm sitting on some stair steps at school, and I turn my laptop on, which is running Gentoo. The boot phase starts as usual, and the text scrolls by as the services start up.

A large, young fellow walks by, sees the screen, and says "Is that MS-DOS?"

"No," I quickly reply, "It's Linux."

"Oh. They're basically the same, aren't they?" he says.

Ouch, that hurt.

Nifty! Custom-Built Linux Computers

| No Comments
system76

What attracted my attention was the server section, where you can by tower or rack mounted servers starting at around $700. Everything is Linux compatible, and they give you lots of options on the components.

After playing around with the "Eland Pedestal" tower server, I found that for about $1,200 I could get something like this:

  • Quad Core Intel Xeon X3230 2.66 GHz 1066 MHz FSB 8 MB L2 65nm
  • 4 GB DDR2
  • 3x 500 GB drives in a RAID 5 array

Wouldn't mind having two or three of those babies for my cluster.
The site blog for indicium.us.
Linux Projects
Online Games
Unsung Linux Games

RSS Feed

Powered by Movable Type 4.21-en
and GNU/Linux


Creative Commons License
The content of this blog is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License. See my copyleft page for more details.