Skip to content

Commit

Permalink
Updated steps for preempting the target Thread::Thread in h5ppConfig.…
Browse files Browse the repository at this point in the history
…cmake.in

* Some builds of HDF5 can link directly to -lpthread causing problems downstream.
  • Loading branch information
DavidAce committed Jun 1, 2021
1 parent f153c6d commit 763edb6
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions cmake/h5ppConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,22 @@ list(INSERT CMAKE_MODULE_PATH 0 "${CMAKE_CURRENT_LIST_DIR}")
include(CMakeFindDependencyMacro)


########################## IMPORTANT #############################
##################################################################
### Preempt Threads::Threads ###
### It's looked for in dependency configs, so we make it right ###
### It's looked for in dependencies, so we make it right ###
### before it's done wrong, i.e. with pthread instead of ###
### -lpthread. If this is undesirable you can preempt it ###
### yourself similarly. The reason is that downstream I often ###
### need to avoid a static linking segfault by using: ###
### -Wl, -whole-archive pthread -Wl, -no-whole-archive ###
### and cmake is unable to identify "-lpthread" as being the ###
### same as pthread --> multiple declaration errors. Note that ###
### CMake changes it into ###
### -Wl, -whole-archive -lpthread -Wl, -no-whole-archive ###
### anyway. If an entire dependency tree had only "-lpthread" ###
### or only "pthread" this wouldn't be a problem, but ###
### unfortunately different dependencies do it differently. ###
### Since conan prefers "pthread", we do the same here. ###
### -lpthread. ###
### Otherwise, we get linker errors consuming h5pp ###
### Here we specify the linking twice ###
### 1) As string to make sure -lpthread gets sandwiched by ###
### -Wl,--whole-archive.... -Wl,--no-whole-archive ###
### 2) As usual to make sure that if somebody links ###
### Threads::Threads, then any repeated pthread appended ###
### to the end (the wrong order causes linking errors) ###
##################################################################
### Read more about pthread segfault
### https://stackoverflow.com/questions/35116327/when-g-static-link-pthread-cause-segmentation-fault-why
if(NOT TARGET Threads::Threads)
set(CMAKE_THREAD_PREFER_PTHREAD FALSE)
set(THREADS_PREFER_PTHREAD_FLAG FALSE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
find_package(Threads)
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set_target_properties(Threads::Threads PROPERTIES INTERFACE_LINK_LIBRARIES pthread)
endif()
endif()


Expand Down

0 comments on commit 763edb6

Please sign in to comment.