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

#include <mutex> causes the bind() function call to throw errors at compile time, why?

我以前没见过这个问题,甚至不确定如何寻找这个问题的解决方案,因为我什至不确定要寻找什么。我有一些代码使用套接字 read/write 到我的 linux 设备驱动程序。此代码已经过测试并按原样工作。

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

这是我 #include <mutex> 并且我的代码中没有任何其他更改后导致问题的行

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

dp_comm.cpp: In function 'int setup_socket_to_dev(const char*, int*)': dp_comm.cpp:160:64: error: no match for 'operator<' in 'std::bind(_Functor&&, _ArgTypes&& ...) [with _Functor = int&, _ArgTypes = {sockaddr*, unsigned int}, typename std::_Bind_helper<_Functor, _ArgTypes>::type = std::_Bind]((* &((sockaddr*)(& saddr))), (* &20u)) < 0'

我还尝试创建一个 int 并使其等于 bind()

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

dp_comm.cpp: In function 'int setup_socket_to_dev(const char*, int*)': dp_comm.cpp:160:71: error: cannot convert 'std::_Bind_helper::type {aka std::_Bind}' to 'int' in initialization

为什么 #include <mutex> 导致我的编译器在 bind() 调用时出现异常?

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

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

怎么回事?

你可能有

using namespace std;

在您的代码中的某处,which you shouldn't do. The bind() socket function is being confused with std::bind()<functonal> 中定义(并且 <mutex> 很可能包括)。

如果您绝对不能删除 using namespace std;,那么您可以通过在全局命名空间中添加 :: 前缀来消除 select 函数 bind() 的歧义,即 ::bind(...).