我正按照说明here尝试“实施”跨平台互斥锁:
这是我的代码:
#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 :(得分:1)
看起来gcc没有在#ifdef
中看到宏定义。我认为__linux__
是要测试的正确宏。甚至可以更好地测试POSIX中的宏,而不仅仅是Linux。
编辑:可能最好是_XOPEN_SOURCE
的测试。 POSIX强制在包含任何标头之前定义它。
答案 1 :(得分:1)