0

I'm trying to expose one of my classes to python using boost's python module. I have a class called StatusEffect that I'd like to expose to python. To do so, I'm creating this in the StatusEffect.h file after the class definition of StatusEffect

BOOST_PYTHON_MODULE(StatusEffect)
{
    boost::python::class_<StatusEffect>("StatusEffect")
        .def("GetPriority", &StatusEffect::GetPriority)
        .def("GetDescription", &StatusEffect::GetDescription)
        .def("GetName", &StatusEffect::GetName);
}

I want to use it inside python from a function declared inside a header as so:

Primaries.h

#include <boost\python.hpp>
#include <StatusEffects\StatusEffect.h>
#include "ScriptDeclarations.h";
extern void Test();

and in Primaries.cpp

#include "Primaries.h"
class StatusEffect;
using namespace boost::python;

class CppClass {
public:
  int getNum() {
    return 7;
  }
};

        BOOST_PYTHON_MODULE(CppMod) {
  class_<CppClass>("CppClass")
    .def("getNum",&CppClass::getNum);
}
void Test()
{
    CppClass mClass;
    try 
    {
    PyImport_AppendInittab("CppMod", &initCppMod);
    PyImport_AppendInittab("StatusEffect", &initStatusEffect);
    Py_Initialize();

    object main_module((
      handle<>(borrowed(PyImport_AddModule("__main__")))));

    object main_namespace = main_module.attr("__dict__");

    StatusEffect effect("Haste");
    main_namespace["eff"] = ptr(&effect);
    handle<> ignored((PyRun_String("print eff.GetName()\n",
                                    Py_file_input,
                                    main_namespace.ptr(),
                                    main_namespace.ptr() ) ));

    /*main_namespace["CppClass"] = class_<CppClass>("CppClass")
                               .def("getNum",&CppClass::getNum);
    main_namespace["cpp"] = ptr(&mClass);
    handle<> ignored(( PyRun_String("print cpp.getNum()\n",
                                     Py_file_input,
                                     main_namespace.ptr(),
                                     main_namespace.ptr() ) ));*/

  } 
  catch( error_already_set ) 
  {
    PyErr_Print();
  }
}

however, when I do this, I get the following error:

Error 16 error LNK2001: unresolved external symbol "public: class std::basic_string,class std::allocator > __thiscall StatusEffect::GetDescription(void)" (?GetDescription@StatusEffect@@QAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) C:\KHMP\Game_Cpp\KHMPCpp\KHMPCpp\GameCharacter.obj KHMPCpp

I'm quite sure this is related to my python declaration as when I remove that, and the corresponding code from Test() it compiles correctly. If I uncomment the code concerning the CppCLass in Primaries.cpp and use CppClass however, everything works fine. Do python module declarations need to be in the same source file as where they're used? If so, is there some way around that? I'd like to ultimately be able to put all the Python wrappers for my classes in a single header file.

Can anyone guide me as to what I'm doing wrong?

thanks

1
  • You should consider changing the title. Every Question on the the site is a question, and the fact that it is boost::python is covered in the tags. You title really tells me nothing. Also, from the look of it, this is a c++ linker problem not a boost::python question. Commented Mar 18, 2011 at 14:13

1 Answer 1

1

One thing to note is that your wrapper should be in your StatusEffect.cpp, not StatusEffect.h or a separate CPP file. However, it should be OK anyway, since everything should still get exported. I would also write a test script in Python, instead of trying to test it from C.

There is not enough information to figure out why your code is not working. Some of the things to consider:

  1. Does GetDescription have an implementation? Is it a pure-virtual function?
  2. Is your StatusEffect class in a different module? If it is, is it __dllexport'ed (if you're on Windows)?

The error seems to point at a problem before you get to the boost::python wrapper. Have you tried commenting out your python stuff and just trying to call StatusEffect::GetDescription directly from your test program?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.