0

I haven't seen this issue before and wasn't exactly sure how to even search for a solution to this problem because i'm not sure what to even look for. I have some code using sockets to read/write to my linux device driver. This code is tested and works as is.

I would like to add a mutex to my program for thread safety reasons but as soon as I add #include <mutex> to my program's main.cpp file and ONLY that line the <sys/socket.h> bind() function throws an error at compile time...

Here is the line causing the problem once I #include <mutex> and change nothing else in my code

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'

I also tried to create an int and have it equal the return value of bind()

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

Why does #include <mutex> cause my compiler to throw a fit at the bind() call?

The only way the code will compile when I #include <mutex> is the following

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

What's going on?

RAZ_Muh_Taz
  • 4,059
  • 1
  • 13
  • 26
  • 2
    Do you have a `using namespace std;` in your code? `bind` is a name in that namespace but also probably a name in your socket implementation. –  Dec 10 '18 at 22:21
  • yes the code i'm editing does `using namespace std` – RAZ_Muh_Taz Dec 10 '18 at 22:22
  • 1
    Well, the answer is simple - don't do that. –  Dec 10 '18 at 22:22
  • yeah, it's not my code, and it's basically one of those things where someone made some prototype and then it was never touched because it "worked" and I'm trying to add functionality to show proof of concept but also change little as possible. I will be refactoring it and I plan remove `using namespace std` – RAZ_Muh_Taz Dec 10 '18 at 22:25
  • I nominated for reopen because the question is fundamentally different: answers to the linked duplicate do not answer this question or explain how to make the code work even if `using namespace std;` cannot be removed. (There may be _another_ question of which this is a duplicate, but in my opinion the linked duplicate is not it.) – TypeIA Dec 10 '18 at 22:28

1 Answers1

3

You probably have

using namespace std;

somewhere in your code, which you shouldn't do. The bind() socket function is being confused with std::bind() which is defined in <functonal> (and which <mutex> may well include).

If you absolutely cannot remove using namespace std; then you can disambiguate to select the bind() function in the global namespace by prefixing it with ::, i.e., ::bind(...).

TypeIA
  • 16,916
  • 1
  • 38
  • 52