Skip to content

Commit 7771212

Browse files
authored
CMakeLists.txt: Fix Android pthread linkage (bytecodealliance#3591)
https://developer.android.com/ndk/guides/stable_apis#core_cc > Note that on Android, unlike Linux, there are no separate libpthread or librt libraries. That functionality is included directly in libc, which does not need to be explicitly linked against. Set `THREADS_PREFER_PTHREAD_FLAG` ON and find package `Threads`, then link `${CMAKE_THREAD_LIBS_INIT}` instead of `-lpthread`. ps. https://cmake.org/cmake/help/latest/module/FindThreads.html
1 parent 1f94cd4 commit 7771212

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,15 @@ endif ()
142142

143143
include (${SHARED_DIR}/utils/uncommon/shared_uncommon.cmake)
144144

145+
set (THREADS_PREFER_PTHREAD_FLAG ON)
146+
find_package(Threads REQUIRED)
147+
145148
# STATIC LIBRARY
146149
if (WAMR_BUILD_STATIC)
147150
add_library(iwasm_static STATIC ${WAMR_RUNTIME_LIB_SOURCE})
148151
set_target_properties (iwasm_static PROPERTIES OUTPUT_NAME vmlib)
149152
target_include_directories(iwasm_static INTERFACE ${WAMR_ROOT_DIR}/core/iwasm/include)
150-
target_link_libraries (iwasm_static INTERFACE ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
153+
target_link_libraries (iwasm_static INTERFACE ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl ${CMAKE_THREAD_LIBS_INIT})
151154
if (WAMR_BUILD_WASM_CACHE EQUAL 1)
152155
target_link_libraries(iwasm_static INTERFACE boringssl_crypto)
153156
endif ()
@@ -160,7 +163,7 @@ if (WAMR_BUILD_SHARED)
160163
add_library (iwasm_shared SHARED ${WAMR_RUNTIME_LIB_SOURCE})
161164
set_target_properties (iwasm_shared PROPERTIES OUTPUT_NAME iwasm)
162165
target_include_directories(iwasm_shared INTERFACE ${WAMR_ROOT_DIR}/core/iwasm/include)
163-
target_link_libraries (iwasm_shared INTERFACE ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl -lpthread)
166+
target_link_libraries (iwasm_shared INTERFACE ${LLVM_AVAILABLE_LIBS} ${UV_A_LIBS} -lm -ldl ${CMAKE_THREAD_LIBS_INIT})
164167
if (WAMR_BUILD_WASM_CACHE EQUAL 1)
165168
target_link_libraries(iwasm_shared INTERFACE boringssl_crypto)
166169
endif ()

0 commit comments

Comments
 (0)