Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4072978
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:57:15+00:00 2026-05-20T16:57:15+00:00

I’m trying to expose one of my classes to python using boost’s python module.

  • 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T16:57:15+00:00Added an answer on May 20, 2026 at 4:57 pm

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.