Skip to content

Commit

Permalink
Cpp: Link to threads library
Browse files Browse the repository at this point in the history
As detailed in #3708, it is necessary to link against the (p)threads
library in order to be able to use std::call_once without producing
linker errors.

Since this function is frequently used in ANTLR's cpp version, this
commit ensures that the respective library is always linked against in
order to avoid this error, even if downstream users are not explicitly
linking against an appropriate threads library.

Fixes #3708

Signed-off-by: Robert Adam <dev@robert-adam.de>

Co-authored-by: Bryan Tan <Technius@users.noreply.github.com>
  • Loading branch information
Krzmbrzl and Technius committed Aug 28, 2022
1 parent 88b8e76 commit f55d251
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions runtime/Cpp/cmake/antlr4-runtime.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ set(ANTLR_VERSION @ANTLR_VERSION@)
set_and_check(ANTLR4_INCLUDE_DIR "@PACKAGE_ANTLR4_INCLUDE_DIR@")
set_and_check(ANTLR4_LIB_DIR "@PACKAGE_ANTLR4_LIB_DIR@")

include(CMakeFindDependencyMacro)
find_dependency(Threads)

include(${CMAKE_CURRENT_LIST_DIR}/@targets_export_name@.cmake)

check_required_components(antlr)
7 changes: 7 additions & 0 deletions runtime/Cpp/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ file(GLOB libantlrcpp_SRC
add_library(antlr4_shared SHARED ${libantlrcpp_SRC})
add_library(antlr4_static STATIC ${libantlrcpp_SRC})

# Make sure to link against threads (pthreads) library in order to be able to
# make use of std::call_once in the code without producing runtime errors
# (see also https://github.com/antlr/antlr4/issues/3708 and/or https://stackoverflow.com/q/51584960).
find_package(Threads REQUIRED)
target_link_libraries(antlr4_shared Threads::Threads)
target_link_libraries(antlr4_static Threads::Threads)

if (ANTLR_BUILD_CPP_TESTS)
include(FetchContent)

Expand Down

0 comments on commit f55d251

Please sign in to comment.