wjakob / nanobind

nanobind: tiny and efficient C++/Python bindings
BSD 3-Clause "New" or "Revised" License
2.14k stars 161 forks source link

undefined symbol of static class member #611

Closed JiauZhang closed 3 weeks ago

JiauZhang commented 3 weeks ago

Problem description

I defined an class with static class member, and I can successfully compile the code, but there are problems when import it

$ python -c "import _C"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: /mnt/d/code/nnops/build/_C.cpython-39-x86_64-linux-gnu.so: undefined symbol: _ZN6Device8devices_E

The main branch can reproduce this error: https://github.com/JiauZhang/nnops/tree/main

# build and test
git clone https://github.com/JiauZhang/nnops
cd nnops
mkdir build && cd build && cmake .. && make
python -c "import _C"

If I define this static class member https://github.com/JiauZhang/nnops/blob/main/csrc/device.h#L23 at .cpp file, everything is OK. The corresponding code is on branch fix: https://github.com/JiauZhang/nnops/tree/fix

commit diff: https://github.com/JiauZhang/nnops/commit/3a0db85f833c9de21da4943ea1b2624f1a7719f8

Reproducible example code

No response

wjakob commented 3 weeks ago

This is a C++ linking issue that isn't related to nanobind.