cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
samling
Visitor
Visitor
6,525 Views
Registered: ‎01-30-2017

SDK linux app error: undefined reference to `pthread_create'

Hi All,

I'm new to linux app design. 

I tried a simple thread application with SDK 2016.2. And have a error:

/main.c:33: undefined reference to `pthread_create'

 

Can anyone to help? Thx

 

Hereby the code I used FYI.

 

#include <iostream>
#include <cstdlib>
#include <pthread.h>

using namespace std;

#define NUM_THREADS 5

void *PrintHello(void *threadid) {
long tid;
tid = (long)threadid;
cout << "Hello World! Thread ID, " << tid << endl;
pthread_exit(NULL);
}

 

int main () {
pthread_t threads[NUM_THREADS];
int rc;
int i;

for( i=0; i < NUM_THREADS; i++ ){
cout << "main() : creating thread, " << i << endl;
rc = pthread_create(&threads[i], NULL, PrintHello, (void *)i);

if (rc){
cout << "Error:unable to create thread," << rc << endl;
exit(-1);
}
}

pthread_exit(NULL);
}

 

 

 

0 Kudos
6 Replies
balkris
Xilinx Employee
Xilinx Employee
6,516 Views
Registered: ‎08-01-2008

You can add -lpthread to the gcc command in your make file in the application.
Thanks and Regards
Balkrishan
--------------------------------------------------------------------------------------------
Please mark the post as an answer "Accept as solution" in case it helped resolve your query.
Give kudos in case a post in case it guided to the solution.
samling
Visitor
Visitor
6,511 Views
Registered: ‎01-30-2017

Thanks for your reply. 

yes, I did added the option -lpthread .  Still have the same error.

 

 

'Invoking: ARM Linux g++ compiler'
arm-xilinx-linux-gnueabi-g++ -Wall -O0 -g3 -lpthread -c -fmessage-length=0 -MT"main.o" -MMD -MP -MF"main.d" -MT"main.o" -o "main.o" "../main.c"
../main.c: In function 'int main()':

0 Kudos
shabbirk
Moderator
Moderator
6,500 Views
Registered: ‎12-04-2016

Can you try this:

arm-xilinx-linux-gnueabi-gcc -Wall -O0 -g3 -lpthread -c -fmessage-length=0 -MT"main.o" -MMD -MP -MF"main.d" -MT"main.o" -o "main.o"

 

 

Regards

Shabbir

0 Kudos
shabbirk
Moderator
Moderator
6,492 Views
Registered: ‎12-04-2016

Hey sorry, ignore my previous response. you can try adding -lpthread in the attached path:

 

Right-click on application properties -> C/C++ Build -> Settings -> Tool Settings tab -> GCC C++ Linker -> Miscellaneous

 

flag.PNG
0 Kudos
joe4702
Contributor
Contributor
6,467 Views
Registered: ‎08-21-2012

I believe you also need to link the rt library.

0 Kudos
thiruparan
Visitor
Visitor
1,979 Views
Registered: ‎11-09-2018

did you solve the issue,

I am using sdk 2018.2 , i am getting undefined refernce to pthread_create 

 

i did this , still i am getting the same error any thouts 

"To add this, right click on the gpio_test in Project explorer, and select C/C++ Build Properties. Under ARM Linux gcc linker  Inferred Options  Software Platform. Select the Ad symbol, and add the –lpthread option."

 

0 Kudos