Undefined reference to pthread_create in Linux
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Understanding the "Undefined Reference to pthread_create" Error in Linux
When you're developing multi-threaded applications in Linux using C or C++, a common issue you might encounter is the linker error: "undefined reference to pthread_create." This error often puzzles beginners, but it has a straightforward explanation and solution.
Technical Explanation
The pthread_create function is part of the POSIX Threads (pthreads) library, which provides APIs for creating and managing threads in Unix-like operating systems, including Linux. When you compile your C or C++ code that uses pthreads, you must link it against the pthreads library. The "undefined reference" error surfaces typically due to not linking this library during the compilation process.
Common Scenario
Consider the following minimal example of a C program:
If you compile this program using a simple gcc command:
This will result in:
Reason Behind the Error
The error emerges because the pthread library (libpthread) is a separate entity and does not automatically get linked with your program unless explicitly specified. pthread_create and all other pthread-related functions reside in this library.
Solution: Linking with -pthread
The error can be resolved by linking your program with the pthread library using the -pthread option when compiling. The corrected command will be:
Alternatively, you can use -lpthread, which specifically links with the pthread library:
Summary Table
| Step | Action |
| Identify the error | When the compilation fails with "undefined reference to pthread_create". |
| Understand the cause | Caused by missing link to the pthread library (libpthread). |
| Correct the command | Add -pthread or -lpthread during compilation: |
| Compilation command | gcc my_program.c -o my_program -pthread
or
gcc my_program.c -o my_program -lpthread |
| Result | Program compiles successfully, allowing you to use pthread functions. |
Additional Details and Considerations
- Order of Options: When using
-lpthread, ensure-lpthreadappears after the source files in thegcccommand. This order specifies that the linker should resolve symbols in those files using the pthread library. -pthreadvs-lpthread: The-pthreadoption is generally recommended because it may also set other compiler-specific flags that configure the environment for threading (e.g., defining macros or activating certain kernel features), providing compatibility across different systems and compilers.- Static vs Dynamic Linking: By default, using
-lpthreador-pthreadresults in dynamic linking. If you require static linking, additional options need to be specified, like-static. However, this can complicate deployment due to potential portability issues. - Compiler Settings: Some Integrated Development Environments (IDEs) or build systems like
CMakewill require explicit settings adjustments to add-pthread. Configuring the appropriate build flags or libraries in your build system scripts is crucial. - Thread Safety: Ensure your program accounts for thread-safety issues. Just linking with pthread doesn't automatically protect shared resources, so use proper synchronization mechanisms like mutexes and condition variables where necessary.
With these insights, you should be able to address the undefined reference to pthread_create error effectively and understand the context of linking against external libraries in Linux. Happy coding!
Related reading
- Understanding async in perl on specific example
- Understanding async with WebClient UploadString vs UploadStringTaskAsync
- Understanding ConfigureAwait
- Understanding dispatch_async
- Understanding Scope and Lifetime of References in stdasync within a Loop
- Understanding stdhardware_destructive_interference_size and stdhardware_constructive_interference_size
- Understanding java.lang.Thread.State WAITING parking
- understanding of pthread_cond_wait and pthread_cond_signal
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.