cmultithreading64-bitx86-64windows-server-2008-x64

"error: expected identifier or '(' before '{' token" on pthread.h when compiling for 64-bits


I've found previous questions about this error message but they don't match my case, this is the scenario:

Environment: Windows 8.1 and CodeBlocks with 64-bit GCC configured per => How do I compile for 64bit using G++ w/ CodeBlocks?

Issue: Testing basic multi-threading, simple program listed in => https://www.geeksforgeeks.org/multithreading-c-2/ as follows:

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

// A normal C function that is executed as a thread 
// when its name is specified in pthread_create()
void *myThreadFun(void *vargp)
{
    sleep(1);
    printf("Printing GeeksQuiz from Thread \n");
    return NULL;
}

int main()
{
    pthread_t tid;
    printf("Before Thread\n");
    pthread_create(&tid, NULL, myThreadFun, NULL);
    pthread_join(tid, NULL);
    printf("After Thread\n");
    exit(0);
}

compiling this in standard 32-bit completes and works flawlessly, but when compiling with MinGW-64 (How do I compile for 64bit using G++ w/ CodeBlocks?) it fails with 4 errors on pthread.h :

pthread.h|418|error: expected identifier or '(' before '{' token|
pthread.h|428|error: expected identifier or '(' before '{' token|
pthread.h|438|error: expected identifier or '(' before '{' token|
pthread.h|447|error: expected identifier or '(' before '{' token|

Goes without saying pthread.h is not being modified or altered in any way. the errors point to the four #define lines:

/* Recursive API emulation.  */
#undef localtime_r
#define localtime_r(_Time, _Tm) ({ struct tm *___tmp_tm;        \
                        pthread_testcancel();   \
                        ___tmp_tm = localtime((_Time));\
                        if (___tmp_tm) {    \
                          *(_Tm) = *___tmp_tm;  \
                          ___tmp_tm = (_Tm);    \
                        }           \
                        ___tmp_tm;  })

#undef gmtime_r
#define gmtime_r(_Time,_Tm) ({ struct tm *___tmp_tm;        \
                        pthread_testcancel();   \
                        ___tmp_tm = gmtime((_Time)); \
                        if (___tmp_tm) {    \
                          *(_Tm) = *___tmp_tm;  \
                          ___tmp_tm = (_Tm);    \
                        }           \
                        ___tmp_tm;  })

#undef ctime_r
#define ctime_r(_Time,_Str) ({ char *___tmp_tm;         \
                        pthread_testcancel();   \
                        ___tmp_tm = ctime((_Time));  \
                        if (___tmp_tm)      \
                         ___tmp_tm =        \
                           strcpy((_Str),___tmp_tm); \
                        ___tmp_tm;  })

#undef asctime_r
#define asctime_r(_Tm, _Buf)    ({ char *___tmp_tm;         \
                        pthread_testcancel();   \
                        ___tmp_tm = asctime((_Tm)); \
                        if (___tmp_tm)      \
                         ___tmp_tm =        \
                           strcpy((_Buf),___tmp_tm);\
                        ___tmp_tm;  })

Other programs compile and run correctly for 64-bits, showing it in the details for the process when running. I don't understand how the same unmodified header file shows errors only when compiling for 64-bits. The build log shows:

-------------- Build: Debug in MultiThreadGCCx64 (compiler: GNU GCC Compiler x64)---------------

x86_64-w64-mingw32-gcc.exe -Wall -g -std=c99 -m64 -I"C:\Program Files (x86)\CodeBlocks\MinGW" -c Z:\CTemp\CProjects\MultiThreadGCCx64\main.c -o obj\Debug\main.o
In file included from Z:\CTemp\CProjects\MultiThreadGCCx64\main.c:11:0:
Z:\CTemp\CProjects\MultiThreadGCCx64\pthread.h:418:34: error: expected identifier or '(' before '{' token
 #define localtime_r(_Time, _Tm) ({ struct tm *___tmp_tm;  \
                                  ^
Z:\CTemp\CProjects\MultiThreadGCCx64\pthread.h:428:30: error: expected identifier or '(' before '{' token
 #define gmtime_r(_Time,_Tm) ({ struct tm *___tmp_tm;  \
                              ^
Z:\CTemp\CProjects\MultiThreadGCCx64\pthread.h:438:30: error: expected identifier or '(' before '{' token
 #define ctime_r(_Time,_Str) ({ char *___tmp_tm;   \
                              ^
Z:\CTemp\CProjects\MultiThreadGCCx64\pthread.h:447:31: error: expected identifier or '(' before '{' token
 #define asctime_r(_Tm, _Buf) ({ char *___tmp_tm;   \
                               ^
Z:\CTemp\CProjects\MultiThreadGCCx64\main.c: In function 'myThreadFun':
Z:\CTemp\CProjects\MultiThreadGCCx64\main.c:19:5: warning: implicit declaration of function 'sleep'; did you mean '_sleep'? [-Wimplicit-function-declaration]
     sleep(1);
     ^~~~~
     _sleep
Process terminated with status 1 (0 minute(s), 0 second(s))
4 error(s), 1 warning(s) (0 minute(s), 0 second(s))

I confess I'm just a beginner in these areas (multi-threading, 64-bit programs) so apologies if what I'm asking is very obvious to the experts, but after considerable time researching this I cannot find any help in previous posts and I'm stuck.

Many thanks in advance for any assistance you can provide.


Solution

  • Use 64-bit MinGW's pthread.h instead of copying or otherwise using the pthread.h from 32-bit MinGW (like you were apparently doing).

    Low-level header files like this are specific to the compiler, including compiler version and configuration.