Search code examples
c++cythonpython-extensions

Building 64 bit Python extension on Windows tuple : undeclared identifier


I work on 64 bit windows with 64 bit python 2.7 and i am trying to compile my first c++ extension for python. At first i tried to use mingw but ran into many linker errors, so now i use c++ for python 2.7 with the windows7 SDK.

It should be an object storing two times a c++ map, map<tuple<int,int,int>,pen> and map<int,pen>, where pen is a c++ struct.

The header of the class (Tactic.h):

#include <iostream>
#include <string>
#include <string.h>
#include <sstream>
#include <map>
#ifndef TACTIC
#define TACTIC
#include <tuple>
#include <vector>
using namespace std;
struct pen
{
public:
    pen():p0(-15),p1(-15),p2(-15),p3(-15),
        p4(-15),p5(-15),p6(-15),
        p7(-15),p8(-15),p9(0){}
    signed int p0:11;
    signed int p1:11;
    signed int p2:11;
    signed int p3:11;
    signed int p4:11;
    signed int p5:11;
    signed int p6:11;
    signed int p7:11;
    signed int p8:11;
    signed int p9:4;
};
class Tactic
{
    public:
        Tactic();
        Tactic(vector<vector<int>> &map1, vector<vector<int>> &map2);
        ~Tactic();
        void adjust_map1(int k0, int k1,int k2,const char *act, int modifier);
        void adjust_map2(int key,const char *act, int modifier);
        vector<int> get_map1(int key);
        vector<int> get_map2(int k0, int k1,int k2);
        vector< vector < vector<int> > > get_list();
        void load_dict(vector< vector < vector<int> > > dict_list);
    private:
        map<int,pen> *map1;
        map<tuple<int,int,int>,pen> *map2;
};
#endif

the setup.py:

import os

from setuptools import setup
from setuptools import Extension
from Cython.Build import cythonize

ext_modules=[
    Extension("tactic",
        extra_compile_args=["/EHsc"],
        sources = ["tactic.pyx","Tact_source.cpp"],
        language = "c++",)]
setup(
    name="tactic",
    ext_modules = cythonize(ext_modules),)

The command python setup.py build_ext --inplace -compiler=msvc fails with following error:

D:\...>python setup.py build_ext --inplace -compiler=msvc
running build_ext
building 'tactic' extension
C:\Users\....\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:\Python27\include -IC:\Python27\PC /Tptactic.cpp /Fobuild\temp.win-amd64-2.7\Release\tactic.obj /EHsc
tactic.cpp
d:\...\Tactic.h(44) : error C2065: 'tuple' : undeclared identifier
d:\...\Tactic.h(44) : error C2062: type 'int' unexpected
d:\...\Tactic.h(44) : error C2059: syntax error : ','
d:\...\Tactic.h(44) : error C2238: unexpected token(s) preceding ';'
error: command '"C:\Users\....\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\VC\Bin\amd64\cl.exe"' failed with exit status 2

So, it seems that although i imported tuple, the compiler cannot find a declaration of tuple. Does anyone have an idea what the matter could be? Is this maybe not a compiler but a linker error? Thank you in advance for any help.


Solution

  • Visual studio 2008 C++ compiler being outdated does not have tuple in std namespace but in std::tr1, so replacing instances of std::tuple with std::tr1::tuple should work.

    This is also documented in msdn docs