Compiling programs

  • See cpp
  • If required, need to install build-essential meta package for Debian/Ubuntu
    • gcc, g++
    • make
    • dpkg-dev meta package
      • dpkg-buildpackage
      • dpkg-source etc.
    • libc6-dev: The standard C library headers and development libraries

Source code

  • single user: ~/src
  • multiple users: /usr/local/src
  • installed by distribution: /usr/src

Header files

  • Inside the project
    • #include "getopt.h"
  • Outside the project: /usr/include
    • #include <stdio.h>

Building the program using Autotools

  • See Autotools
  • autoreconf generates configure script
autoreconf -i
  • configure shell script is present in the source code
    • analyzes the build environment
    • checks if necessary external tools and components are installed
    • creates Makefile file
./configure
  • make will read Makefile and generate targets in current directory
    • running make again will only build targets which are changed
    • This creates executable binaries
make
  • Install the program in the system
    • default location is /usr/local
    • requires sudo privileges
sudo make install
  • Uninstall the program if supported
sudo make uninstall

Build and install using configure script

./configure
make
make install

Install program for current user

./configure --prefix=$HOME/.local
make
make install

Building using Autotools

./autogen.sh
./configure
make
sudo make install

Install from package source

  • https://www.cyberciti.biz/ref/apt-dpkg-ref.html
  • apt install
    • installs the binary packages directly without compiling from source
  • apt source
    • looks for source via deb-src in apt config
    • downloads in the current directory
      • *.orig.tar.gz (original source archive)
      • *.debian.tar.xz (Debian component file)
        • modifications/patches/build instructions
        • contains debian folder
      • *.dsc (Description file)
    • extracts the source code archive
      • using *.dsc package metadata, the other two archives are extracted and patches are applied to get final source code
  • apt build-dep
    • reads Build-Depends from debian/control
  • dpkg-buildpackage
    • generates debian package .deb for installation
  • use cases
    • control source code and make changes if necessary before installing
# get Debian source package
apt source <package>
 
# get inside extracted package folder
cd <package-source-dir>
 
# install build dependencies from Build-Depends
sudo apt build-dep .
 
# build .deb binaries from source tree
# .deb package is generated at parent folder
dpkg-buildpackage -us -uc
 
# install produced .deb with dependency resolution
cd ..
sudo apt install ./<binary>.deb