Skip to content

Instantly share code, notes, and snippets.

@NotSoIntelligent
Last active July 18, 2024 07:33
Show Gist options
  • Select an option

  • Save NotSoIntelligent/5ea0fcef440627f388514c3a32b73166 to your computer and use it in GitHub Desktop.

Select an option

Save NotSoIntelligent/5ea0fcef440627f388514c3a32b73166 to your computer and use it in GitHub Desktop.
Creating a C Program with a Separate Add Function as a Shared Library

Creating a C Program with a Separate Add Function as a Shared Library

In this article, we will guide you through the process of creating a C program that adds two numbers, with the addition functionality implemented in a separate file. We will compile the addition function as a shared library and link it to the main program.

Step 1: Create the Main Program

First, we need to create the main program that will use the Add function to add two numbers. Save the following code in a file named main.c:

#include <stdio.h>
#include "add.h"

int main() {
    int num1, num2, sum;

    printf("Enter two integers: ");
    scanf("%d %d", &num1, &num2);

    sum = Add(num1, num2);
    printf("Sum: %d\n", sum);

    return 0;
}

Step 2: Create the Addition Function

Next, we create the file that contains the implementation of the Add function. Save the following code in a file named add.c:

int Add(int a, int b) {
    return a + b;
}

We also need a header file to declare the Add function so it can be used in main.c. Save the following code in a file named add.h:

#ifndef ADD_H
#define ADD_H

int Add(int a, int b);

#endif

Step 3: Compile the Addition Function as a Shared Library

We will compile add.c into a shared object file (.so). Open a terminal (or command prompt) and navigate to the directory where the files are saved. Then, run the following commands:

gcc -fPIC -c add.c -o add.o
gcc -shared -o libadd.so add.o
  • The -fPIC option generates position-independent code, which is required for shared libraries.
  • The -shared option creates a shared object file from the compiled object file.

Step 4: Compile the Main Program and Link it with the Shared Library

Now, we need to compile main.c and link it with the shared library we created. Run the following command:

gcc -o add_numbers main.c -L. -ladd
  • The -L. option tells the compiler to look in the current directory for libraries.
  • The -ladd option tells the compiler to link with the library named libadd.so.

Step 5: Set the Library Path and Run the Program

To ensure the shared library can be found when running the program, you may need to set the LD_LIBRARY_PATH environment variable. Run the following command:

export LD_LIBRARY_PATH=.

Finally, run the compiled program:

./add_numbers

The program will prompt you to enter two integers and then output their sum.

int Add(int a, int b) {
return a + b;
}
#ifndef ADD_H
#define ADD_H
int Add(int a, int b);
#endif
#include <stdio.h>
#include "add.h"
int main() {
int num1, num2, sum;
printf("Enter two integers: ");
scanf("%d %d", &num1, &num2);
sum = Add(num1, num2);
printf("Sum: %d\n", sum);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment