Search code examples
c++pythonboostlnk2019

boost exposing classes LNK2019 / module does not define init function


I know those errors in the headline are nothing new, but in this constellation I have here it's rather strange and I don't find a solution that is helping. So let me explain.

I'm working with boost.python, Visual Studio 2012 and the tutorial from the developers site. So as i finally fixed all linker problems and imports I got the first tutorial working:

char const* greet()
{
   return "hello, world";
}

#include <boost/python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(hello_ext)
{
    using namespace boost::python;
    def("greet", greet);
}

Works fine.

Now I'm trying the whole thing with a class, that I can create an object in python. Ofc I started with the easier example having a default constructor. Here's my code:

World.h

#pragma once
class World
{
public:
    void greet();
};

World.cpp

#include "World.h"
#include <stdio.h>

void greet(){
    printf("hello");
}


#include <boost\python.hpp>
using namespace boost::python;

BOOST_PYTHON_MODULE(hello)
{
    class_<World>("World")
        .def("greet", &World::greet)
        ;
}

If I now try to build it, I get a LNK2019 Error which I had before whilst having wrong linkers or imports on the simple greet method project. What cracks my head is that I used the same project settings for my class exposure example (I can't explain why i get a linker error again). When I try to only expose the method greet by:

def("greet", greet);

in the BOOST_PYTHON_MODULE block, I can build it but get an import error in python such as:

module does not define init function

It's so strange that it's a linker error, because I triple checked and compared the settings to my project with simple greet function. Do I maybe have to change some settings when trying to expose classes? Or am I doing something horribly wrong? Any Suggestions?

Greetings Chris


Solution

  • You are missing the name of class:

    void World::greet(){
        printf("hello");
    }