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

Static linking using Cmake #466

Closed
amitibo opened this issue Oct 26, 2016 · 5 comments
Closed

Static linking using Cmake #466

amitibo opened this issue Oct 26, 2016 · 5 comments

Comments

@amitibo
Copy link

amitibo commented Oct 26, 2016

Hi,

I am trying to (cmake) compile a binding to a library that uses a another library (dlib). This library is compiled for static linking. Is it possible to set pybind11 to use static linking?

Thanks,
Amit

@wjakob
Copy link
Member

wjakob commented Oct 26, 2016

Python extension modules have to be dynamic. However, you can have a dynamic pybind11 module that links to a static library.

Best,
Wenzel

On Oct 26, 2016, at 3:04 PM, Amit Aides notifications@github.com wrote:

Hi,

I am trying to (cmake) compile a binding to a library that uses a another library (dlib). This library is compiled for static linking. Is it possible to set pybind11 to use static linking?

Thanks,
Amit


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub #466, or mute the thread https://github.com/notifications/unsubscribe-auth/ABJdrTlpZDs5PXsnuXb-bcAAjo-Rzgkpks5q30_TgaJpZM4KhJJ3.

{"api_version":"1.0","publisher":{"api_key":"05dde50f1d1a384dd78767c55493e4bb","name":"GitHub"},"entity":{"external_key":"github/pybind/pybind11","title":"pybind/pybind11","subtitle":"GitHub repository","main_image_url":"https://cloud.githubusercontent.com/assets/143418/17495839/a5054eac-5d88-11e6-95fc-7290892c7bb5.png","avatar_image_url":"https://cloud.githubusercontent.com/assets/143418/15842166/7c72db34-2c0b-11e6-9aed-b52498112777.png","action":{"name":"Open in GitHub","url":"https://github.com/pybind/pybind11"}},"updates":{"snippets":[{"icon":"DESCRIPTION","message":"Static linking using Cmake (#466)"}],"action":{"name":"View Issue","url":"https://github.com/pybind/pybind11/issues/466"}}}

@amitibo
Copy link
Author

amitibo commented Oct 26, 2016

I am linking using CMake, following the docs example:

ADD_SUBDIRECTORY(pybind11)
PYBIND11_ADD_MODULE(pyface pyface.cpp dummy.cpp)
TARGET_LINK_LIBRARIES(pyface dlib ${OpenCV_LIBS})

When running make, I get the following error:

Linking CXX shared module pyface.so
/usr/bin/ld: dlib_build/libdlib.a(threads_kernel_shared.o): relocation R_X86_64_32 against `_ZN4dlib21threads_kernel_shared14thread_starterEPv' can not be used when making a shared object; recompile with -fPIC
dlib_build/libdlib.a: could not read symbols: Bad value

If I understand correctly, it tries to do dynamic linking with the dlib library which is compiled for static linking. How can I request static linking with the dlib library when creating the pyface module?

Thanks,
Amit

@dean0x7d
Copy link
Member

Add the following line to enable -fPIC for your dependency:

set_target_properties(dlib PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

Note that this really isn't specific to pybind11. It's general build system and linking for any dynamic library.

@wjakob wjakob closed this as completed Oct 27, 2016
@surabhi96
Copy link

How to link pybind11 module that links to a static library? I tried TARGET_LINK_LIBRARIES but I seem to be using it wrongly.
Thanks!

@Sberm
Copy link

Sberm commented Aug 15, 2023

Add the following line to enable -fPIC for your dependency:

set_target_properties(dlib PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

Note that this really isn't specific to pybind11. It's general build system and linking for any dynamic library.

Thank you so much sir

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

5 participants