pybind / pybind11

Seamless operability between C++11 and Python
https://pybind11.readthedocs.io/
Other
15.85k stars 2.12k forks source link

[QUESTION] How to use pybind11 libary in c++? #3157

Open nickhuangxinyu opened 3 years ago

nickhuangxinyu commented 3 years ago

I complie a c++ library which can be called by python.

i think it should be able to use for c++, but when i link it.

but i met:

//usr/local/lib/libutil_cpp.so: undefined reference to `PyCMethod_New'
//usr/local/lib/libutil_cpp.so: undefined reference to `PyThread_tss_set'
//usr/local/lib/libutil_cpp.so: undefined reference to `PyThread_tss_create'
//usr/local/lib/libutil_cpp.so: undefined reference to `PyThread_tss_get'
//usr/local/lib/libutil_cpp.so: undefined reference to `PyThread_tss_alloc'
//usr/local/lib/libutil_cpp.so: undefined reference to `PyIndex_Check'

i have linked libpython3.so, is there anything else i need to link?

henryiii commented 3 years ago

I would usually keep the bindings in a separate SO, so that you don't have to depend on libpython. So you would produce two .so files, one with the main library, and the other with the bindings. Then C++ only needs the un-bound one, and doesn't depend on Python. Otherwise you do have to link to libpython. Maybe you are not linking correctly, did you check ldd to see what it thinks it's linking to?

nickhuangxinyu commented 3 years ago

@henryiii thanks, keep two library is a clean choice. for for some reason, i want to use the binded so directly.

ldd looks good.

    linux-vdso.so.1 (0x00007ffec5dc7000)
    libpthread.so.0 => /usr/lib64/libpthread.so.0 (0x00007f154e2e6000)
    libcurl.so.4 => /miniconda3/lib/libcurl.so.4 (0x00007f154e912000)
    libconfig++.so.11 => /usr/local/lib/libconfig++.so.11 (0x00007f154e0ce000)
    libpython3.so => /miniconda3/lib/libpython3.so (0x00007f154e90d000)
    libstdc++.so.6 => /miniconda3/lib/libstdc++.so.6 (0x00007f154df5a000)
    libm.so.6 => /usr/lib64/libm.so.6 (0x00007f154dbd8000)
    libgomp.so.1 => /miniconda3/lib/libgomp.so.1 (0x00007f154e8de000)
    libgcc_s.so.1 => /miniconda3/lib/libgcc_s.so.1 (0x00007f154e8ca000)
    libc.so.6 => /usr/lib64/libc.so.6 (0x00007f154d813000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f154e771000)
    libssh2.so.1 => /miniconda3/lib/./libssh2.so.1 (0x00007f154e887000)
    libssl.so.1.1 => /miniconda3/lib/./libssl.so.1.1 (0x00007f154e7f6000)
    libcrypto.so.1.1 => /miniconda3/lib/./libcrypto.so.1.1 (0x00007f154d547000)
    libgssapi_krb5.so.2 => /miniconda3/lib/./libgssapi_krb5.so.2 (0x00007f154e7a1000)
    libkrb5.so.3 => /miniconda3/lib/./libkrb5.so.3 (0x00007f154d474000)
    libk5crypto.so.3 => /miniconda3/lib/./libk5crypto.so.3 (0x00007f154d45a000)
    libcom_err.so.3 => /miniconda3/lib/./libcom_err.so.3 (0x00007f154d454000)
    libz.so.1 => /miniconda3/lib/./libz.so.1 (0x00007f154d434000)
    librt.so.1 => /usr/lib64/librt.so.1 (0x00007f154d22c000)
    libpython3.9.so.1.0 => /miniconda3/lib/./libpython3.9.so.1.0 (0x00007f154ce55000)
    libdl.so.2 => /usr/lib64/libdl.so.2 (0x00007f154cc51000)
    libkrb5support.so.0 => /miniconda3/lib/././libkrb5support.so.0 (0x00007f154cc42000)
    libresolv.so.2 => /usr/lib64/libresolv.so.2 (0x00007f154ca2b000)
    libutil.so.1 => /usr/lib64/libutil.so.1 (0x00007f154c827000)

I think it may be related to python lib issue.