I have written a multithread server program in C, which echoes back all the data that a client sends.
我已经用C编写了一个多线程服务器程序,它回显客户端发送的所有数据。
Initially, I used poll()
function in my program to detect POLLRDHUP
event, for that I defined _GNU_SOURCE
macro (This event is defined here).
最初,我在程序中使用poll()函数来检测POLLRDHUP事件,因为我定义了_GNU_SOURCE宏(此事件在这里定义)。
Later I updated my code & removed poll()
function, however I forgot to remove _GNU_SOURCE
macro.
稍后,我更新了我的代码& remove poll()函数,但是我忘记了删除_GNU_SOURCE宏。
Now my code is finally complete (and a little long to post, more than 250 lines). Before removing macro I was compiling my program using:
现在,我的代码终于完成了(而且发布时间有点长,超过250行)。在删除宏之前,我使用以下方法编译我的程序:
gcc multi_thread_socket_v4.c -Wall -Werror -g -lpthread -o multi_thread_socket
and it worked fine: No errors, no warnings
它运行得很好:没有错误,没有警告
After I removed the macro definition, and compiled using same command-line, the output of gcc was:
在删除宏定义并使用相同的命令行编译后,gcc的输出是:
multi_thread_socket_v4.c: In function ‘main’:
multi_thread_socket_v4.c:194: warning: implicit declaration of function ‘pthread_mutexattr_settype’
multi_thread_socket_v4.c:194: error: ‘PTHREAD_MUTEX_ERRORCHECK’ undeclared (first use in this function)
multi_thread_socket_v4.c:194: error: (Each undeclared identifier is reported only once
multi_thread_socket_v4.c:194: error: for each function it appears in.)
I have included all the required libraries as it worked fine initially.
我已经包含了所有需要的库,因为它最初运行良好。
I peeked into pthread.h
at /usr/include/pthread.h
and found out this:
我看了pthread。h在/usr/include/pthread.h发现了这个:
/* Mutex types. */
enum
{
PTHREAD_MUTEX_TIMED_NP,
PTHREAD_MUTEX_RECURSIVE_NP,
PTHREAD_MUTEX_ERRORCHECK_NP,
PTHREAD_MUTEX_ADAPTIVE_NP
#ifdef __USE_UNIX98
,
PTHREAD_MUTEX_NORMAL = PTHREAD_MUTEX_TIMED_NP,
PTHREAD_MUTEX_RECURSIVE = PTHREAD_MUTEX_RECURSIVE_NP,
PTHREAD_MUTEX_ERRORCHECK = PTHREAD_MUTEX_ERRORCHECK_NP,
PTHREAD_MUTEX_DEFAULT = PTHREAD_MUTEX_NORMAL
#endif
#ifdef __USE_GNU
/* For compatibility. */
, PTHREAD_MUTEX_FAST_NP = PTHREAD_MUTEX_TIMED_NP
#endif
};
and this:
这:
#ifdef __USE_UNIX98
/* Return in *KIND the mutex kind attribute in *ATTR. */
extern int pthread_mutexattr_gettype (__const pthread_mutexattr_t *__restrict
__attr, int *__restrict __kind)
__THROW __nonnull ((1, 2));
/* Set the mutex kind attribute in *ATTR to KIND (either PTHREAD_MUTEX_NORMAL,
PTHREAD_MUTEX_RECURSIVE, PTHREAD_MUTEX_ERRORCHECK, or
PTHREAD_MUTEX_DEFAULT). */
extern int pthread_mutexattr_settype (pthread_mutexattr_t *__attr, int __kind)
__THROW __nonnull ((1));
I checked out here to check if __USE_UNIX98
is a feature test macro, but it was not there.
我在这里检查__USE_UNIX98是否是一个特性测试宏,但是它不在那里。
So please help me understanding the reasons for the error, because the function & the macro where gcc
shows error are defined in POSIX standard. I do not know what more info regarding my problem will be required so please tell me, I will update my question.
请帮助我理解错误的原因,因为在POSIX标准中定义了gcc显示错误的函数和宏。我不知道关于我的问题需要更多的信息,所以请告诉我,我会更新我的问题。
3 个解决方案
#1
4
You should use
你应该使用
#define _POSIX_C_SOURCE 200112L
if you want to use POSIX features such as pthread_mutexattr_settype ... see http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_02.html
如果您想使用POSIX特性,如pthread_mutexattr_settype…参见http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_02.html
Another possibility is
另一种可能性是
#define _XOPEN_SOURCE 700
See http://man7.org/linux/man-pages/man7/feature_test_macros.7.html and http://pubs.opengroup.org/onlinepubs/9699919799/
看到http://man7.org/linux/man-pages/man7/feature_test_macros.7.html和http://pubs.opengroup.org/onlinepubs/9699919799/
Setting _GNU_SOURCE includes POSIX and lots of other definitions.
设置_GNU_SOURCE包含POSIX和许多其他定义。
P.S. I would expect that including <pthread.h>
includes <features.h>
, which by default defines _POSIX_C_SOURCE as 200112L, but it's possible that you have defined something that overrides that ... see /usr/include/features.h on your system for details of the symbols and their usage.
附注:我希望包括
#2
1
It doesn't, your problem likely lies elsewhere.
事实并非如此,你的问题可能在别处。
I just compiled a trivial program with the following content:
我刚刚用以下内容编译了一个简单的程序:
#include <pthread.h>
int main(int argc, char **argv)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
return 0;
}
This compiles perfectly with gcc -pthread -Wall -Werror a.c
.
这是用gcc -pthread -Wall -Werror a.c完美编译的。
It's possible that another part of your program causes this, by eg. doing something silly like defining _PTHREAD_H
, or some other minor sabotage.
有可能是程序的另一部分导致了这种情况。做一些傻事,比如定义_PTHREAD_H,或者做一些其他的小破坏。
You might want to try to get a minimal test case by using a tool like delta or creduce, which will probably make the problem evident.
您可能希望通过使用delta或creduce之类的工具来获得最小的测试用例,这可能会使问题变得明显。
#3
1
When you're using old libraries (e.g. 2.1.x) you should use
当你使用旧的库(如2.1.1 .x)时,你应该使用
#define __USE_UNIX98
Using a macro beginning with "__" it's not usually a good idea, but sometimes it's the only way... see also this discussion
用“__”开头的宏通常不是个好主意,但有时这是唯一的办法……也看到这个讨论
#1
4
You should use
你应该使用
#define _POSIX_C_SOURCE 200112L
if you want to use POSIX features such as pthread_mutexattr_settype ... see http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_02.html
如果您想使用POSIX特性,如pthread_mutexattr_settype…参见http://pubs.opengroup.org/onlinepubs/007904975/functions/xsh_chap02_02.html
Another possibility is
另一种可能性是
#define _XOPEN_SOURCE 700
See http://man7.org/linux/man-pages/man7/feature_test_macros.7.html and http://pubs.opengroup.org/onlinepubs/9699919799/
看到http://man7.org/linux/man-pages/man7/feature_test_macros.7.html和http://pubs.opengroup.org/onlinepubs/9699919799/
Setting _GNU_SOURCE includes POSIX and lots of other definitions.
设置_GNU_SOURCE包含POSIX和许多其他定义。
P.S. I would expect that including <pthread.h>
includes <features.h>
, which by default defines _POSIX_C_SOURCE as 200112L, but it's possible that you have defined something that overrides that ... see /usr/include/features.h on your system for details of the symbols and their usage.
附注:我希望包括
#2
1
It doesn't, your problem likely lies elsewhere.
事实并非如此,你的问题可能在别处。
I just compiled a trivial program with the following content:
我刚刚用以下内容编译了一个简单的程序:
#include <pthread.h>
int main(int argc, char **argv)
{
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK);
return 0;
}
This compiles perfectly with gcc -pthread -Wall -Werror a.c
.
这是用gcc -pthread -Wall -Werror a.c完美编译的。
It's possible that another part of your program causes this, by eg. doing something silly like defining _PTHREAD_H
, or some other minor sabotage.
有可能是程序的另一部分导致了这种情况。做一些傻事,比如定义_PTHREAD_H,或者做一些其他的小破坏。
You might want to try to get a minimal test case by using a tool like delta or creduce, which will probably make the problem evident.
您可能希望通过使用delta或creduce之类的工具来获得最小的测试用例,这可能会使问题变得明显。
#3
1
When you're using old libraries (e.g. 2.1.x) you should use
当你使用旧的库(如2.1.1 .x)时,你应该使用
#define __USE_UNIX98
Using a macro beginning with "__" it's not usually a good idea, but sometimes it's the only way... see also this discussion
用“__”开头的宏通常不是个好主意,但有时这是唯一的办法……也看到这个讨论