antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

Cpp: std::call_once requires linking to pthreads (on Linux)

Open Krzmbrzl opened this issue 2 years ago • 2 comments

When using the C++ target, the generated lexer will use a call to std::call_once in its initialize method. However, without further action, this will throw std::runtime_error with the message Unknown error -1.

Thanks to https://stackoverflow.com/questions/51584960/stdcall-once-throws-stdsystem-error-unknown-error-1, I figured out that it is necessary to link against pthreads, since std::call_once requires a function from it that is not found otherwise (triggering the error).

The following cmake snippet is enough to accomplish this:

find_package(Threads REQUIRED)
target_link_libraries(myTarget PRIVATE Threads::Threads)

Now, I think that this dependency should be resolved on ANTLR's side such that for any downstream user it is enough to just link against the ANTLR runtime and thanks to cmake's magic the dependency will propagate. Thus, my suggestion would be to link ANTLR's C++ runtime against pthreads using above snippet.

At this point, I am not sure whether on other platforms other libraries might be required in order for this to work. Does someone have experience with C++ ANTLR on macOS and Windows?

EDIT: Based on the docs it seems that above cmake snippet is actually cross-platform and not pthread-specific, so maybe we don't have to pay any special attention here?

Krzmbrzl avatar May 09 '22 13:05 Krzmbrzl

You are a life-saver

zhaochenyuangit avatar Jul 17 '22 12:07 zhaochenyuangit

Since this appears to affect multiple users and nobody seems to want to answer my questions here, I went ahead and created a PR as I see fit: #3794

Krzmbrzl avatar Jul 18 '22 05:07 Krzmbrzl