PJSIP and Winsock2 API C2039 error

Good day!

I worked on client-server application that uses PJSIP library for SIP calls.

It was necessary to add client-server connection based on websockets. I used great websocketpp for server and Casablanca C++ REST SDK for client. Everything went well until I met sudden compile error:

boost_1_56_0/boost/asio/detail/impl/socket_ops.ipp(2191): error C2039: 's_addr' : is not a member of 'in_addr'

There was no reason for that. I went to google and found only three links that gave me the right direction:

I tryed many things like define WIN32_LEAN_AND_MEAN globally for solution or include winsock2.h before windows.h before websocketpp and casablanca headers, but it doesn't helps.

Then I turned on /P command line parameter (which is for "preprocess to a file") for my c++ source file that gives an error to dig deeper. I found that there was no s_addr in in_addr struct. There was macro s_addr which redirects to S_un.S_addr. But in precompiled file I found that macro has not been translated into S_un.S_addr and was unchanged: s_addr.

Then I turned on /C command line parameter (which is for "keep comments") for my c++ source file. And BINGO! I found comment

/* Must undefine s_addr because of pj_in_addr below */

just before including winsock2.h! It was in pjlib core library: pjproject/pjlib/include/pj/sock.h. Yes, I used PJSIP headers before including websocketpp! And, sadly, these headers actually do #undef s_addr.

Actually, I used pjproject/pjsip/include/pjsua2/persistent.hpp header for reading settings. But in fact that header includes all-the-things from PJSIP!

So, I fixed my C++ source file by rearranging order of includes: first websocketpp headers, then PJSIP headers.

Beware dark side of PJSIP :)

Happy bug-hunting!

Comments