2
votes

Edit: accidentally copy pasted a line twice.

I'm making a C++ Python extension using a combination of boost and the normal C API, but I can't compile it. I've read the following documentations:

documentation from boost.org

documentation from python wiki

Python bindings for C++ medium

here's the code from the cpp file:

#include <boost/python.hpp>
#include <boost/python/make_constructor.hpp>
#include <boost/python/detail/api_placeholder.hpp>

#include <iostream>
#include <string>
using namespace std;
class TestClass{
    TestClass(string msg){
        cout << "Created test class object" << endl;
        cout << msg;
    }
};
BOOST_PYTHON_MODULE(packet)
{
    class_<TestClass>("TestClass", init<std::string>());
}

the makefile:

test: test.cpp
    g++ -Wall -shared -std=c++11 -fPIC -I/usr/include -o test`python3-config --extension-suffix` test.cpp

error output:

test.cpp:17:5: error: ‘class_’ was not declared in this scope
     class_<TestClass>("TestClass", init<std::string>());
     ^
test.cpp:17:5: note: suggested alternative:
In file included from /usr/include/boost/python/object_core.hpp:20:0,
                 from /usr/include/boost/python/args.hpp:25,
                 from /usr/include/boost/python.hpp:11,
                 from test.cpp:1:
/usr/include/boost/python/def_visitor.hpp:14:56: note:   ‘boost::python::class_’
 template <class T, class X1, class X2, class X3> class class_;
                                                        ^
test.cpp:17:21: error: expected primary-expression before ‘>’ token
     class_<TestClass>("TestClass", init<std::string>());
                     ^
test.cpp:17:36: error: ‘init’ was not declared in this scope
     class_<TestClass>("TestClass", init<std::string>());
                                    ^
test.cpp:17:36: note: suggested alternative:
In file included from /usr/include/boost/python/class.hpp:20:0,
                 from /usr/include/boost/python.hpp:18,
                 from test.cpp:1:
/usr/include/boost/python/init.hpp:58:7: note:   ‘boost::python::init’
 class init; // forward declaration
       ^
test.cpp:17:52: error: expected primary-expression before ‘>’ token
     class_<TestClass>("TestClass", init<std::string>());
                                                    ^
test.cpp:17:54: error: expected primary-expression before ‘)’ token
     class_<TestClass>("TestClass", init<std::string>());
                                                      ^
make: *** [test] Error 1

I think I've included all the header files but I'm not sure why it says that it's not declared in this scope. Any help would be very appreciated

1

1 Answers

1
votes

Like (almost?) everything (that isn't a macro) in Boost, class_ is in a namespace, in this case boost::python. Either:

  1. Add using namespace boost::python; at the top of your file (just after the includes).
  2. Refer to boost::python::class_ instead.

The same applies to the other symbols to.

This is shown in the quickstart part of the documentation. For most of the rest of it they only show short code snippets so I think it's meant to be implicit.