代码之家  ›  专栏  ›  技术社区  ›  Fred

“错误:在为64位编译时,pthread.h上的“{”标记“之前应为标识符或“(”

  •  0
  • Fred  · 技术社区  · 7 年前

    我发现了之前关于此错误消息的问题,但它们与我的情况不匹配,以下是场景:

    环境 :Windows 8.1和根据配置64位GCC的代码块=> How do I compile for 64bit using G++ w/ CodeBlocks?

    问题 :测试中列出的基本多线程简单程序=> https://www.geeksforgeeks.org/multithreading-c-2/ 具体如下:

    #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);
    }
    

    在标准32位中编译此文件可以完美地完成并工作,但在使用 MinGW-64 ( 如何使用带有代码块的G++64位进行编译? )它失败,pthread上出现4个错误。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|
    

    不言而喻 pthread。h类 未以任何方式进行修改或更改。错误指向四个 #定义 线:

    /* 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;  })
    

    其他程序编译并正确运行64位,运行时在进程的详细信息中显示。 我不明白为什么只有在编译64位时,相同的未修改的头文件才会显示错误。生成日志显示:

    -------------- 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))
    

    我承认我只是这些领域(多线程、64位程序)的初学者,所以如果我的问题对专家来说很明显,我深表歉意,但经过长时间的研究,我在之前的帖子中找不到任何帮助,我陷入了困境。

    非常感谢您提供的任何帮助。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Peter Cordes    7 年前

    使用64位MinGW pthread.h 而不是复制或以其他方式使用 pthread。h类 来自32位MinGW( like you were apparently doing ).

    像这样的低级头文件是特定于编译器的,包括编译器版本和配置。