Linking languages

后端 未结 9 1294
野趣味
野趣味 2020-12-31 06:16

I asked a question earlier about which language to use for an AI prototype. The consensus seemed to be that if I want it to be fast, I need to use a language like Java or C+

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-31 06:56

    Boost.Python provides an easy way to turn C++ code into Python modules. It's rather mature and works well in my experience.

    For example, the inevitable Hello World...

    char const* greet()
    {
      return "hello, world";
    }
    

    can be exposed to Python by writing a Boost.Python wrapper:

    #include 
    
    BOOST_PYTHON_MODULE(hello_ext)
    {
      using namespace boost::python;
      def("greet", greet);
    }
    

    That's it. We're done. We can now build this as a shared library. The resulting DLL is now visible to Python. Here's a sample Python session:

    >>> import hello_ext
    >>> print hello.greet()
    hello, world
    

    (example taken from boost.org)

提交回复
热议问题