Here is a simple code for shared library (.so) and an executable that uses it:
Library Source files (libhello.c and libhello.h):
Source for the executable that uses the above library (main.c):
sudo apt-get install build-essential git
Manual build:
(Reference: http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html)
Place the three source files in the same directory. Open the command prompt, switch to the same directory and execute the following commands to compile, execute and clean:
Place the three source files in the same directory. Open the command prompt, switch to the same directory and execute the following commands to compile, execute and clean:
- Compilation
- Compiling the library sources into object files with Position Independent Code:
gcc -c -Wall -Werror -fpic libhello.c - Creating a shared library from an object file
gcc -shared -o libhello.so libhello.o - Linking with a shared library
(prefix "lib" will be automatically added to library name, -L is used to specify search folder)
gcc -L$(pwd) -Wall -o main.exe main.c -lhello
if there are any issues, use the command to debug linker: ld -lhello --verbose - Running the executable
- Making the library available at runtime
export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH - Run the binary
./main.exe - Cleaning
- Execute:
rm -rf *.o *.so *.exe
Building using Makefile:
Place the three source files in the same directory. Also create and place a new file "Makefile" with the following contents in the same directory:
Open the command prompt, switch to the same directory and execute the following commands to compile, execute and clean:
- Compilation
- make all
- Running the executable
- Making the library available at runtime
export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH
- Run the binary
./main.exe
- Cleaning
- make clean
Place the three source files in the same directory. Also create and place a new file "Makefile" with the following contents in the same directory:
Open the command prompt, switch to the same directory and execute the following commands to compile, execute and clean:
- Compilation
- make all
- Running the executable
- Making the library available at runtime
export LD_LIBRARY_PATH=$(pwd):$LD_LIBRARY_PATH - Run the binary
./main.exe - Cleaning
- make clean
Building using Bazel:
To use Bazel (The build system from Google), you need to first install it. Do so using following commands:
- Install JDK 8
- sudo apt-get install openjdk-8-jdk
- Add Bazel distribution URI as a package source (one time setup):
- echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
- curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
- Install and update Bazel
- sudo apt-get update && sudo apt-get install bazel
Next, create a directory structure like so:
Current folder
├────WORKSPACE (Blank file)
│
└───main (folder)
├───BUILD (Bazel build script)
├───libhello.c
├───libhello.h
└───main.c
The content of the BUILD file should be as follows:
Now carry out the following commands to build the file using Bazel from the command prompt after changing to the directory with blank "WORKSPACE" file:
- Compilation
- bazel build //main:all
- Running the executable
- ./bazel-bin/main/main.exe
- Cleaning
- bazel clean

Compiling, running and cleaning using Bazel

Output files created in the working directory by Bazel
To use Bazel (The build system from Google), you need to first install it. Do so using following commands:
- Install JDK 8
- sudo apt-get install openjdk-8-jdk
- Add Bazel distribution URI as a package source (one time setup):
- echo "deb [arch=amd64] http://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
- curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
- Install and update Bazel
- sudo apt-get update && sudo apt-get install bazel
Next, create a directory structure like so:
├────WORKSPACE (Blank file)
│
└───main (folder)
├───BUILD (Bazel build script)
├───libhello.c
├───libhello.h
└───main.c
Now carry out the following commands to build the file using Bazel from the command prompt after changing to the directory with blank "WORKSPACE" file:
- Compilation
- bazel build //main:all
- Running the executable
- ./bazel-bin/main/main.exe
- Cleaning
- bazel clean
![]() |
Compiling, running and cleaning using Bazel |
![]() |
Output files created in the working directory by Bazel |
Comments
Post a Comment