LoginSignup
5
2

More than 5 years have passed since last update.

Travis CI で C++11 な Python モジュールをテストするときの設定

Last updated at Posted at 2015-10-11

Cythonで作ったPythonモジュールをTravis CIでテストしようとしたら、以下のような感じでインストールでこけた。

$ python setup.py build install
running build
running build_ext
building 'neologdn' extension
creating build
creating build/temp.linux-x86_64-2.6
gcc -pthread -fno-strict-aliasing -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/opt/python/2.6.9/include/python2.6 -c neologdn.cpp -o build/temp.linux-x86_64-2.6/neologdn.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for Ada/C/ObjC but not for C++ [enabled by default]
In file included from /usr/include/c++/4.6/unordered_map:35:0,
                 from neologdn.cpp:255:
/usr/include/c++/4.6/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
neologdn.cpp:663:8: error: ‘unordered_map’ in namespace ‘std’ does not name a type
neologdn.cpp:666:8: error: ‘unordered_set’ in namespace ‘std’ does not name a type

Travis CIのgccのバージョン(4.6)が古くてC++11に対応してないため、std::unordered_mapstd::unordered_set がないのが原因。
.travis.yml で gcc と g++ をアップグレードして CFLAGS を設定してあげよう。

install:
  - sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
  - sudo apt-get update -qq
  - sudo apt-get install -qq gcc-4.8
  - sudo apt-get install -qq g++-4.8
  - export CC="gcc-4.8"
  - export CXX="g++-4.8"
  - export CFLAGS=-std=c++11
  - export CXXFLAGS=-std=c++11
  - python setup.py build install

そして setup.py でC++11だよって明示的に指定するとTravisだけじゃなくてみんなしあわせになれます。

setup(
...
    ext_modules=[Extension('hoge', ['hoge.cpp'], language='c++', extra_compile_args=["-std=c++11"])],
...

あるいは、以下のようにCythonで unordered_maptr1 内にあることを指定してもできる。


# from https://github.com/ikuyamada/cython-cpp-experiment/blob/master/unordered_map.pxd
from libcpp.utility cimport pair

cdef extern from "<tr1/unordered_map>" namespace "std::tr1":
    cdef cppclass unordered_map[T, U]:
        cppclass iterator:
            pair[T, U]& operator*() nogil
            iterator operator++() nogil
            iterator operator--() nogil
            bint operator==(iterator) nogil
            bint operator!=(iterator) nogil
        unordered_map()
        unordered_map(unordered_map&)
        U& operator[](T&) nogil
        # unordered_map& operator=(unordered_map&)
        U& at(T&) nogil
        iterator begin() nogil
        void clear() nogil
        size_t count(T&) nogil
        bint empty() nogil
        iterator end() nogil
        pair[iterator, iterator] equal_range(T&) nogil
        void erase(iterator) nogil
        void erase(iterator, iterator) nogil
        size_t erase(T&) nogil
        iterator find(T&) nogil
        pair[iterator, bint] insert(pair[T, U]) nogil
        iterator insert(iterator, pair[T, U]) nogil
        void insert(input_iterator, input_iterator)
        size_t max_size() nogil
        void rehash(size_t)
        size_t size() nogil
        void swap(unordered_map&) nogil
5
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
2