error C2065: 'socklen_t' : undeclared identifier - c++

The whole error is:
Error 1 error C2065: 'socklen_t' : undeclared identifier c:\users\richard\documents\visual studio 2010\projects\server\server\server.cpp 41 1 Server
This is the problematic line:
int iRcvdBytes=recvfrom(iSockFd, buff, 1024000, 0, (struct sockaddr*)&cliAddr, (socklen_t*)&cliAddrLen);
I have these headers included:
#include <winsock2.h>
#include <windows.h>
#include <direct.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
I have also added WS2_32.lib to the linker in Visual Studio 2010.
What else could cause this problem? I'm just trying to rewrite my simple UDP program to work under Windows.

The socklen_t type is defined inside of WS2tcpip.h in windows. This is not transitively included from winsock2.h (AFAICT). You'll need to include WS2tcpip.h manually in order to use the socklen_t type.

Visual Studio can't find the type socklen_t. MSDN says that this function takes an int* as the last parameter, so cast to that.

Related

Visual Studio doesn't recognize std library

I'm writing an MFC application on Visual Studio 2015 in C++. I added some code which uses members of std library and suppose to take an int and create from it a hex char* with the prefix "0x". I tried to build the project on VS 2015 and VS 2017 from two different computers and I get the same errors - VS doesn't recognize the std library. I've tied running the code on other programs (Clion) and it worked well.
When I include #include <stdlib> I get the following error:
cannot open source file "stdlib"
I've tried re-installing VS, and checked I have all the necessary extensions to support C++, but I guess there's still something missing. How can I fix it?
The code:
std::ostringstream ss;
int i = 7;
ss << std::hex << std::showbase << i;
std::string str = ss.str();
const char *output = str.c_str();
std::cout << output << std::endl;
and included the following headers:
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
I get the following errors:
'Ostringstream': is not a member of 'std'
'Ostringstream': undeclared identifier
'ss': undeclared identifier
'hex': is not a member of 'std'
'showbase': is not a member of 'std'
'string': is not a member of 'std'
'string': undeclared identifier
Thank you.
I've included the headers in the wrong order. In every C++ project in Visual Studio it includes "stdafx.h" library automatically. This library contains many of the commonly used libraries such as <string> and etc. The solution was to write the includes in the following way:
#include "stdafx.h"
// other headers of the form "header.h"
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>
instead of:
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <strstream>
// other headers of the form <header>
#include "stdafx.h"
// other headers of the form "header.h"
a bit more about this in this question
Thanks for everyone who tried to help, I appreciate your time and attention.

Visual Studio Professional 2013, Update 5, "No Target Architecture"

I have a CPP project in Visual Studio, I'm getting the following message when I try to build the solution:
1>c:\program files (x86)\windows kits\8.1\include\um\winnt.h(147): fatal error C1189: #error : "No Target Architecture"
The contents of stdafx.h:
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
#include <ctime>
#include <time.h>
#include <iostream>
#include <profileapi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <Windows.h>
What's missing?
In the end I found that removing the headers:
profileapi.h
sys/types.h
Resolved the issue and the project now builds without warning or error.
Thank you Dai, the better solution:
#include "targetver.h"
#include <Windows.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <chrono>
#include <ctime>
#include <time.h>
#include <fstream>
#include <profileapi.h>
#include <sys/stat.h>
#include <sys/types.h>

Compilation errors with socket bind function

At the very beginning I include the following files and everything goes fine with the bind function
#include "stdafx.h"
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
bind function
iResult = bind(ListenSocket, result->ai_addr, (int)result->ai_addrlen);
Things start messing up when I include the mysql libraries as well
#include "stdafx.h"
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
//mysql connections
#include "mysql_connection.h"
#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
I started debug and it give me this error
IntelliSense: no suitable conversion function from "std::_Bind<false, void, SOCKET &, sockaddr *&, int>" to "int" exists
error C2440: '=' : cannot convert from 'std::_Bind<false,void,SOCKET &,sockaddr *&,int>' to 'int'
Please help.
I guess the problem is not in including the Mysql library, i got this problem once and i fixed it removing the
using namespace std;
or another solution is to call bind from the global namespace to avoid this kind of problems;
i forgot to write that calling bind from the global namespace is done like this :
::bind(...);
Hope this helps.

Compile errors when attempting to link <boost\property_tree\json_parser.hpp>

I have the following "includes" file in my project.
#pragma once
//glm
#include <glm\glm.hpp>
#include <glm\ext.hpp>
#include <glm\gtc\matrix_transform.hpp>
//glew
#include "GL\glew.h"
//glfw
#define GLFW_DLL
#include "GLFW\glfw3.h"
//libpng
#include <png.h>
//std
#include <stdio.h>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <list>
#include <memory>
#include <iostream>
#include <fstream>
#include <assert.h>
//boost
#include <boost\filesystem.hpp>
#include <boost\property_tree\json_parser.hpp> /* problem */
//mandala
#include "types.h"
#include "type_traits.h"
#include "hash.h"
#include "macros.h"
When I include <boost\property_tree\json_parser.hpp>, I get many errors indicating that I'm redefining APIENTRY such as this one:
1>c:\program files (x86)\windows kits\8.0\include\shared\minwindef.h(130): warning C4005: 'APIENTRY' : macro redefinition
I'm perplexed as to why this is happening. I've tried to suppress the minwindef.h file from being processed by putting #define _MINWINDEF_ before the include statement but to no avail. Has anyone else encountered this or have any idea how I can properly include this boost library?
NOTE
Since youd did neither update your question to reflect the changes to the includes you made, nor provide the whole warning message, I can only guess:
You still have glfw.h included before the boost lib that includes the WinAPI header. Because when I just google for "APIENTRY redefinition", I get this SO question as first result, including the answer: Put the WinAPI header (or the boost header includign them) before the glfw.h include.
You may want to include also ptree.
#include <boost/property_tree/ptree.hpp>

C++ header files for UDP in Windows?

I have a linux applications which sends data over UDP protocol. It uses these header files:
#include <stdio.h>
/* standard C i/o facilities */
#include <stdlib.h>
/* needed for atoi() */
#include <unistd.h>
/* defines STDIN_FILENO, system calls,etc */
#include <sys/types.h> /* system data type definitions */
#include <sys/socket.h> /* socket specific definitions */
#include <netinet/in.h> /* INET constants and stuff */
#include <arpa/inet.h> /* IP address conversion stuff */
#include <netdb.h>
#include <string.h> /* for string and memset etc */
/* gethostbyname */
#include <iostream>
#include <fstream>
#include <opencv/highgui.h>
#include <opencv/cv.h>
#include <opencv/cxcore.h>
I want to make a WIndows version of my app. But some of the above header files do not work in WIndows, especially those for UDP.
Which header files should I substitute them for in Windows (Visual Studio 2010)?
UPDATE:
Ok, so my header now looks like this:
#include <iostream>
#include <fstream>
#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <winsock2.h>
I get this error when trying to compile (and many other similar errors):
Error 13 error C2011: 'fd_set' : 'struct' type redefinition c:\program files (x86)\microsoft sdks\windows\v7.0a\include\winsock2.h 132 1 Client
At compile-time, you need to use Winsock2.h instead of the Unix headers.
At link-time, include ws2_32.lib to provide linkage to the required system DLL.
Comment out the include files that are "missing"
or put them into the following:
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <winsock2.h>
#include <windows.h>
#else
// unix includes here
#endif
You don't need most of those includes. The only file you will need is winsock2.h and link with ws2_32.lib.
So, for all networking stuff, just include winsock2.h.
You want to #include winsock2.h. One peculiarity, you need to #include before including anything else, including :
#include <winsock2.h>
#include <windows.h>