Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimal Boost Python example not working #437

Open
robertapplin opened this issue May 17, 2024 · 7 comments
Open

Minimal Boost Python example not working #437

robertapplin opened this issue May 17, 2024 · 7 comments

Comments

@robertapplin
Copy link

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

@stefanseefeld
Copy link
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.

@robertapplin
Copy link
Author

robertapplin commented May 18, 2024

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?

@robertapplin
Copy link
Author

robertapplin commented May 18, 2024

My conda env is created using this:

name: dev-env

channels:
  - conda-forge

dependencies:
  - cmake
  - boost
  - ninja

@robertapplin
Copy link
Author

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

@MasterBe
Copy link

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..

@robertapplin
Copy link
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

@CEXT-Dan
Copy link

CEXT-Dan commented Jun 7, 2024

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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants