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,


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


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 -o -lc libadd.o
ln -sf

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


#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 but 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 $ROOTFS/lib/

This time the executable should run fine.


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