0

I have two c++ files named as "animal.cpp" and "zoo.cpp". These files are compiled as a shared library "zoo.so". I want to access the functions defined in these files from Python interpreter.

However, I am getting errors. I tried to do some changes but getting different kind of errors. I hope expert of Boost-Python may guide me in solving these issues.

Note: To keep the problem simple, I have not used classes, otherwise I could easily make classes of "animal" and "zoo".

I want to access both files function from Python.

Any guidance will be highly appreciated.

Thanks,

animal.cpp

+++++++++++

/*
 * This is the C++ function we write and want to expose to Python.
 */

const std::string hello_animal() {
    return std::string("hello, animal");
}

/*
 * This is the C++ function we write and want to expose to Python.
*/
const std::string getname_animal() {
    std::string input;
    std::cout<<"Please enter your favorit animal name: ";
    std::getline(std::cin,input);
    return std::string("Your favorit animal name is: ").append(input);
}

/*
 * This is a macro Boost.Python provides to signify a Python extension module.
 */
BOOST_PYTHON_MODULE(animal) {
    // An established convention for using boost.python.
    using namespace boost::python;

    // Expose the function hello_animal().
    def("hello_animal", hello_animal);

    // Expose the function getname_animal().    
    def("getname_animal", getname_animal);
}


zoo.cpp
++++++++

/*
 * This is the C++ function we write and want to expose to Python.
 */
const std::string hello_zoo() {
    return std::string("hello, zoo");
}

/*
 * This is the C++ function we write and want to expose to Python.
*/
const std::string getname_zoo() {
    std::string input;
    std::cout<<"Please enter your favorit zoo name: ";
    std::getline(std::cin,input);
    return std::string("Your favorit zoo name is: ").append(input);
}

/*
 * This is a macro Boost.Python provides to signify a Python extension module.
 */
BOOST_PYTHON_MODULE(zoo) {
    // An established convention for using boost.python.
    using namespace boost::python;

    // Expose the function hello_zoo().
    def("hello_zoo", hello_zoo);

    // Expose the function getname_zoo().    
    def("getname_zoo", getname_zoo);
}

I have created a shared library using following command:

g++ -shared -o zoo.so -fPIC zoo.cpp animal.cpp -lboost_python -lpython2.7 -I/usr/include/python2.7

Python Interpreter Commands and output:

import zoo

zoo.getname_zoo()

Please enter your favorit zoo name: zoo

'Your favorit zoo name is: zoo'

zoo.getname_animal()

Traceback (most recent call last): File "", line 1, in AttributeError: 'module' object has no attribute 'getname_animal'

When I make following changes in animal.cpp file, I get different error.

BOOST_PYTHON_MODULE(zoo) {

instead of

BOOST_PYTHON_MODULE(animal) {

On compilation I get following error:

[root@localhost zooexample]# g++ -shared -o zoo.so -fPIC zoo.cpp animal.cpp -lboost_python -lpython2.7 -I/usr/include/python2.7 /tmp/cci4WKrP.o: In function initzoo': animal.cpp:(.text+0x15d): multiple definition ofinitzoo' /tmp/cctaGDer.o:zoo.cpp:(.text+0x15d): first defined here /tmp/cci4WKrP.o: In function init_module_zoo()': animal.cpp:(.text+0x179): multiple definition ofinit_module_zoo()' /tmp/cctaGDer.o:zoo.cpp:(.text+0x179): first defined here collect2: error: ld returned 1 exit status

+++++++++++++++++++++++++++++UPDATED++++UPDATED+++++UPDATED+++++++++++++++

Considering the suggestion given by Dan for my previous issue. Now I have created a separate file pyintf.cpp containing the following code:

BOOST_PYTHON_MODULE(pyintf) {
    def("hello_zoo", hello_zoo);
    def("getname_zoo", getname_zoo);
    def("hello_animal", hello_animal);
    def("getname_animal", getname_animal);
} 

but when I compile and create a shared library using the following command:

g++ -shared -o pyintf.so -fPIC pyintf.cpp -lboost_python -lpython2.7 -I/usr/include/python2.7

I get following error when I import the "pyintf" module in python.

import pyintf Traceback (most recent call last): File "", line 1, in ImportError: ./pyintf.so: undefined symbol: _Z9hello_zoov

Can anyone tell what I am not doing correctly?

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

4
  • First problem is because you have 2 modules in one .so. Python will only initialize the module that has the same name as the .so (that's how the loading of binary modules works). The second problem is because you have two occurrences of BOOST_PYTHON_MODULE(zoo). This macro generates functions (with name based on the parameter) to initialize the module. See here. | Simple solution would be to put all the Python related stuff into a separate .cpp file. Commented Jan 30, 2019 at 14:33
  • Thanks Dan for your suggestion. Commented Jan 30, 2019 at 20:54
  • Looks like you forgot to compile and link the other two CPP files that contain the implementation of your functions. Commented Jan 30, 2019 at 21:46
  • Thanks Dan, Now, the above issue resolved by adding header files (zoo.h, animal.h) and source files (zoo.cpp, animal.cpp) in the compilation command. g++ -shared -o pyintf.so -fPIC pyintf.cpp animal.h animal.cpp zoo.h zoo.cpp -lboost_python -lpython2.7 -I/usr/include/python2.7 Commented Jan 31, 2019 at 3:52

1 Answer 1

1

The issues were resolved by putting all methods which I wanted to expose in a single pyintf.cpp. The errors which I got after update were due to missing header files and source files.

Now, the above issue has resolved by adding header files (zoo.h, animal.h) and source files (zoo.cpp, animal.cpp) in the compilation command. g++ -shared -o pyintf.so -fPIC pyintf.cpp animal.h animal.cpp zoo.h zoo.cpp -lboost_python -lpython2.7 -I/usr/include/python2.7

Thanks Dan for corrections.

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.