Category: Programming

Quick summary on shared libraries

Shared libraries are reusable libraries which can be used by multiple applications at the same time. The advantages of shared libraries are

  1. Can be loaded at application start or on demand at run time.
  2. Can be upgraded separately and reloaded.

Only disadvantage compared to statically linked library is, there is an overhead for loading the library and resolving the symbols at run time.

In case of loading during application start, the loader will load the library into process virtual address space and resolve before the application is launched.

In case of loading dynamically the same can be achieved with the respective system library apis when in need..

Windows it will be a dynamically linked library(*.dll) and uses the same PE format as windows executable.

Linux it will be a shared object(*.so) and uses the ELF format as a normal linux executable.

In linux use the -shared gcc option to create a shared object. if your library name provided is abc.so, the linux linker will create it as libabc.so.

While statically linking, the option -labc should be provided to link with the same.

If you are using dynamic linking, you need to use dlopen(3) ,dlsym(3) and dlclose(3) apis to load, access the symbol and unload the library.

Sample codes

application.c

#include <stdio.h>
#include “call_lib.h”

int main(int argc, char * argv[])
{
printf(“In application\n”);
call_lib(3);

return 0;
}

call_lib.h

int call_lib(int num);

library.c

#include <stdio.h>
#include “call_lib.h”

int call_lib(int num)
{
printf(“passed value is %d\n”, num);

return 0;
}

To compile the library code
gcc -shared -fPIC -o library.so library.c

To compile the application code
gcc -o sample -L /home/yogi/libs -lrary application.c

Running the executable

set first the library path

$export LD_LIBRARY_PATH=/home/yogi/libs:$LD_LIBRARY_PATH
$ ./sample
In application
passed value is 3

Source: Working with Shared Libraries | Set 1 – GeeksforGeeks

Interesting Facts about Macros and Preprocessors in C – GeeksforGeeks

One of the phase in compilation is pre processor replacement. In C programming there are multiple pre processor directives. The common one are include and define. There could be other directives like pragma and others which are compiler supported ones and may not be supported in all the compilers.

#include directive can be used to include the header files. If the header files are included <> brackets, the header file will be searched in the standard include paths mentioned as part of compilation with -I option. If the header files are used within double quotes “”, the current path will be used to search first before searching the standard paths.

#define will replace all the tokens with the values defined for the token.

Source: Interesting Facts about Macros and Preprocessors in C – GeeksforGeeks

Single instance in Linux applications

Sometimes you may need to write an application in lnux for which multiple instances are not allowed. There is a simple way to achieve even though you have lot of other ways like semaphores, sockets etc to achieve the same. Here is a small snippet code to achieve the same using file locking.

#include <sys/file.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

 

int pid_file = -1; int rc = -1;

pid_file = open(“/var/run/whatever.pid”, O_CREAT | O_RDWR, 0666);

rc = flock(pid_file, LOCK_EX | LOCK_NB);

if(rc) {

if(EWOULDBLOCK == errno)     {

close(pid_file); // another instance is running

return;

}

}

else {

// this is the first instance

}