Question

I would like to implement the expose module in derived class (.h or .cpp file). its work fine, when I exposed this in main in one BOOST_PYTHON_MODULE but when I exposed differently in abstract and derived class its get error one or more multiply defined symbols errors. the example code is as follows.

 **Base.h**

   class Base
     {
       public:
       virtual void Set(const std::vector<std::string>& AllParameters) = 0;
      };

     struct BaseWrap : Base, wrapper<Base>
     {
        void Set(const std::vector<std::string>& AllParameters)
           {
             this->get_override("Set")(AllParameters);

           }

      **Base.cpp**

        BOOST_PYTHON_MODULE(Example)
          {
            class_<Basewrapper , boost::noncopyable> ("Base")
              .def("Set",pure_virtual(&Base::Set))
               ;
          }

       **Derived.h**

        class Derived : public Base
                 {
                  public:
                     int test(int a, int b);

                 };
             BOOST_PYTHON_MODULE(Example)
            {
                 class_<Derived , boost::noncopyable> ("Derived")
                   .def("test",&Derived ::test)
               ;
          }

        **Derived.cpp**


        void Derived:: test(int a , int b)

         { 
           return a+b; 

           }
Was it helpful?

Solution

The BOOST_PYTHON_MODULE macro defines a function with identifiers based on the provided argument. Hence, defining the Example module in both Base.cpp and Derived.h violates the one definition rule when linking them into the same library. To resolve this, consider splitting the function bodies that export each individual class into their own functions (i.e. export_base() and export_derived()), and have a single file (example.cpp) that defines BOOST_PYTHON_MODULE with a body that invokes the other export functions. This technique can be seen here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top