2011-06-20 95 views
0

我想「落實」中的說明here一個跨平臺的互斥:「錯誤:在XXX之前預期說明符 - 限定符列表」?

這裏是我的代碼:

#ifndef __SIMPLEAV_CORE_UTIL_SAMUTEX_H_DEFINED__ 
#define __SIMPLEAV_CORE_UTIL_SAMUTEX_H_DEFINED__ 

/* 
* A simple cross-platform (currently only on linux and win) mutex. 
* 
* usage: 
* SAMutex mutex; 
* SAMutex_init(&mutex); 
* SAMutex_lock(&mutex); 
* SAMutex_unlock(&mutex); 
* SAMutex_destroy(&mutex); 
* 
* all functions return 0 on success, -1 on error. 
*/ 

#if defined(LINUX) 
    #include <pthread.h> 
    //typedef pthread_mutex_t SAMutex; 
    #define SAMutex pthread_mutex_t 
#elif defined(WINDOWS) 
    #include <windows.h> 
    #include <process.h> 
    //typedef HANDLE SAMutex; 
    #define SAMutex HANDLE 
#endif 

int SAMutex_init(SAMutex *); 
int SAMutex_lock(SAMutex *); 
int SAMutex_unlock(SAMutex *); 
int SAMutex_destroy(SAMutex *); 

#endif 

但我跑GCC後得到的是:

~/git/SimpleAV/build $ make 
[ 20%] Building C object CMakeFiles/player2.dir/player2.c.o 
In file included from /home/wecing/git/SimpleAV/include/SimpleAV/core/core.h:4, 
       from /home/wecing/git/SimpleAV/include/SimpleAV/SDL/api.h:5, 
       from /home/wecing/git/SimpleAV/player2.c:4: 
/home/wecing/git/SimpleAV/include/SimpleAV/core/util/SAMutex.h:28: error: expected ‘)’ before ‘*’ token 
/home/wecing/git/SimpleAV/include/SimpleAV/core/util/SAMutex.h:29: error: expected ‘)’ before ‘*’ token 
/home/wecing/git/SimpleAV/include/SimpleAV/core/util/SAMutex.h:30: error: expected ‘)’ before ‘*’ token 
/home/wecing/git/SimpleAV/include/SimpleAV/core/util/SAMutex.h:31: error: expected ‘)’ before ‘*’ token 
In file included from /home/wecing/git/SimpleAV/include/SimpleAV/SDL/api.h:5, 
       from /home/wecing/git/SimpleAV/player2.c:4: 
/home/wecing/git/SimpleAV/include/SimpleAV/core/core.h:28: error: expected specifier-qualifier-list before ‘SAMutex’ 
make[2]: *** [CMakeFiles/player2.dir/player2.c.o] Error 1 
make[1]: *** [CMakeFiles/player2.dir/all] Error 2 
make: *** [all] Error 2 

順便說一下,在Linux,pthread_mutex_t定義爲:

typedef union 
{ 
    struct __pthread_mutex_s 
    { 
    int __lock; 
    unsigned int __count; 
    int __owner; 
#if __WORDSIZE == 64 
    unsigned int __nusers; 
#endif 
    /* KIND must stay at this position in the structure to maintain 
     binary compatibility. */ 
    int __kind; 
#if __WORDSIZE == 64 
    int __spins; 
    __pthread_list_t __list; 
# define __PTHREAD_MUTEX_HAVE_PREV 1 
#else 
    unsigned int __nusers; 
    __extension__ union 
    { 
     int __spins; 
     __pthread_slist_t __list; 
    }; 
#endif 
    } __data; 
    char __size[__SIZEOF_PTHREAD_MUTEX_T]; 
    long int __align; 
} pthread_mutex_t; 

我做錯了什麼?

+0

既不定義LINUX也不定義WINDOWS? – Lundin

回答

1

你有沒有在你的Makefile中定義LINUX或WINDOWS定義?


嘗試

gcc -dM -E - < /dev/null 

此打印所有的預定義宏。

並且這裏是windows macros

+0

不......我認爲gcc會爲我做這件事。 :-P – wecing

+0

WOW。說真的,我很震驚。謝謝。但是我找不到有關POSIX的宏...也許我應該使用'__unix__'。 – wecing

1

看起來gcc沒有看到#ifdef中的宏定義。我認爲__linux__是正確的宏測試。或者甚至更好地測試POSIX的宏而不是單獨的Linux。

編輯:可能是最好的測試_XOPEN_SOURCE。 POSIX規定這是在包含任何頭文件之前定義的。

+0

謝謝,我明白了。但是我測試了宏_XOPEN_SOURCE,_POSIX_VERSION和__USE_POSIX ......它們都不起作用。另一方面,'__unix__'確實有效,但我認爲它不是一個很好的替代品。還有什麼建議? – wecing

相關問題