摘要
使用pthreads-w32庫時,提示“timespec”:“struct”類型重定義的錯誤,添加預編譯關鍵字HAVE_STRUCT_TIMESPEC
解決問題。
問題
圖像處理過程中使用pthreads-w32多線程庫(下載),使用Visual Studio 2017編譯時報錯:
***\pthreads-w32-2-9-1-release\Pre-built.2\include\pthread.h(320): error C2011: “timespec”:“struct”類型重定義
***\ucrt\time.h(39): note: 參見“timespec”的聲明
打開兩個重定義對應的文件,看到
// pthread.h
#if !defined(HAVE_STRUCT_TIMESPEC)
#define HAVE_STRUCT_TIMESPEC
#if !defined(_TIMESPEC_DEFINED)
#define _TIMESPEC_DEFINED
struct timespec {
time_t tv_sec;
long tv_nsec;
};
#endif /* _TIMESPEC_DEFINED */
#endif /* HAVE_STRUCT_TIMESPEC */
// ucrt\time.h
#ifndef _CRT_NO_TIME_T
struct timespec
{
time_t tv_sec; // Seconds - >= 0
long tv_nsec; // Nanoseconds - [0, 999999999]
};
#endif
於是添加預處理器定義:HAVE_STRUCT_TIMESPEC
,重新編譯,問題解決。
添加路徑:"項目屬性頁"->"C/C++"->"預處理器"->"預處理器定義"
或者利用cmake生成工程時,直接添加定義即可:
add_definitions("-DHAVE_STRUCT_TIMESPEC")
pthread-w32下載地址
https://sourceforge.net/projects/pthreads4w/files/
ftp://sourceware.org/pub/pthreads-win32/dll-latest