We’ll create a shared library liba.so from a.cpp using the following command:
g++ -fPIC a.cpp -c -o a.o && g++ -shared a.o -o liba.so The above command creates an object file a.o having Position Independent Code (PIC), and then it creates a shared library liba.so
Then we have the file b.cpp. This is linked with liba.so using the following command:
g++ -o exec b.cpp -la -L. -Wl,-rpath,.This creates an executable exec with the source file b.cpp. Further details:
- The command
-lalinksliba.so -L.adds the current folder to library path-Wl,-rpath,.sets the RPATH to current directory