Monday, September 26, 2011

ITK and C++11

Dacap made me aware of the new C++11 standard and the supported functionality in GCC 4.6.1. Right after that, I downloaded GCC 4.6.1 and compiled it to try it with the Insight Tool-kit (ITK), particularly because of all those super-templated types in ITK that make coding longer than what it should take, mostly because of the amount of typedefs and re-typing of class names. One of the 'wonders' of C++11 is the auto keyword, which happens to be close to a salvation angel for ITK users.

My main concern was that C++11's implementation in GCC is still quite new, such that incompatibilities may appear almost instantly; and that was the case with a particular GCC extension that -std=c++0x was considering invalid. Therefore, when compiling ITK-based code you may find 'constexpr' errors if you enable c++0x support, particulary in the file vnl.h. Fortunately, a patch has already been created and published here and can be applied directly to the 4.6.1 release source code.

To compile it with Ubuntu you can follow the instructions given here. In my case I didn't want to overwrite the previous gcc compiler in my system, nor I hadn't administrative rights, so I used make DESTDIR=/wherever/you/want install . This way I just had to modify the PATH environment variable to point to the gcc 4.6.1 binaries and tell CMake to use g++-4.6 as the compiler rather than the standard gcc one.

The patch mentioned earlier doesn't disable the constexpr error by itself; for that to be effective one must use the -fpermissive compiler flag. Some other compatibility problems are described here.

Moreover, it is very likely to run on another error regarding an include file missing. To obtain a successful build of ITK my CXX flags were set to: -std=c++0x -fpermissive -include cstddef

UPDATE: a better practice is to compile GMP, MPFR and other libs as static before compiling GCC, so that they are statically linked and the generated compiler binaries can be executed in other machines that may not have those specific libraries installed. Instructions for compiling these libraries and GCC can be found here. My particular configure line for GCC 4.6.1 is the following:

configure --prefix=/usr \
--enable-languages=c,c++,fortran \
--enable-threads=posix \
--enable-tls \
--enable-libgomp \
--enable-lto \
--disable-nls \
--disable-checking \
--disable-multilib \
--with-gmp=/tmp/gcc \
--with-mpfr=/tmp/gcc \
--with-mpc=/tmp/gcc \
--with-libelf=/tmp/gcc \
--with-fpmath=sse

No comments:

Post a Comment