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 8920597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:18:45+00:00 2026-06-15T06:18:45+00:00

I’m currently trying to put part of a complex program into a dynamic library.

  • 0

I’m currently trying to put part of a complex program into a dynamic library. This part consists of some classes that are also wrapped with boost python into a module to be embedded again. Here is a simplified version of the dll source.

Hello.cpp:

#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/python.hpp>
#include <Foo.h>

using namespace boost::python;

typedef boost::shared_ptr<Hello> hello_ptr;

BOOST_PYTHON_MODULE(Hello)
{
    class_<Hello, hello_ptr>("Hello")
        .def("say_hello", &Hello::say_hello)
    ;
};


void Hello::say_hello(){
    cout << "Hello World!" << endl;
}

void World::foo(){
    Py_Initialize();

    try {
        PyRun_SimpleString(
            "hello = None\n"
            "\n"
            "def setup(hello_from_cxx):\n"
            "    print 'setup called with', hello_from_cxx\n"
            "    global hello\n"
            "    hello = a_foo_from_cxx\n"
            "\n"
            "def run():\n"
            "    hello.say_hello()\n"
            "\n"
            "print 'main module loaded'\n"
        );
        //initialize eviroment
        initHello();

        hello_ptr hello = boost::make_shared<Hello>();

        object main = object(handle<>(borrowed(
            PyImport_AddModule("__main__")
        )));

        // pass the reference to hello into python:
        object setup_func = main.attr("setup");
        setup_func(hello);

        // now run the python 'main' function
        object run_func = main.attr("run");
        run_func();
    }
    catch (error_already_set) {
        PyErr_Print();
    }
}

Hello.h

#ifndef HELLO_H_INCLUDED
#define HELLO_H_INCLUDED

#include <iostream>
#include <boost/python.hpp>

using namespace std;

class Hello{
public:
    void say_hello();
};

class World{
public:
    void foo();
};

#endif // HELLO_H_INCLUDED

In the main function of the application I then create a instance of the class that embeds python but it gives an undefined reference to World::foo() even though the function is defined in the dynamic library libFoo.dll which is also linked against by the main application.

main.cpp:

#include <iostream>
#include <Hello.h>

using namespace std;

int main(){
    cout << "Test" << endl;

    World world;

    world.foo();
}

Console:

undefined reference to `World::foo()'

I’m using Code::Blocks with MinGW.

This problem has kept me awake some days now and I can’t seem to find a way to solve it. Hope you can help me.

Thanks in advance.

UPDATE:

I now tried to slove this task using the normal Python C API. There the step of defining the Python_Module (BOOST_PYTHON_MODULE()) to which this error is definitely connected, is done by defining a function like:

PyMODINIT_FUNC initHello(void){
...
}

Now writing infront of this

#define PyMODINIT_FUNC void

solves the error. Does anyone now how the boost.python library defines the BOOST_PYTHON_MODULE function? Maybe there is this kind of solution as well? I couldn’t find any PyMODINIT_FUNC stuff in the source files.

  • 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-06-15T06:18:46+00:00Added an answer on June 15, 2026 at 6:18 am

    Found the solution myself.

    It’s like I had already guessed. What is needed is to define BOOST_PYTHON_MODULE_INIT(name) like this.

    #   define BOOST_PYTHON_MODULE_INIT(name)                               \
      void BOOST_PP_CAT(init_module_,name)();                               \
    extern "C" __attribute__ ((__visibility__("default"))) _BOOST_PYTHON_MODULE_INIT(name)
    

    Actually I found this in one of the header files with some #ifdef infront but it seems that they’re not working correctly. At least not for me.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am currently running into a problem where an element is coming back from
I'm trying to create an if statement in PHP that prevents a single post

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.