simple C systems development in Ubuntu

Edouard Tavinor
1 min readApr 10, 2021

Hi everybody!

Yesterday I decided to look into simple C development on Linux. In particular I wanted to find out how to write a systems library so I could use it in my C projects. This is what I did:

Firstly, I started a new virtual machine, because I didn’t want to break anything on my workstation :)

In the virtual machine I installed sudo apt-get install build-essential

Then I wrote the following 3 files:

first, the header file with a function definition: times2.h

#ifndef TIMES2_H
#define TIMES2_H
extern int times2(int);#endif

and then the implementation of the header: times2.c

#include <times2.h>int times2(int i) {
return i+i;
}

and finally a file which uses times2.h called uses_times2.c

#include <stdio.h>
#include <times2.h>
int main(int argc, char **argv) {
int i = 3;
printf("%d times2: %d\n", i, times2(i));
return 0;
}

Then I moved the header file times2.h to /usr/local/include. gcc on Ubuntu will look here for header files. After that I built the implementing code as follows:

gcc -shared -o libtimes2.so -fPIC times2.c

and moved the compiled library to /usr/local/lib. After that I ran sudo ldconfig to make the system aware of the new library. This meant that I could now build my program:

gcc -o uses_times2 uses_times2.c -ltimes2

and run it:

./uses_times2 
3 times2: 6

And that’s it :) Maybe this helps someone :)

--

--