cross compiling custom shared libraries for linux and ARM

Install the cross compiler tool chain, If you haven’t. This is our actual shared library source code,

libadd.c

#include "libadd.h"
int ladd(int a, int b)
{
	return (a + b);
}

libadd.h

int ladd(int , int );

Compile these files into dynamically linked shared library file(.so).

arm-linux-gcc -fPIC -c libadd.c
arm-linux-ld -shared -soname libadd.so.1 -o libadd.so.1.0 -lc libadd.o
ln -sf libadd.so.1.0 libadd.so

We have successfully created a shared library. Its time to compile our application using this shared library,

main.c

#include "stdio.h"
void main()
{
 int a, b;
 a = 10;
 b = 20;
 printf("%dn", ladd(a, b));
}

To compile this application using our shared library,

arm-linux-gcc -o main main.c -L. -ladd

Copy the executable to your root file system and try to execute it. It will show an error that the shared libraries are missing. To know which libraries the executable needs,

arm-linux-objdump -x main | grep NEEDED

It will show you a list of libraries the executable requires. You will notice that the library file required is not libadd.so.1.0 but libadd.so.1. It is because of the -soname flag we specified while compiling the shared library. Now copy the library file to /lib/ directory of your root file system.

ln -sf libadd.so.1.0 $ROOTFS/lib/libadd.so.1

This time the executable should run fine.

tejainece

Ravi Teja is a technology entrepreneur with interest in Embedded Systems, Software engineering, Operating Systems, Web development, Cloud computing, Machine learning, etc.

More Posts - Website

Follow Me:
LinkedInGoogle Plus