Install gflags

gflags, commandline flags module for C++.

Environment

Install gflags.

Download gflags-2.0.tar.gz

$ tar zxvf gflags-2.0.tar.gz
$ cd gflags-2.0
$ ./configure
$ make
$ sudo make install
$ sudo ldconfig
$ cd ..
$ rm -rf gflags-2.0

Code

// gflags_test.cc

#include <iostream>
#include <gflags/gflags.h>

DEFINE_bool(my_bool, false, "bool type");
DEFINE_int32(my_int32, 123, "int32 type");
DEFINE_int64(my_int64, 1245L, "int64 type");
DEFINE_uint64(my_uint64, 12345L, "uint64 type");
DEFINE_double(my_double, 1.234L, "double type");
DEFINE_string(my_string, "init", "string type");

int main(int argc, char** argv) {
  google::ParseCommandLineFlags(&argc, &argv, true);
  std::cout << "my_bool: " << FLAGS_my_bool << std::endl;
  std::cout << "my_int32: " << FLAGS_my_int32 << std::endl;
  std::cout << "my_int64: " << FLAGS_my_int64 << std::endl;
  std::cout << "my_uint64: " << FLAGS_my_uint64 << std::endl;
  std::cout << "my_double: " << FLAGS_my_double << std::endl;
  std::cout << "my_string: " << FLAGS_my_string << std::endl;                                                                       
  return 0;
}

Build

$ g++ gflags_test.cc -lgflags

Test

$ ./a.out 
my_bool: 0
my_int32: 123
my_int64: 1245
my_uint64: 12345
my_double: 1.234
my_string: init
$ ./a.out --my_bool=true --my_int32=234 --my_int64=2345 --my_uint64=23456 \
> --my_double=2.345 --my_string=new
my_bool: 1
my_int32: 234
my_int64: 2345
my_uint64: 23456
my_double: 2.345
my_string: new

Extending Python with C or C++

Wrapping a C library in Python.

Environment

Install Python.h

$ sudo apt-get install python2.7-dev

Code

// spammodule.c
#include <Python.h>

#ifdef __cplusplus
extern "C" {
#endif

static PyObject* SpamError;

static PyObject* spam_system(PyObject* self, PyObject* args) {
  const char* command;
  int sts;

  if (!PyArg_ParseTuple(args, "s", &command)) {
    return NULL;
  }
  sts = system(command);
  if (sts != 0) {
    PyErr_SetString(SpamError, "System command failed");
    return NULL;
  }
  return PyLong_FromLong(sts);
}

static PyMethodDef SpamMethods[] = {
    {"system", spam_system, METH_VARARGS, "Execute a shell command."},
    {NULL, NULL, 0, NULL}  // Sentinel
};

void initspammodule(void) {
  PyObject* m = Py_InitModule("spammodule", SpamMethods);
  if (m == NULL) {
    return;
  }

  SpamError = PyErr_NewException("spammodule.error", NULL, NULL);
  Py_INCREF(SpamError);
  PyModule_AddObject(m, "error", SpamError);
}

#ifdef __cplusplus
}
#endif

Build

$ gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c spammodule.c -o spammodule.o
$ gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro spammodule.o -o spammodule.so

Test

$ python
>>> import spammodule
>>> spammodule.system('uname')
Linux
0L
>>> spammodule.system('bad command')
sh: 1: bad: not found
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
spammodule.error: System command failed

Install Protocol Buffer

Ubuntu 12.04 64 bits

wget 'http://code.google.com/p/protobuf/downloads/detail?name=protobuf-2.4.1.tar.bz2' -O protobuf-2.4.1.tar.bz2
tar xvf protobuf-2.4.1.tar.bz2
cd protobuf-2.4.1/
./configure
make
make check
sudo make install
cd ..
rm -rf protobuf-2.4.1/

C++

protoc --cpp_out=dst_dir example.proto

Java

protoc --java_out=dst_dir example.proto

Python

protoc --python_out=dst_dir example.proto

Install HPN-SSH

Abstract

Install High Performance SSH/SCP - HPN-SSH 5.9 to Cent OS 5.4 (32bit).

Procedure

Install dependencies
yum install -y patch rpm-build openssl-devel glibc-devel krb5-devel gcc pam-devel
Download and patch openssh
wget http://ftp.jaist.ac.jp/pub/OpenBSD/OpenSSH/portable/openssh-5.9p1.tar.gz
wget http://www.psc.edu/networking/projects/hpn-ssh/openssh-5.9p1-hpn13v12.diff.gz
tar zxvf openssh-5.9p1.tar.gz
cd openssh-5.9p1
zcat ../openssh-5.9p1-hpn13v12.diff.gz | patch
Configure and make package
rm -rf contrib/aix/ contrib/hpux/ contrib/suse/ contrib/caldera/  contrib/solaris/ contrib/cygwin/
#
# # no_x11_askpass 0 -> 1
# # no_gnome_askpass 0 -> 1
# # %doc CREDITS ChangeLog INSTALL LICENCE OVERVIEW README* PROTOCOL* TODO WARNING*
# # ->
# # %doc CREDITS ChangeLog INSTALL LICENCE OVERVIEW README* PROTOCOL* TODO
#
sed -i 's/no_x11_askpass 0/no_x11_askpass 1/g' contrib/redhat/openssh.spec
sed -i 's/no_gnome_askpass 0/no_gnome_askpass 1/g' contrib/redhat/openssh.spec
sed -i 's/TODO WARNING\*/TODO/g' contrib/redhat/openssh.spec
cd ..
rm -f openssh-5.9p1-hpn13v12.diff.gz openssh-5.9p1.tar.gz
tar zcvf openssh-5.9p1.tar.gz openssh-5.9p1/
rpmbuild -tb --clean openssh-5.9p1.tar.gz
Install hpn-ssh
rpm -Uvh /usr/src/redhat/RPMS/i386/openssh-5.9p1-1.i386.rpm
rpm -Uvh /usr/src/redhat/RPMS/i386/openssh-server-5.9p1-1.i386.rpm
rpm -Uvh /usr/src/redhat/RPMS/i386/openssh-clients-5.9p1-1.i386.rpm
Clean up
rm -f /usr/src/redhat/RPMS/i386/openssh-*
rm -rf openssh-5.9p1 openssh-5.9p1.tar.gz
Check
/usr/bin/ssh -V
# OpenSSH_5.9p1-hpn13v12, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
/usr/sbin/sshd -V
# OpenSSH_5.9p1-hpn13v12, OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008
Change sshd_config
NoneEnabled no -> NoneEnabled yes

references