Solved – Undefined reference to ‘pthread_create’ in Linux with Explanation

By | April 5, 2018

When a programmer uses pthread_create function program to create threads in a C program, then he/she may receive “undefined reference to ‘pthread_create’” error. This error mainly occurs on Linux platform while compiling a C Program in G++ or GCC.

undefined reference to pthread_create


Working Solution for Undefined reference to ‘pthread_create’ error in C – Linux

So the solution to this error is
1) Make sure you have added #include <pthread.h>” in your header
Add -pthread while compiling the program

2) Some people suggest adding -lpthread while compiling the program. It may work for some people. Adding -lpthread will only link the pthread library and it will not define the pre-defined macros too.



On the other side, adding -pthread will link the library and it will configure the compilation for threads too. So it is always recommended to use -pthread instead of -lpthread.


Alternative Solution

If you are using eclipse, then you can add this setting.
1) properties >> c/c++Build >> setting >> GCC C++ linker >> libraries
2) Add -pthread at the top.


Sample code of C that produces this error

#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>

void *new_thread (void *arg)
	printf("A new Thread Created!");
	return arg;

int main ()
	pthread_t tid;
	int checking;
	checking = pthread_create(&tid, NULL, new_thread, NULL);
	if (checking !=0)
	perror("create Thread");

Save the code as T1.c

Now compile the code by following the below commands.

gcc T1.c -o T1 -pthread



A new Thread Created

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.