Text

Cross compiling to 32bit with GCC

Normally if you want to cross compile something to produce a 32bit executable with gcc, you have to pass in the -m32 argument.

$ gcc -m32 -o test32 test.c

After a new install of Ubuntu 11.10 I was left scratching my head for a little bit with the following error:

> In file included from /usr/include/stdio.h:28:0,
>             from test.c:1:
> /usr/include/features.h:323:26: fatal error: bits/predefs.h: No such file or directory
> compilation terminated.

In order to build 32bit executables you need to install the i386 libc dev package.

$ sudo apt-get install libc6-dev-i386

For a little bit of ‘fun’ if you pass the -S -masm=intel arguments to gcc, you can compile once with -m32, and once with -m64 to see how your program changes based on target architecture. It can (perhaps it’s just me :)) be Interesting to see the subtle changes in programs based on how they are assembled for x86_64 vs x86.

$ gcc -m32 -S -masm=intel test.c
Text

PHP and C: The value of getting your hands dirty

Many PHP programmers today fall into two camps: those that are self-taught and learned PHP to make a website, or those coming out of university Comp Sci courses heavily grounded in strictly typed OO (I.e Java).

This is, of course, not absolute, there are camps of reformed Perl sys-admin monkeys that haven’t been seduced by Python. And of course a rare breed of ex systems programmers versed in C and C++.

It is actually this last group of coders that tend to get on with PHP the best.

This makes sense, because PHP was originally designed to be a template engine to produce HTML with a core C backend doing the grunt work. This seems a ridiculous idea now, as CPU cycles are much cheaper than man hours. But in the late 90s native code ran circles around dynamic, interpreted alternatives.

These days C is a little bit like ASM was in the 90s. Being proficient in it is handy, but not essential. I would argue though, for PHP coders it is an important environment in which to be familiar.

Many of PHP’s eccentricities can be traced to it’s C roots. The strange and inconsistent function parameter orders, right down to the sometimes peculiar way it handles memory and references.

The file and string functions marry up almost 1:1 with standard C. Experience of and understanding of how these low level functions work will immeasurably improve your application of their PHP counterparts.

Networking is another area where PHP and C are very similar. Indeed you can write a simple tcpclient in PHP, get rid of all the $ variable identifiers and have a recognisable C fragment.

The biggest benefit though of learning a bit of C or C++, is to get an “appreciation” of memory management. C is unique among the programming environments of today in that it does not manage memory for you. Arguably this is what makes C so painful to develop in. But similarly, a sometimes useful characteristic that makes the environment still relevant today.

By way of example, strings in C are represented by an array of chars, terminated by a NUL ‘\0’.

In C, you would declare a string like  this: char str[20];

This declares a string of 20 characters, and str itself points to an address in memory where 20 bytes have been reserved for itself. Now if we try to write 21 characters to this string, C wont autoexpand the array to fit, or do anything so helpful as to warn you that you’ve gone out of the bounds of your allocated storage. No, C will do what you tell it, and in this case it means overwriting someone else’s data.

This is quite a powerful characteristic and as such requires a programmer to be responsible.

Generally if you have a 20 char string defined but want to store a 25 char string, you need to reallocate memory. You could declare a new array or do a concatenation operation. Either way, this costs CPU time.

So when in PHP you are spamming ‘.’ concat operators everywhere, you can appreciate that PHP is doing a lot of memory re-allocation under the hood to provide that syntactic sugar.

As mentioned above, PHP was originally intended to be a purely templating language for C web applications. Which is where PHP modules / extensions come in. These originally were where your business logic was to go. PHP extensions are compiled to native code, and as such run FAST. You can then access these functions in PHP just like the other core functions and classes. Yahoo, Facebook, use PHP in this way.

So can you. A great way to dabble in a bit of C is to write your own PHP extension. The benefit of getting your hands dirty in this way is the nice speed boost for the functions in your extension. But the more important benefit is how it can greatly improve your understanding of what PHP does for you under the hood. This lets you make informed design choices when writing your regular PHP code.

As a starting point, I can thoroughly recommend starting out with the Zend engine hacker’s guide on the main PHP site. Becoming familar with the structure of the internals is the first step to appreciating all the of the magic PHP does for you under the hood.

Tags: C PHP