【问题标题】:"error: expected identifier or '(' before '{' token" on pthread.h when compiling for 64-bits编译 64 位时 pthread.h 上的“错误:预期标识符或 '(' 之前的 '{' 标记”
【发布时间】:2018-01-02 13:17:42
【问题描述】:

我发现了有关此错误消息的先前问题,但它们与我的情况不符,这就是场景:

环境:Windows 8.1 和配置了 64 位 GCC 的 CodeBlocks => 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 (How do I compile for 64bit using G++ w/ CodeBlocks?) 编译时,它会在 pthread.h 上出现 4 个错误:

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 没有以任何方式被修改或更改。错误指向四个 #define 行:

/* 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 位程序)的初学者,所以如果我的要求对专家来说非常明显,我深表歉意,但经过大量时间研究后,我找不到任何帮助在以前的帖子中,我被卡住了。

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

【问题讨论】:

  • 能否提供构建日志?
  • 谢谢。我在原始帖子的末尾添加了构建日志。
  • 不要从谁知道哪里复制你自己的 pthread.h,使用 MinGW 提供的那个。
  • pthread.h 文件的来源是什么?每个环境的文件是否相同?
  • 谢谢汉斯和格哈德。您对特定文件和位置的建议让我意识到我使用了错误的版本。 x64 版本的 pthread.h 位于不同的文件夹中。新手误区。问题已解决

标签: c multithreading 64-bit x86-64 windows-server-2008-x64


【解决方案1】:

使用 64 位 MinGW 的 pthread.h,而不是从 32 位 MinGW (like you were apparently doing) 复制或以其他方式使用 pthread.h

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

【讨论】:

  • 是的,就是这样。无法相信这样的菜鸟错误。谢谢大家。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-28
  • 2012-02-19
相关资源
最近更新 更多