尝试为c++互操作自动生成库时出错

Errors when trying to auto generate my library for c++ interop

本文关键字:出错 自动生成 互操作 c++      更新时间:2023-10-16

我正在使用pybindgen创建python和c++之间的链接代码。或者,如果我能让它与我的代码一起工作,至少我会的。运行python文件时会产生以下错误。任何我可以满足pybindgen或解决这些问题的方法都将非常有帮助。

pygccxml.parser.source_reader.gccxml_runtime_error_t: Error occured while running GCC-XML: In file included from c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/cstdio:44,
from c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/fstream:43,
from C:UsersRichardworkspacePyBindGensrc/tokeniser.hpp:13,
from C:UsersRichardworkspacePyBindGensrc/includes.hpp:11,
from C:UsersRichardworkspacePyBindGensrcfile.hpp:9:
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdio.h:191: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
... [some recuring size_t errors removed because size too big]
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdio.h:413: error: expected constructor, destructor, or type conversion before 'fwrite'
In file included from c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/fstream:43,
from C:UsersRichardworkspacePyBindGensrc/tokeniser.hpp:13,
from C:UsersRichardworkspacePyBindGensrc/includes.hpp:11,
from C:UsersRichardworkspacePyBindGensrcfile.hpp:9:
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/cstdio:109: error: '::fread' has not been declared
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/cstdio:115: error: '::fwrite' has not been declared
In file included from C:UsersRichardworkspacePyBindGensrc/tokeniser.hpp:14,
from C:UsersRichardworkspacePyBindGensrc/includes.hpp:11,
from C:UsersRichardworkspacePyBindGensrcfile.hpp:9:
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:345: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:345: error: expected constructor, destructor, or type conversion before 'wcstombs'
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:348: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:348: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:348: error: 'size_t' has not been declared
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:349: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:349: error: expected constructor, destructor, or type conversion before 'mbstowcs'
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:350: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:350: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:350: error: 'size_t' has not been declared
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:355: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:355: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:355: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:355: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:355: error: initializer expression list treated as compound expression
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:356: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/stdlib.h:356: error: reference to 'size_t' is ambiguous
... [some recuring size_t errors removed because size too big]
In file included from c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/cstring:44,
from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/lexical_cast.hpp:32,
from C:UsersRichardworkspacePyBindGensrc/dataitem.hpp:9,
from C:UsersRichardworkspacePyBindGensrc/includes.hpp:24,
from C:UsersRichardworkspacePyBindGensrcfile.hpp:9:
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/string.h:36: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/string.h:36: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/string.h:36: error: 'size_t' has not been declared
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/string.h:37: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
... [some recuring size_t errors removed because size too big]
In file included from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/lexical_cast.hpp:32,
from C:UsersRichardworkspacePyBindGensrc/dataitem.hpp:9,
from C:UsersRichardworkspacePyBindGensrc/includes.hpp:24,
from C:UsersRichardworkspacePyBindGensrcfile.hpp:9:
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/cstring:86: error: '::strcspn' has not been declared
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/cstring:88: error: '::strlen' has not been declared
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/cstring:92: error: '::strspn' has not been declared
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/cstring:94: error: '::strxfrm' has not been declared
In file included from c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/ctime:44,
from c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/bits/locale_facets_nonio.h:39,
from c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/locale:43,
from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/lexical_cast.hpp:47,
from C:UsersRichardworkspacePyBindGensrc/dataitem.hpp:9,
from C:UsersRichardworkspacePyBindGensrc/includes.hpp:24,
from C:UsersRichardworkspacePyBindGensrcfile.hpp:9:
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/time.h:124: error: reference to 'size_t' is ambiguous
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/stddef.h:212: error: candidates are: typedef unsigned int size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/mingw32/bits/c++config.h:155: error:                 typedef unsigned int std::size_t
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/../../../../include/time.h:124: error: expected constructor, destructor, or type conversion before 'strftime'
In file included from c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/bits/locale_facets_nonio.h:39,
from c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/locale:43,
from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/lexical_cast.hpp:47,
from C:UsersRichardworkspacePyBindGensrc/dataitem.hpp:9,
from C:UsersRichardworkspacePyBindGensrc/includes.hpp:24,
from C:UsersRichardworkspacePyBindGensrcfile.hpp:9:
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/ctime:74: error: '::strftime' has not been declared
In file included from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/assert.hpp:81,
from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/array.hpp:44,
from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/lexical_cast.hpp:146,
from C:UsersRichardworkspacePyBindGensrc/dataitem.hpp:9,
from C:UsersRichardworkspacePyBindGensrc/includes.hpp:24,
from C:UsersRichardworkspacePyBindGensrcfile.hpp:9:
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/cstdlib:122: error: '::mbstowcs' has not been declared
c:/mingw/bin/../lib/gcc/mingw32/4.6.1/include/c++/cstdlib:134: error: '::wcstombs' has not been declared
In file included from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/range/end.hpp:24,
from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/range/functions.hpp:19,
from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/range/iterator_range_core.hpp:26,
from C:/Users/Richard/Desktop/code/boost_1_51_0/boost/lexical_cast.hpp:155,
from C:UsersRichardworkspacePyBindGensrc/dataitem.hpp:9,
from C:UsersRichardworkspacePyBindGensrc/includes.hpp:24,
from C:UsersRichardworkspacePyBindGensrcfile.hpp:9:
C:/Users/Richard/Desktop/code/boost_1_51_0/boost/range/detail/implementation_help.hpp: In function 'const char* boost::range_detail::str_end(const char*, const char*)':
C:/Users/Richard/Desktop/code/boost_1_51_0/boost/range/detail/implementation_help.hpp:37: error: 'strlen' was not declared in this scope

我的.py文件是

#! /usr/bin/env python
import sys
import os
import pybindgen
from pybindgen.typehandlers import base as typehandlers
from pybindgen import (ReturnValue, Parameter, Module, Function, FileCodeSink)
from pybindgen import (CppMethod, CppConstructor, CppClass, Enum)
from pybindgen.gccxmlparser import ModuleParser
from pybindgen.function import CustomFunctionWrapper
from pybindgen.cppmethod import CustomCppMethodWrapper
import generator_common
def my_module_gen():
out = FileCodeSink(sys.stdout)
pygen_file = open(sys.argv[2], "wt")
module_parser = ModuleParser('eyon', '::')
module_parser.enable_anonymous_containers = True
module = module_parser.parse([sys.argv[1]], includes=['"file.hpp"'], pygen_sink=FileCodeSink(pygen_file),
include_paths=["C:\Users\Richard\Desktop\code\boost_1_51_0"])
pygen_file.close()
generator_common.customize_module(module)
module.generate(out)

def main():
if sys.argv[1] == '-d':
del sys.argv[1]
import pdb
pdb.set_trace()
my_module_gen()
else:
import os
if "PYBINDGEN_ENABLE_PROFILING" in os.environ:
try:
import cProfile as profile
except ImportError:
my_module_gen()
else:
print >> sys.stderr, "** running under profiler"
profile.run('my_module_gen()', 'foomodulegen-auto.pstat')
else:
my_module_gen()            
if __name__ == '__main__':
main()

只是猜测,但从您的错误消息来看,您可能有路径问题。注意src和tokeniser之间的路径标记。C:UsersRichardworkspacePyBindGensrc/tokeniser.hpp:13

这不是一项容易的工作,但现在已经完成了。

我从代码中删除了所有的"using namespace std;"和任何其他"using"项,因此对标准库中的任何内容都使用了"std::"。这消除了size_t名称冲突的问题。这似乎解决了很多问题,可能是标准库的c++标准/MinGW实现,不能很好地与GCC_XML配合使用。换句话说,c++标准允许GCC_XML无法理解的名称空间声明,因此会产生冲突。

请注意,这似乎不会影响在名称空间中包含内容。所以这很好用。

namespace Hello
{
void func()
{
printf("Hello World");
}
}

其他问题来自于我没有向pybindgen声明足够的boost库依赖性。这是修改后的.py文件。

#! /usr/bin/env python
import sys
import os
import pybindgen
from pybindgen.typehandlers import base as typehandlers
from pybindgen import (ReturnValue, Parameter, Module, param, Function, FileCodeSink)
from pybindgen import (CppMethod, CppConstructor, CppClass, Enum)
from pybindgen.gccxmlparser import ModuleParser
from pybindgen.function import CustomFunctionWrapper
from pybindgen.cppmethod import CustomCppMethodWrapper
import generator_common
def my_module_gen():
out = FileCodeSink(sys.stdout)
pygen_file = open(sys.argv[2], "wt")
#setup the module
module = Module('eyon', cpp_namespace='::')
module.add_include('"file.hpp"')
module.add_include('"boost/lexical_cast.hpp"')
module_parser = ModuleParser('eyon', '::')
module_parser.enable_anonymous_containers = True
module = module_parser.parse([sys.argv[1]], includes=['"file.hpp"'], pygen_sink=FileCodeSink(pygen_file),
include_paths=["C:\Users\Richard\Desktop\code\boost_1_51_0"])
pygen_file.close()
module.generate(out)

def main():
if sys.argv[1] == '-d':
del sys.argv[1]
import pdb
pdb.set_trace()
my_module_gen()
else:
import os
if "PYBINDGEN_ENABLE_PROFILING" in os.environ:
try:
import cProfile as profile
except ImportError:
my_module_gen()
else:
print >> sys.stderr, "** running under profiler"
profile.run('my_module_gen()', 'foomodulegen-auto.pstat')
else:
my_module_gen()            
if __name__ == '__main__':
main()