Friday, 28 August 2020

Shared Libraries with GCC on linux

 

  •  Create an .so file and run that file, link the .so from the same path to the executable.

Program:

Program file 1 Header file  (foo.h):

/* Shared Libraries with GCC on linux
 * Check : http://velrajcoding.blogspot.in
 */
#ifndef foo_h__
#define foo_h__

extern void foo(void);

#endif  // foo_h__

Program file 2 foo.c:
 
/* Shared Libraries with GCC on linux
 * Check : http://velrajcoding.blogspot.in
 */

#include <stdio.h>


void foo(void)
{
        printf("Hello, I am a shared library");
}

Program file 3 Main file (main.h):
/* Shared Libraries with GCC on linux
 * Check : http://velrajcoding.blogspot.in
 */
#include <stdio.h>
#include "foo.h"

int main(void)
{
    puts("This is a shared library test...");
    foo();
    return 0;
}

Compile & execute:

Step 1: Compiling with Position Independent Code

[so_crete]$ ls
foo.c foo.h main.c
[so_crete]$
[so_crete]$ gcc -c -Wall -Werror -fpic foo.c
[so_crete]$ ls
foo.c foo.h foo.o main.c
[so_crete]$

Step 2: Creating a shared library from an object file

[so_crete]$ gcc -shared -o libfoo.so foo.o
[so_crete]$ ls
foo.c foo.h foo.o libfoo.so main.c
[so_crete]$

Step 3: Linking with a shared library

[so_crete]$ gcc -Wall -o test main.c -lfoo
/bin/ld: cannot find -lfoo
collect2: error: ld returned 1 exit status
[so_crete]$

Tell GCC where to find the shared library
[so_crete]$ pwd
/users/vkutrala/vel/sample/so_crete
[so_crete]$ gcc -L /users/vkutrala/vel/sample/so_crete -Wall -o test main.c -lfoo
[so_crete]$ ls
foo.c foo.h foo.o libfoo.so main.c test
[so_crete]$

Step 4: Making the library available at runtime

[so_crete]$ ./test
./test: error while loading shared libraries: libfoo.so: cannot open shared object file: No such file or directory
[so_crete]$

Using LD_LIBRARY_PATH

[so_crete]$ echo $LD_LIBRARY_PATH

[so_crete]$ LD_LIBRARY_PATH=/users/vkutrala/vel/sample/so_crete:$LD_LIBRARY_PATH
[so_crete]$ ./test
./test: error while loading shared libraries: libfoo.so: cannot open shared object file: No such file or directory
[so_crete]$
[so_crete]$
[so_crete]$
What happened? Our directory is in LD_LIBRARY_PATH, but we did not export it. In Linux, if you do not export the changes to an environment variable, they will not be inherited by the child processes
[so_crete]$ export LD_LIBRARY_PATH=/users/vkutrala/vel/sample/so_crete:$LD_LIBRARY_PATH
[so_crete]$ ./test
This is a shared library test...
Hello, I am a shared library
[so_crete]$

Reference:
     https://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

 

Read About Dynamic loading lib(run time load or patch update)

Friday, 31 July 2020

python: how to use pexpect without installing

We can use the pexpect without installing, below is the best way to use:

  1. Downloaded Pexpect 2.4 ( https://pypi.org/project/pexpect/2.4/#files).
    • We tested on python 2.7.10 version, need to check in latest python version, not sure this will work on latest.
    • Also seems not support in latest pexpect version because latest version doesn't have pexpect.py file iteself. so best use pexpect 2.4 version. 
       2. Place pexpect.py next to my script
       3. import pexpect in my script.


It will work.