1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Rep Power

    Python module build error. Compiler doesn't understand C++ syntax

    I'm trying to build a Python module written in C++. In the module file I use C++ types I defined in other files and I include their header files in the C++ module file.

    The problem I'm having is that when I build the module with my build script (listed below) I get errors implying that the compiler doesn't understand C++ syntax. I get the error listed below. I included the directory containing the types I use as include_dirs and library_dirs in the Extension command in my build script but that didn't help.

    Here is the error I get:

    running build
    running build_ext
    building 'xx' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c xxmodule.c -o build/temp.linux-x86_64-2.7/xxmodule.o
    In file included from cdtm-model.h:4:0,
                     from xxmodule.c:19:
    corpus.h:12:3: error: unknown type name ‘doc’
    corpus.h:18:1: error: unknown type name ‘class’
    corpus.h:18:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    In file included from xxmodule.c:19:0:
    cdtm-model.h:6:1: error: unknown type name ‘class’
    cdtm-model.h:7:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    cdtm-model.h:21:1: error: unknown type name ‘class’
    cdtm-model.h:22:1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
    xxmodule.c: In function ‘xx_main_est’:
    xxmodule.c:169:3: error: unknown type name ‘corpus’
    xxmodule.c:169:15: error: ‘new’ undeclared (first use in this function)
    xxmodule.c:169:15: note: each undeclared identifier is reported only once for each function it appears in
    xxmodule.c:169:19: error: expected ‘,’ or ‘;’ before ‘corpus’
    xxmodule.c:170:3: error: unknown type name ‘settings’
    xxmodule.c:171:10: error: request for member ‘read_setting’ in something not a structure or union
    xxmodule.c:170:12: warning: variable ‘setting’ set but not used [-Wunused-but-set-variable]
    xxmodule.c:169:11: warning: unused variable ‘c’ [-Wunused-variable]
    error: command 'gcc' failed with exit status 1

    When I changed
    struct doc {...};
    struct {...} doc;
    in my header file, the compiler stopped complaining about having "unknown type name 'doc'". It seems like the compiler can't understand C++ syntax.

    Here is my build script is:

    from distutils.core import setup, Extension
    cdtmmodule = Extension('xx',
                        library_dirs = ['/home/welshamy/workspace/cidtm/cdtm'],
                        sources = ['xxmodule.c'])
    setup (name = 'cdtm',
           version = '1.0',
           description = 'This is the cDTM package',
           ext_modules = [cdtmmodule])
  2. #2
  3. Contributing User

    Join Date
    Aug 2011
    Rep Power
    gcc recognizes file types.
    Looks like you have files named

    On my *nix system I name files with the extension
    to have gcc to invoke the c++ compiler.

    Clearly, that might not be portable. I'm not sure what your files should be named, perhaps one of

    or you could pass an option (probably) that tells gcc which compiler to use,

    or maybe by invoking gcc with g++ you'd access the c++ compiler.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Rep Power
    Thanks b49P23TIvg. I changed the module extension from .c to .cpp and the problem is solved.

IMN logo majestic logo threadwatch logo seochat tools logo