How to compile and run a C program in Linux

PostgreSQL is written in C, so installing PostgreSQL software is nothing but compiling and running all the C-Programs in your source code.

To work with PostgreSQL internals it is highly recommended to learn the basics of C programming.

How to create and compile a C Program on Linux

Create a C program called first.c

To compile this program, enter:

The above command will compile first.c program and creates an executable file called first.o.

To run the first.o just do ./first.o

What is shared library

In programming, a library is an assortment of pre-compiled pieces of code that can be reused in a program.

For instance, if you are building an application that needs to perform math operations, you don’t have to create a new math function for that, you can simply use existing functions in libraries for that programming language.

libc (the standard C library) is one such standard library on Linux ecosystem.

Linux supports two classes of libraries, namely:

  • Static libraries – are bound to a program statically at compile time.
  • Dynamic or shared libraries – are loaded when a program is launched and loaded into memory and binding occurs at run time.

The shared library file can be created by gcc compiler and its extension would be .so

Locating Shared Libraries in Linux

A program can call a library using its library name or filename, and a library path stores directories where libraries can be found in the filesystem. By default, libraries are located in /usr/local/lib/usr/local/lib64/usr/lib and /usr/lib64; system startup libraries are in /lib and /lib64. Programmers can, however, install libraries in custom locations.

The default location of PostgreSQL shared libraries/Shared objects is <install_directory>/lib. For version 1o it is /usr/pgsql-10/lib

To set it temporarily, use the LD_LIBRARY_PATH environment variable on the command line. If you want to keep the changes permanent, then add this line in the shell initialization file /etc/profile (global) or ~/.profile (user specific).

How to create a shared object on Linux

Now that we understand a library file, we now going to create a shared object for the above C Program with the following command.

gcc -shared -o first.so -fPIC first.c

Now, a shared object file called first.so is created.

To run this file execute below command

Here, it created the a.out file, run ./a.out to get the output.

As we discussed just now, a shared library can now be executed by setting the environment.

What is shared_preload_libraries in PostgreSQL?

Now this dynamic library shared object can be loaded into PostgreSQL by below techniques.

  • From shared_preload_libraries. This loads at postmaster start time, so the extension can register shared memory, locks, hooks etc before we start forking backends.
  • With session_preload_libraries. This loads after fork(), in a backend, so it can’t register shmem etc.
  • With local_preload_libraries. Like, but limited to the plugins directory and settable by normal users.
  • With the LOAD a statement, or implicitly by running a function that refers to a LANGUAGE c implementation.

The Extensions created in PostgreSQL are mostly created as dynamic library shared objects, so when create extension is executed, the shared object is loaded and called.

Depending on the extension usage, it may need to be added in shared_preload_libraries in postgresql.conf and few extension can be loaded normally.

For example., pg_stat_statements must be loaded by adding it to shared_preload_libraries in postgresql.conf, because it requires additional shared memory. This means that a server restart is needed to add or remove the module.

How to create a simple function in PostgreSQL

The following function adds 10 to the given value.

The above function accepts the integer and adds 10 to that and displays it as output.

The above is pre-requisite for understanding PostgreSQL Extensions in PostgreSQL.

Please continue to learn PostgreSQL extensions.

 

Words from postgreshelp

Thank you for giving your valuable time to read the above information. I hope the content served your purpose in reaching out to the blog.
Suggestions for improvement of the blog are highly appreciable. Please contact us for any information/suggestions/feedback.

If you want to be updated with all our articles

please follow us on Facebook Twitter
Please subscribe to our newsletter.

 

Leave a Reply