#include <mutex> 导致 bind() 函数调用在编译时抛出错误,为什么?



我以前从未见过这个问题,甚至不确定如何搜索这个问题的解决方案,因为我甚至不知道该寻找什么。我有一些代码使用套接字来读/写我的linux设备驱动程序。此代码经过测试并按原样运行。

出于线程安全的原因,我想在程序中添加一个互斥对象,但一旦我将#include <mutex>添加到程序的main.cpp文件中,并且仅在那一行,<sys/socket.h> bind()函数就会在编译时抛出错误。。。

这是导致问题的一行,当我#include <mutex>并且在我的代码中没有更改任何其他内容时

if (bind(sock_fd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)

dp_comm.cpp:在函数"int setup_socket_to_dev(const char*,int*("中:dp_comm.cpp:160:64:错误:"operator<"不匹配在里面'std::bind(_Functor&,_ArgTypes&&…([其中_Functor=int&,_ArgTypes={sockaddr*,unsigned int},typename std::_Bind_helper&lt_函数,_ArgTypes>::类型=std::_Bind]((*&((sockaddr*((&saddr(((,(*&20u((<0'

我还试图创建一个int,并使其等于bind()的返回值

int ret_bind = bind(sock_fd, (struct sockaddr *)&saddr, sizeof(saddr));

dp_comm.cpp:在函数"int setup_socket_to_dev(const char*,int*("中:dp_comm.cpp:160:71:错误:无法在初始化中将"std::_Bind_helper::type{aka std::_Bind}"转换为"int">

为什么#include <mutex>会导致我的编译器在bind()调用时出错

当我#include <mutex>时,代码编译的唯一方法是以下

bind(sock_fd, (struct sockaddr *)&saddr, sizeof(saddr));

怎么回事?

您可能有

using namespace std;

bind()套接字函数与<functonal>中定义的std::bind()(<mutex>很可能包含该函数(混淆。

如果您绝对不能删除using namespace std;,那么您可以在全局名称空间中选择bind()函数,方法是在其前面加上::,即::bind(...)

相关内容

最新更新