Link error undefined reference to `pthread_atfork`
Closed this issue · 1 comments
I am trying to use your project to build openssl for arm64
. It largely works. Our (private) repo has dozen or so targets that depend on openssl
, and many of then compile and link fine. But a few are failing with an error like this:
opt/openssl-cmake/usr/local/lib/libcrypto.a(threads_pthread.o): In function `fork_once_func':
threads_pthread.c:(.text+0x18): undefined reference to `pthread_atfork'
It seems that CMake
is unable to enforce a dependency from crypto
to pthread
. For some apps, the link order is coincidentally correct, but for other apps the link order is wrong, and this error occurs.
I have tried modifying the CMakeLists.txt
. The simplest change that I thought might work was just to declare the dependency on pthread
like this:
target_link_libraries(crypto INTERFACE crypto_lib pthread)
That doesn't seem to work. It might work if we could make the dependency be on crypto
instead of crypto_lib
, but that fails because crypto
is not built in this project.
I'm reasonably well-versed with CMake but not a true expert. Do you see a way forward here?
I think I have a somewhat hacky workaround, which is to build openssl
in a separate repo, and then just check in the headers had libraries into our main repo and wrap then as a CMake STATIC IMPORTED
target. But if you know how to solve this it would be nice to eliminate that hack.
Could you provide us with a minimal example for reproduction? Your changes look correct to me, maybe there is a ordering issue of linked libraries. Could you run a verbose make and check the order of linked libraries in the final linker command?