How to fix C++ error "undefined reference to `pthread_rwlock_init'"

Problem:

When compiling your C++ application, you see one or more of the following or similar error messages (complete error message log example shown below)

threads_pthread.c:(.text+0x4a): undefined reference to `pthread_rwlock_init'
threads_pthread.c:(.text+0x89): undefined reference to `pthread_rwlock_rdlock'
threads_pthread.c:(.text+0xa9): undefined reference to `pthread_rwlock_wrlock'
threads_pthread.c:(.text+0xc9): undefined reference to `pthread_rwlock_unlock'
threads_pthread.c:(.text+0xee): undefined reference to `pthread_rwlock_destroy'
threads_pthread.c:(.text+0x129): undefined reference to `pthread_once'
threads_pthread.c:(.text+0x149): undefined reference to `pthread_key_create'
threads_pthread.c:(.text+0x17b): undefined reference to `pthread_setspecific'
threads_pthread.c:(.text+0x19b): undefined reference to `pthread_key_delete'
threads_pthread.c:(.text+0x207): undefined reference to `pthread_once'
threads_pthread.c:(.text+0x167): undefined reference to `pthread_getspecific'

Solution

You need to link the pthread library to your executable. When running gcc manually, use the -lpthread option, for example:

gcc -o myexe main.cpp -lpthread

When using CMake, use

add_executable(myexe main.cpp)
target_link_libraries(myexe pthread)

Full error log example:

/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_lock_new':
threads_pthread.c:(.text+0x4a): undefined reference to `pthread_rwlock_init'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_read_lock':
threads_pthread.c:(.text+0x89): undefined reference to `pthread_rwlock_rdlock'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_write_lock':
threads_pthread.c:(.text+0xa9): undefined reference to `pthread_rwlock_wrlock'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_unlock':
threads_pthread.c:(.text+0xc9): undefined reference to `pthread_rwlock_unlock'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_lock_free':
threads_pthread.c:(.text+0xee): undefined reference to `pthread_rwlock_destroy'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_run_once':
threads_pthread.c:(.text+0x129): undefined reference to `pthread_once'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_init_local':
threads_pthread.c:(.text+0x149): undefined reference to `pthread_key_create'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_set_local':
threads_pthread.c:(.text+0x17b): undefined reference to `pthread_setspecific'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_cleanup_local':
threads_pthread.c:(.text+0x19b): undefined reference to `pthread_key_delete'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `openssl_init_fork_handlers':
threads_pthread.c:(.text+0x207): undefined reference to `pthread_once'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(threads_pthread.o): in function `CRYPTO_THREAD_get_local':
threads_pthread.c:(.text+0x167): undefined reference to `pthread_getspecific'
/usr/bin/ld: /home/uli/.conan/data/openssl/1.1.1k/_/_/package/6af9cc7cb931c5ad942174fd7838eb655717c709/lib/libcrypto.a(dso_dlfcn.o): in function `dlfcn_globallookup':