I'm trying to "implement" a cross-platform mutex following the instructions here:
And here is my code:
#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 <pth开发者_如何学JAVAread.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
but what I got after running gcc was:
~/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
by the way, on linux, pthread_mutex_t is defined as:
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;
What did I do wrong?
have you defined LINUX or WINDOWS define in your Makefile?
try
gcc -dM -E - < /dev/null
this prints all the predefined macros.
and here the windows macros.
It looks that gcc doesn't see the macro definition in the #ifdef
. I think __linux__
is the correct macro to test. Or even better test for the macros from POSIX and not for Linux alone.
Edit: Probably the best is test for _XOPEN_SOURCE
. POSIX imposes that this is defined before any header is included.
精彩评论