How Do You Create Your Own RPM Package (Binary File) for Your Own C Program?

Problem scenario
You want to know more about RPM packaging so you can create your own .rpm file (a custom RPM package file).  In other words you want to create your own RPM package (a binary file).  You want it for installing an uncompiled C program you wrote.  The uncompiled C program is one .c file and very simple.  You don't know how to use a spec (specification) file to create your own RPM package file.  You don't know how to use a Makefile to install the C program.  How do you create an rpm package for your own program?  More specifically how do you create your own stand-alone RPM file that will install an executable C program from a source (.c) file?

1.  Log into the Red Hat server as any user except root.

2.  Run this command:
sudo yum -y install gcc rpm-build rpm-devel rpmlint make python bash coreutils diffutils patch rpmdevtools

3.  Run this command:  mkdir /tmp/contint-1.0

4.  Run this command:  cd /tmp/contint-1.0

5.  Create a C program named contint.c with this as the content (the first line is "#include <stdio.h>"):

#include <stdio.h>

int main(void) {
    printf(" says Hello!\n");
    return 0;

6.a.  Create a file named "Makefile" in the same directory as the contint.c.  The Makefile should have the following content, but remember that indented, non-blank lines need to be indented pressing the Tab key before the text on the right:

        gcc -o contint contint.c 
        rm contint
        mkdir -p $(DESTDIR)/usr/bin
        install -m 0755 contint $(DESTDIR)/usr/bin/contint

6.b.  Clarification: The step above will not work if you just copy and paste the content; you can copy and paste it, but re-indent the indented lines using the Tab key.

7.  Create a LICENSE file in the same directory as the two files above.  The LICENSE file should have the following content:

This is just a test.  For information about real GNU licenses, see this link:

8.  Run these several commands:  

cd /tmp/
tar -cvzf contint-1.0.tar.gz contint-1.0
cd ~
mv /tmp/contint-1.0.tar.gz ~/rpmbuild/SOURCES/
cd /tmp/contint-1.0
cp contint.c contint.c.orig
diff -Naur contint.c.orig contint.c > ~/rpmbuild/SOURCES/contint-output-first-patch.patch

9.  Create contint.spec in ~/rpmbuild/SPECS/ with the content below:

Name:           contint
Version:        1.0
Release:        1%{?dist}
Summary:        A Hello World type example using the C programming language

License:        MadeUpLicense
URL:  {name}

Patch0:         contint-output-first-patch.patch

BuildRequires:  gcc
BuildRequires:  make

The verbose description for this Continual Integration example.
You can put lines here.

%setup -q


make %{?_smp_mflags}


%license LICENSE

* Wed Nov 29 2017 Continual Integration <> - 1.0-1
- A basic contint package

10.  Run this command:   rpmbuild -bb ~/rpmbuild/SPECS/contint.spec

11.   Now you should have an .rpm file in this directory: ~/rpmbuild/RPMS/x86_64/.  If you did not change any of the above directions, the file will be called "contint-1.0-1.el7.x86_64.rpm".  Test installing it by running these four commands:

contint # This command should fail
cd ~/rpmbuild/RPMS/x86_64/
sudo rpm -i contint-1.0-1.el7.x86_64.rpm # *
contint  # This command should work
# * This command could work if you do not want to use "rpm" to install it:
# sudo yum localinstall /path/to/contint-1.0-1.el7.x86_64.rpm

12.  The .rpm can now be used on other servers running a RedHat distribution of Linux.  If you want to learn more about creating .spec files and .rpm packages, see this link.

Leave a comment

Your email address will not be published. Required fields are marked *