Skip to content

Minimal Boost Python example not working #437

Description

@robertapplin

I'm having trouble getting boost python to work with this minimal example. I've emailed the mailing list but have had no response.

I'm using C++20

#include <boost/python.hpp>

#include <iostream>
#include <string>

void printStr(const std::string &testStr) {
  std::cout << "BEFORE" <<std::endl;
  std::cout << testStr <<std::endl;
  std::cout << "AFTER" <<std::endl;
}

BOOST_PYTHON_MODULE(my_module) {
  using namespace boost::python;

  def("printStr", printStr);
}

This compiles fine, but I get a segmentation fault when I attempt to call the function from python (using 3.10):

import my_module

message = "This is a test"

my_module.printStr(message)

The output I get:

BEFORE
Segmentation fault

Can you tell me what I'm doing wrong? Thanks in advance

Activity

  1. stefanseefeld commented on May 17, 2024

    @stefanseefeld
    Member

    The code above looks fine. I suspect the problem stems from the way this is compiled. Can you share the details of your build & execution environment ? I.e., platform, compiler, build commands, etc.

  2. robertapplin commented on May 18, 2024

    @robertapplin
    Author

    The code above looks fine. I suspect the problem stems from the way this is compiled. Can you share the details of your build & execution environment ? I.e., platform, compiler, build commands, etc.

    Thanks for your quick reply!

    I'm on Windows 11, using the compiler shipped with Visual Studio 17 2022, toolset v142. Also I use Ninja as the generator. To compile from the build folder activate my conda environment and I just do

    ninja
    

    I get boost 1.84 from Conda-forge. My CMakeLists file that finds boost has:

    find_package(Python REQUIRED COMPONENTS Development) 
     if(Python_FOUND) 
       include_directories(${Python_INCLUDE_DIRS}) 
     else() 
       message(FATAL_ERROR "Python not found") 
     endif() 
     
    find_package(Boost REQUIRED COMPONENTS python) 
     if(Boost_FOUND) 
       include_directories(${Boost_INCLUDE_DIRS}) 
     else() 
       message(FATAL_ERROR "Boost not found") 
     endif()
    

    I then link boost to the library that requires it. Perhaps I am missing a component of boost or python?

  3. robertapplin commented on May 18, 2024

    @robertapplin
    Author

    My conda env is created using this:

    name: dev-env
    
    channels:
      - conda-forge
    
    dependencies:
      - cmake
      - boost
      - ninja
    
  4. robertapplin commented on May 19, 2024

    @robertapplin
    Author

    I've tried creating a minimal example here, but it seems to be struggling to build. Any ideas why? robertapplin/boost-example#2

  5. MasterBe commented on May 26, 2024

    @MasterBe

    Not sure exact solution but can you try using something other than std::string e.g int, this will likely mean that your code does not find/accept converter for const std::string&. Also why not python 3.11..

  6. robertapplin commented on May 30, 2024

    @robertapplin
    Author

    It works for an int. The debugger seems to be able to read the std::string when I hover over the variable after setting a breakpoint. However it segfaults on the std::cout line

    I've also tried with python 3.10, and 3.11 - the same problem happens

  7. CEXT-Dan commented on Jun 7, 2024

    @CEXT-Dan

    this is what I'm using to print
    https://github.com/CEXT-Dan/PyRx/blob/fd5b8ad47d3fddb50453277ba63ace1d1f51fb6f/PyRxCore/PyAcEd.cpp#L13-L18
    It's not to cout, and I don't remember why I locked the GIL, something for you to try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions