Posts Tagged ‘linux’

Remove old linux kernels

Posted: January 12, 2013 in System Administration
Tags: ,

When upgrading linux kernel, Ubuntu does not automatically remove previous version of the kernel. Yup, it is good if you keep the previous version, so that if anything crashes (eg: your driver is incompatible with the new kernel), you may revert to the old kernel. However, sometimes the old kernels may accumulate and fill up your disk space. Therefore, if you are running fine on your current kernel, you can choose to remove the old ones to free up some disk space. Here’s how you can do it.

  1. Launch your preferred package or software manager. The new Ubuntu ships with Ubuntu Software Center, so you may use that.
  2. Under installed software, search for linux-image.
  3. Remove all kernels other than your current version. (You may find out your kernel version by executing “uname -r” on the terminal.) Using the screenshot below as example, the current kernel version is 3.2.0-35-generic-pae. So we will remove other kernels, which are 3.2.0-23-generic-pae, 3.2.0-24-generic-pae, and 3.2.0-29-generic-pae. The package ‘Generic Linux kernel image’ needs not be removed because it is just a meta-package to pull in the latest kernel image.
  4. Ubuntu Software CenterThen, under installed software, search for linux-headers.
  5. Do the same as step 3. Remove previous version of the linux headers.
  6. Remove GRUB entries for older kernels.

This post will describe on how to enable static and dynamic linking on gcc (GNU Compiler Collection).

By default, a library is dynamically linked, if the -l option is used to include the library.

For example, the code below will dynamically link somelib as well as libc, libstdc++, etc.

g++ file.cpp -o a.out -l somelib

Meanwhile, if the -static specifier is used, all libraries will be linked statically.

g++ file.cpp -o a.out -l somelib -static

Mixing between linking static and dynamic libraries is also possible; and there are two ways.

  1. Specify the full name of the static lib.
    • For example:

       g++ file.cpp -o a.out libsome.a -lsomelib2

    • By this way, the library libsome.a will be linked statically along with other object files. Meanwhile, somelib2 and the standard c libraries will remain dynamic.
  2. Pass additional options on to the linker.
    • The -Wl,<options> switch is used to pass on the options to the linker.
    • For example:

       g++ file.cpp -o a.out -Wl,-Bstatic -lsome -Wl,-Bdynamic -lsomelib2

    • The command above performs the same linking as method 1, as the -Wl,-Bstatic switch sets the linker in static-linking mode, and the -Wl,-Bdynamic switch sets the linker back to dynamic-linking mode.

Linking between static and shared library is just as simple as this. Hope this tutorial might help anyone encountering this problem.