How to use pthread functions on Colibri iMX6ULL

Hi. I’m using iMX6ULL 512MB WB IT and I want to use pthread functions for multi-tasking. I followed one example code in Toradex community but there is a compile error. Please let me know how to build pthread functions.

My compiler Settings are the followings.

Cross-compilation:

. /usr/local/oecore-x86_64/environment-setup-armv7at2hf-neon-angstrom-linux-gnueabi

C/C++ Build/Settings

  - Cross GCC compiler : ${CC}
       Miscellaneous : ${CFLAGS} -c
  - Cross GCC Linker : ${CXX}
       Miscellaneous : ${LDFLAGS}

The following is my code.

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h> 
#include <termios.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <pthread.h>

void *print_message_function( void *ptr );
void test(void);

int main(int argc, char *argv[])
{
   test();
}

void test(void)
{
pthread_t thread1, thread2;
char *message1 = "Thread 1";
char *message2 = "Thread 2";
int  iret1, iret2;

/* Create independent threads each of which will execute function */
iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);

/* Wait till threads are complete before main continues. Unless we  */
/* wait we run the risk of executing an exit which will terminate   */
/* the process and all threads before the threads have completed.   */
pthread_join( thread1, NULL);
pthread_join( thread2, NULL);

printf("Thread 1 returns: %d\n",iret1);
printf("Thread 2 returns: %d\n",iret2);
exit(0);
}

void *print_message_function( void *ptr )
{
     char *message;
     message = (char *) ptr;
     printf("%s \n", message);
}

Invoking: Cross GCC Linker

arm-angstrom-linux-gnueabi-g++ -march=armv7-a -mthumb -mfpu=neon -mfloat-abi=hard --sysroot=/usr/local/oecore-x86_64/sysroots/armv7at2hf-neon-angstrom-linux-gnueabi -Wl,-O1 -Wl,–hash-style=gnu -Wl,–as-needed -o “hello-world” ./src/hello-world.o

…/src/hello-world.c:117: error: undefined reference to ‘pthread_create’

makefile:30: recipe for target ‘hello-world’ failed

…/src/hello-world.c:118: error: undefined reference to ‘pthread_create’

…/src/hello-world.c:124: error: undefined reference to ‘pthread_join’

…/src/hello-world.c:125: error: undefined reference to ‘pthread_join’

collect2: error: ld returned 1 exit status

make: *** [hello-world] Error 1

Hi @HansKim72

Thanks for writing to the Toradex Community!

./src/hello-world.c:117: error: undefined reference to ‘pthread_create’

Did you add pthread to your linker libraries as explained in this thread?

Best regards,
Jaski

Hi

If your program needs code from a library you have to tell the linker the name of the library to link against. Usually that is done with the “-l” option.

pthread is special as there is a special option “-pthread” which should be given to both compiler and linker.

So you have to add -pthread to CFLAGS and LDFLAGS.

Max

Thank you. Settled.

Perfect that it works. Thanks for the feedback.

If you’re building in eclipse neon to build and configure imx6ull. Go to properties - c/c++ build - settings - crossGccCompiler - miscellaneous. Then make sure you have pthread added in both CFLAGS and LDFLAGS.
It should look like this.

${CFLAGS} -c -pthread //for CFLAGS
${LDFLAGS} -pthread //for LDFLAGS

As stated in the answer pthread must be added to both the compiler and the linker respectively.

Hi @BenjFTL

Welcome to the Toradex Community!
Thanks for your valuable Input.

Best regards,
Jaski