64 bitのUbuntu Linuxで32 bitの実行ファイルを動かす方法のメモ

環境

Ubuntu 14.04.4 LTS 64bit版

$ uname -a
Linux vm-ubuntu64 3.19.0-25-generic #26~14.04.1-Ubuntu SMP Fri Jul 24 21:16:20 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 14.04.4 LTS
Release:        14.04
Codename:       trusty

$ gcc --version
gcc (Ubuntu 4.8.4-2ubuntu1~14.04.1) 4.8.4

32 bitの実行ファイルを動かす

以下のパッケージをインストールすればよい。

$ sudo apt-get install gcc-multilib g++-multilib

実際にコンパイルして動かしてみる。

$ cat hello.c
#include <stdio.h>

int main()
{
    puts("Hello, World!");
    return 0;
}

$ gcc -m32 hello.c -o hello

$ file hello
hello: ELF 32-bit LSB  executable, Intel 80386, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.24, BuildID[sha1]=5dff36fa3ffeddf01e261047235e02bf2935bb5b, not stripped

$ ./hello
Hello, World!