What are the correct link options to use std::thread in GCC under linux?
I think on Linux pthread is used to implement std::thread
so you need to specify the -pthread
compiler option.
As this is a linking option, this compiler option need to be AFTER the source files:
$ g++ -std=c++0x test.cpp -pthread
Compiling multithread code with g++
The answer was provided by a kind member of SO C++ chat.
It looks like this behaviour is caused by a bug in gcc.
The workaround provided in the last comment of that bug discussion does work and solves the issue:
-Wl,--no-as-needed
Which C++ standard library headers invoke a requirement for the -pthread option with GCC?
<thread> <mutex> <condition_variable> <future> <shared_mutex> <stop_token>
all use Pthreads types and functions.
The Networking TS headers such as <experimental/net>
and <experimental/executor>
use <mutex>
and <future>
so they also depend on Pthreads.
We'll soon be adding more concurrency headers such as <latch> <barrier> <semaphore>
.
Basically anything related to concurrency and synchronization, except <atomic>
which notably does not depend on anything from Pthreads.
On the website: godbolt.org, and only there: how can I use std::thread?
In all likelyhood, thread creation is disabled on godbolt.org on purpose (to prevent denial of service attacks or other abuse), so there is currently no way to use std::thread on that service.
Related Topics
Why Is Conversion from String Constant to 'Char*' Valid in C But Invalid in C++
Is Floating-Point Addition and Multiplication Associative
Boost Asio Async_Write: How to Not Interleaving Async_Write Calls
Why Will Std::Sort Crash If the Comparison Function Is Not as Operator ≪
How to Specialize Std::Hash≪Key≫::Operator() For User-Defined Type in Unordered Containers
What How to Use Instead of the Arrow Operator, '-≫'
Calculate the Factorial of an Arbitrarily Large Number, Showing All the Digits
Best C++ Ide or Editor For Windows
Is Stateful Metaprogramming Ill-Formed (Yet)
Printing 1 to 1000 Without Loop or Conditionals
Why Do We Need Extern "C"{ #Include ≪Foo.H≫ } in C++
Is It a Conforming Compiler Extension to Treat Non-Constexpr Standard Library Functions as Constexpr
Why Are Function Pointers and Data Pointers Incompatible in C/C++
How to Validate User Input as a Double in C++
How to Forward Declare an Inner Class
Is It Safe to Link C++17, C++14, and C++11 Objects
Are "Anonymous Structs" Standard? And, Really, What *Are* They