1

I am attempting to pass a float16 ndarray from Python to C++ using pybind11. While the compilation succeeds, running the Python code results in an error regarding an undefined symbol. Below are the relevant code snippets and environment details.

Error Message:

check_dtype.cpython-310-x86_64-linux-gnu.so: undefined symbol: _ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE15_M_replace_coldEPcmPKcmm

(demangles as std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace_cold(char*, unsigned long, char const*, unsigned long, unsigned long))

C++ Code:

#include <iostream>
#include <stdfloat>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>

namespace py = pybind11;

void check_array(py::array_t<std::float16_t> arr) {
    auto r = arr.unchecked<1>();  // Read-only access to NumPy array
    for (ssize_t i = 0; i < r.shape(0); ++i) {
        std::cout << "Element " << i << " has a size of " << sizeof(r(i)) << " bytes." << std::endl;
    }
}

PYBIND11_MODULE(check_dtype, m) {
    m.def("check_array", &check_array, "A function to check the byte size of array elements.");
}

Python Code:

import numpy as np
import check_dtype

a = np.array([1.0, 2.0, 3.0], dtype=np.float16)
check_dtype.check_array(a)

Environment:

  • G++ version: 13.3.0
  • Python version: 3.10.15
  • pybind11 version: 2.13.6
  • OS: Ubuntu 22.04

Has anyone encountered this issue before or has insights on how to resolve the undefined symbol error? Any help would be greatly appreciated!

I attempted to pass a float16 ndarray from Python to a C++ extension module using pybind11. Given the successful compilation, I expected the program to run smoothly and print the size of each element in the array in bytes.

3
  • How are you building the extension module? Do you have a setup.py or similar? Commented Nov 29, 2024 at 4:54
  • This is the command in the shell: g++ -g -Wall -shared -std=c++2b -fPIC `python3 -m pybind11 --includes` check_dtype.cpp -o check_dtype`python3-config --extension-suffix` `python3-config --ldflags` Commented Dec 1, 2024 at 3:07
  • When you run ldd check_dtype.<WHATEVER>.so what libstdc++ is it linking against? Since you're using c++23 features it's possible that it's trying to link with an older and incompatible standard library. This may be useful. Commented Dec 2, 2024 at 3:17

0

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.