Settings for testing C ++ 11 Python modules on Travis CI

When I try to test a Python module created with Cython with Travis CI, the installation looks like the following. It was.

$ 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

This is due to the lack of std :: unordered_map and std :: unordered_set because the gcc version (4.6) of Travis CI is old and not compatible with C ++ 11. Let's upgrade gcc and g ++ with .travis.yml and set 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

And if you explicitly specify C ++ 11 in setup.py, everyone will be happy, not just Travis.

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

Alternatively, you can specify in Cython that ʻunordered_map is in tr1`, as shown below.


# 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

Recommended Posts

Settings for testing C ++ 11 Python modules on Travis CI
Initial settings for using Python3.8 and pip on CentOS8
Snippet settings for python jupyter notebook
Python cheat sheet (for C ++ experienced)
Emacs settings for Python development environment
Install confluent-kafka for Python on Ubuntu
Call C / C ++ from Python on Mac
Notes for using OpenCV on Windows10 Python 3.8.3.
[Python] Automate Pelican builds with Travis CI
Execute Python code on C ++ (using Boost.Python)
Notes on nfc.ContactlessFrontend () for nfcpy in python
Creating amateur python environment settings (for MAC)
[C] [python] Read with AquesTalk on Linux
Install CVXOPT, NumPy, SciPy on Travis CI
Run Python in C ++ on Visual Studio 2017
An introduction to Python for C programmers
A procedure manual for quickly publishing a C ++ Python library using pybind11 on Github.
(Windows) Causes and workarounds for UnicodeEncodeError on Python 3
Settings for uploading Python packages locally to PyPI
Run Python YOLOv3 in C ++ on Visual Studio 2017
Wrap C with Cython for use from Python
Tweet (API 1.1) on Google App Engine for Python
Logging settings for daily log rotation in python
Note on encoding when LANG = C in Python
Boost.NumPy Tutorial for Extending Python in C ++ (Practice)
A memorandum for touching python Flask on heroku
[Python / Chrome] Basic settings and operations for scraping
Settings for Python coding in Visual Studio Code
Wrap C ++ with Cython for use from Python
Installing TensorFlow on Windows Easy for Python beginners
[Visual Studio Code] [Python] Tasks.json + problemMatcher settings for Python
Astro: Python modules / functions often used for analysis
Install Python and libraries for Python on MacOS Catalina
Settings for getting started with MongoDB in python