Archive

Archive for the ‘Labs’ Category

Create Static Libraries using GCC

May 22nd, 2010

Static libraries provide a mechanism by which we can “pack” object code into reusable libraries. In this article I’m going to focus on creating static libraries of C (and C++) objects and functions. Let’s get started.

Creating a static library is actually a very easy process that consists in just two simple steps:

  1. Compiling source code into object code.
  2. Archiving all the generated object code into a single file that represents the library.

Assume we have the following C source files that declare a 3-element vector type and a dot function that calculates the dot product among two vectors:

/** algebra.h **/

#ifndef ALGEBRA_H
#define ALGEBRA_H

typedef struct
{
	float x, y, z;
} Vec3;

void vec3Init(Vec3* v, float x, float y, float z);

float dot(Vec3* a, Vec3* b);

#endif //ALGEBRA_H
/** algebra.c **/

#include "algebra.h"

void vec3Init(Vec3* v, float x, float y, float z)
{
	v->x = x;
	v->y = y;
	v->z = z;
}

float dot(Vec3* a, Vec3* b)
{
	return a->x * b->x + a->y * b->y + a->z * b->z;
}

We can use the following two commands to create a static library:

gcc -static -c algebra.c -o algebra.o
ar -rcs libalgebra.a algebra.o

Please notice we have named our static library “libalgebra.a”. This is not just a random choice as a static library’s name must begin with “lib” and end with “.a” in order to be able to have GCC link new programs against it.

Now that our library is ready, we can redistribute it with the header file “algebra.h” so other programmers can use it in their applications.

In order to illustrate this point further, let’s write a simple C program that includes the header and links against our library:

#include <stdio.h> /* printf */
#include "algebra.h" /* our library's header */

int main()
{
	Vec3 a;
	Vec3 b;
	vec3Init(&a, 1.0f, 2.0f, 3.0f);
	vec3Init(&b, 3.0f, 2.0f, 1.0f);
	printf("<a,b> = %.2f\n", dot(&a,&b));
}

In the above example you can notice how we can liberally use data types and functions declared in our library as if they where part of this very same program.

Now, in order to compile, all we need is the “algebra.h” header, however, in order to have this program link, we have to use the “-l” (lowercase L) flag of the GCC compiler to specify our shared library, like so:

gcc main.c -L. -lalgebra
$ ./a.out
<a,b> = 10.00

Given the “-lalgebra” flag, GCC will look for a file called “libalgebra.a” (hence the importance in the library’s naming convention). The “-L” flag tells GCC where this file is located.

  • Share/Bookmark

C++, Labs, Programming

How to Create Shared Objects under Mac OS X

February 9th, 2010

Some time ago a friend from Colombia asked me how to create a Shared Object under Mac OS X in order to be able to use it from a Python application (via ctypes).

Although the compiler most commonly used on Mac OS X is the GCC compiler, the version supplied by Apple has a handful of modifications which are specific for OS X. In particular, the -shared compiler flag, used to create Shared Objects under GNU/Linux based systems, has no effect.

In spite of this, Apple did introduce its own mechanism for creating Shared Objects along its GCC extensions. Assuming we have a file called mylibrary.c, which contains the source code for our library, we should invoke GCC like so:

gcc -dynamiclib mylibrary.c -o mylibrary.dylib

If the compiling process succeeds, this command should create a file called mylibrary.dylib, which can now be dynamically linked from other applications.

Unlike Linux based Operating Systems, Mac OS X seems to automatically add the current working directory to the DYLD_LIBRARY_PATH environment variable (an equivalent to Linux’s own LD_LIBRARY_PATH), thus it should not be required to modify this variable in order to have our applications link against the newly created library at runtime, unless, of course, we wanted to move the Shared Object to a directory different from where our process will be executing.

Finally, it should be taken into account that this mechanism is just a simplified example. A correct way to manage the library creation process would be by means of make or equivalent software construction applications, which provide several advantages to software developers.

  • Share/Bookmark

Labs, Programming

How to turn an Ubuntu desktop into a convenient Linux-based server

February 9th, 2010

If you have a desktop computer and a laptop chances are that, in the long run, you end up spending more time working on your laptop than on your desktop. If repurposing your old desktop into a fully fledged server seems like too much of a commitment (either because you do not have the time to set it up or do not want to give up your desktop), you can still configure it as to offer services to other computers on your network without having to go for a server operating system.

Accessing a computer from your local network usually just means assigning an static IP address to your desktop and have it though that IP address every time. The peculiarity with desktop Linux distributions -such as Ubuntu or Fedora- is that they now ship with an utility called NetworkManager which tries to make it “simple” to manage networks. The problem with NetworkManager is that, in my opinion, it was designed mostly for a mobile scenario, where a laptop moves around and tries to join different networks along the way, but this is rarely the case for a desktop computer, which just connects to the same network every time. Furthermore, depending on your router configuration, it may prove difficult to impossible to have a NetworkManager-enabled desktop PC to have the same IP address assigned every time the computer joins the DHCP network.

In this post we will present a method for completely disabling NetworkManager from your Ubuntu desktop system and, using the old-school Linux networking facilities, have your desktop computer’s network connection set up your way, offering services to other computer without sacrificing Internet access.

Read more…

  • Share/Bookmark

Labs, Server Config