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