问题
According to MSDN (VS2012 version), sockaddr_in
is defined in winsock2.h
. There are no exceptions stated (similar to what we see with Windows Phone and some API calls). Yet when I try to compile a file which uses sockaddr_in
and includes winsock2.h
, I get a compile error (below).
Notice that I don't receive a winsock2.h not found
error (or similar). Also note that the same code compiles on Windows Phone 8.
What does one need to include for WinRT?
cl /Fotmp32dll\bss_conn.obj -Iinc32 -Itmp32dll -DOPENSSL_THREADS -W3 -
Gs0 -GF -Gy -nologo -DOPENSSL_SYSNAME_WINRT -DWIN32_LEAN_AND_MEAN -DL_ENDIAN -D_
CRT_SECURE_NO_DEPRECATE -I\usr\local\ssl\fips-2.0/include /nologo /D NDEBUG /D _
USRDLL /D _WINDLL /D WINAPI_FAMILY=WINAPI_PARTITION_APP /FI SDKDDKVer.h /FI wina
pifamily.h -DOPENSSL_NO_RC5 -DOPENSSL_NO_MD2 -DOPENSSL_NO_KRB5 -DOPENSSL_NO_ENGI
NE -DOPENSSL_NO_HW -DOPENSSL_FIPS -DOPENSSL_NO_JPAKE -DOPENSSL_NO_STATIC_ENGINE
/Zi /Fdtmp32dll/lib -DOPENSSL_BUILD_SHLIBCRYPTO -c .\crypto\bio\bss_conn.c
bss_conn.c
.\crypto\bio\bss_conn.c(95) : error C2079: 'them' uses undefined struct 'sockadd
r_in'
.\crypto\bio\bss_conn.c(207) : error C2224: left of '.sin_family' must have stru
ct/union type
.\crypto\bio\bss_conn.c(207) : error C2065: 'AF_INET' : undeclared identifier
.\crypto\bio\bss_conn.c(208) : error C2224: left of '.sin_port' must have struct
/union type
.\crypto\bio\bss_conn.c(208) : warning C4013: 'htons' undefined; assuming extern
returning int
.\crypto\bio\bss_conn.c(214) : error C2224: left of '.sin_addr' must have struct
/union type
.\crypto\bio\bss_conn.c(214) : warning C4013: 'htonl' undefined; assuming extern
returning int
.\crypto\bio\bss_conn.c(217) : warning C4013: 'socket' undefined; assuming exter
n returning int
.\crypto\bio\bss_conn.c(217) : error C2065: 'AF_INET' : undeclared identifier
.\crypto\bio\bss_conn.c(217) : error C2065: 'SOCK_STREAM' : undeclared identifie
r
.\crypto\bio\bss_conn.c(217) : error C2065: 'IPPROTO_TCP' : undeclared identifie
r
.\crypto\bio\bss_conn.c(218) : error C2065: 'INVALID_SOCKET' : undeclared identi
fier
.\crypto\bio\bss_conn.c(220) : warning C4013: 'WSAGetLastError' undefined; assum
ing extern returning int
.\crypto\bio\bss_conn.c(260) : warning C4013: 'connect' undefined; assuming exte
rn returning int
.\crypto\bio\bss_conn.c(366) : error C2065: 'INVALID_SOCKET' : undeclared identi
fier
.\crypto\bio\bss_conn.c(379) : error C2065: 'INVALID_SOCKET' : undeclared identi
fier
.\crypto\bio\bss_conn.c(383) : warning C4013: 'shutdown' undefined; assuming ext
ern returning int
.\crypto\bio\bss_conn.c(384) : warning C4013: 'closesocket' undefined; assuming
extern returning int
.\crypto\bio\bss_conn.c(385) : error C2065: 'INVALID_SOCKET' : undeclared identi
fier
.\crypto\bio\bss_conn.c(422) : warning C4013: 'WSASetLastError' undefined; assum
ing extern returning int
.\crypto\bio\bss_conn.c(423) : warning C4013: 'recv' undefined; assuming extern
returning int
.\crypto\bio\bss_conn.c(447) : warning C4013: 'send' undefined; assuming extern
returning int
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio 11.0
\VC\bin\x86_ARM\cl.EXE"' : return code '0x2'
Stop.
回答1:
Note that as of Visual Studio 2013 Update 4, winsock is available to use in Windows Store apps. (This doesn't apply to the original question, but I'm adding this in case other people find this question in the future).
回答2:
The definition of sockaddr_in is in the file winsock2.h. In my situation, I got the compiler error "error C2065: 'sockaddr_in' : undeclared identifier", so I open the file winsock2.h to check out the definition of sockaddr_in, then I found out the reason.
/*
* Socket address, internet style.
*/
struct sockaddr_in {
short sin_family;
u_short sin_port;
struct in_addr sin_addr;
char sin_zero[8];
};
Above code is definition of sockaddr_in. I define my variables in the wrong way:
sockaddr_in sin;
The right way is:
struct sockaddr_in sin;
回答3:
That's a linker error, not a compiler error.
.h files aren't involved in the linking process. You need to include some .lib file that has that in it.
// Need to link with Ws2_32.lib, Mswsock.lib, and Advapi32.lib
http://msdn.microsoft.com/en-us/library/windows/desktop/ms737591(v=vs.85).aspx
edit: oops, yes it is the compiler. Still, check out the source code I linked to
来源:https://stackoverflow.com/questions/18120275/where-is-sockaddr-in-in-windows-rt