E1696 E0020 errors using VS

Pages: 123
Hi All

I am getting below errors while trying to compile the code in vs.
How can I fix it? Should I use _WIN32 instead of WIN32?

E1696 cannot open source file "sys/socket.h"
E1696 cannot open source file "netinet/in.h"
E1696 cannot open source file "unistd.h"
E1696 cannot open source file "netdb.h"
E1696 cannot open source file "arpa/inet.h"
E0020 identifier "sockaddr_in" is undefined
E0020 identifier "gethostbyname" is undefined
E0020 identifier "AF_INET" is undefined




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifdef WIN32
//#include <winsock.h>         // For socket(), connect(), send(), and recv()
//below added by me
#include <WinSock2.h> // For socket(), connect(), send(), and recv()
#include <Ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")

typedef int socklen_t;
typedef char raw_type;       
#else
#include <sys/types.h>       
#include <sys/socket.h>      
#include <netdb.h>           
#include <arpa/inet.h>       
#include <unistd.h>          
#include <netinet/in.h>      
typedef void raw_type;       
#endif

Last edited on
Should I download the pthreads from the below link and add all .h files, .lib files and .dll to visual studio project path?

https://sourceforge.net/projects/pthreads4w/
Last edited on
After modify the code as shown below, I am not seeing that error, but see another set of errors. I changed #ifdef WIN32 to #ifdef _WIN32

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifdef _WIN32
//#include <winsock.h>         // For socket(), connect(), send(), and recv()
//below added by me
#include <WinSock2.h> // For socket(), connect(), send(), and recv()
#include <Ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")

typedef int socklen_t;
typedef char raw_type;       
#else
#include <sys/types.h>       
#include <sys/socket.h>      
#include <netdb.h>           
#include <arpa/inet.h>       
#include <unistd.h>          
#include <netinet/in.h>      
typedef void raw_type;       
#endif
Below are new errors, I see now.


Error C4996 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Error C4996 'gethostbyname': Use getaddrinfo() or GetAddrInfoW() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings

Error C4996 'inet_ntoa': Use inet_ntop() or InetNtop() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings



But after I change the code to the below, its gone.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifdef _WIN32
#include <winsock.h>         // For socket(), connect(), send(), and recv()
//below added by me
//#include <WinSock2.h> // For socket(), connect(), send(), and recv()
//#include <Ws2tcpip.h>
//#pragma comment(lib, "Ws2_32.lib")

typedef int socklen_t;
typedef char raw_type;       
#else
#include <sys/types.h>       
#include <sys/socket.h>      
#include <netdb.h>           
#include <arpa/inet.h>       
#include <unistd.h>          
#include <netinet/in.h>      
typedef void raw_type;       
#endif
Any idea what is the below error?


Error C4996 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

 
 Message.append(strerror(errno));

After I add the below line, the above error is gone.

 
#pragma warning(disable : 4996) //_CRT_SECURE_NO_WARNINGS 


But i got the below error codes in my cout, as shown below. Any idea what it means? How to overcome this errors?

E0349 no operator "<<" matches these operands
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'pthread_t' (or there is no acceptable conversion)

1
2
3
4
5
6
7
8

catch (SocExcept& e) {
        cerr << "Unable to get the port" << endl;
    }
    cout << " thread " << pthread_self() << endl;  ///error occurs at this line.



Last edited on
Since issue seen only with pthread, I suspect in pthread.h, should below struct be changed? is it VS2019 compiler bug in windows? is there any other way to get the thread id of a pthread in c++ windows?

1
2
3
4
5
6
7
8
9
10
11
12

/*
 * Generic handle type - intended to extend uniqueness beyond
 * that available with a simple pointer. It should scale for either
 * IA-32 or IA-64.
 */
typedef struct {
    void * p;                   /* Pointer to actual object */
    unsigned int x;             /* Extra information - reuse count etc */
} ptw32_handle_t;

typedef ptw32_handle_t pthread_t;
Last edited on
Why don't you use std::thread instead of pthread ?
I actually followed steps mentioned in below link to add pthread to vs2019. should i revert back these changes?

https://www.technical-recipes.com/2016/using-posix-threads-in-microsoft-visual-studio/
yes, get rid of it and use c++ threads.
Refactoring to use std::thread is a solution. But your error is that it doesn't know how to use the << operator with cout (std::ostream&) and a pthread_t.

If you got rid of that cout line, see if it compiles. Then, once you get it to compile, explain what you are actually trying to print out with that line.
Last edited on
Ok I have tried to revert those changes mentioned in the below link. But i got the below set of errors in VS2019. what is missing here now?
how can I use C++ threads? Do i need to go to NuGet Packages..>Browse>search and find pthreads >Install? OR use Vcpkg to install and use pthread in Windows? I know that pthread is for Linux POSIX threads and is not combined with VS2019.


Severity Code Description Project File Line Suppression State
Error (active) E1696 cannot open source file "pthread.h"
Error (active) E0020 identifier "pthread_t" is undefined
Error (active) E0020 identifier "pthread_create" is undefined
Error (active) E0020 identifier "pthread_self" is undefined
Error (active) E0020 identifier "pthread_detach" is undefined
Error (active) E0020 identifier "pthread_self" is undefined


After i installed pthread using NuGet, all those above errors are gone, but I got the below error, which was resolved by adding the below line in \packages\pthreads.2.9.1.4\build\native\include\pthread.h

Error C2011'timespec':'struct' type redefinition

#define HAVE_STRUCT_TIMESPEC
Last edited on
below is my code snippet.
I still see the below errors. What could be wrong?
E0349 no operator "<<" matches these operands
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'pthread_t' (or there is no acceptable conversion)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

void TestClient(Socket* sock) {
    cout << "Testing client ";
    try {
        cout << sock->getAddr() << ":";
    }
    catch (SocketExcept& e) {
        cerr << "Unable to get address" << endl;
    }
    try {
        cout << sock->getPort();
    }
    catch (SocketExcept& e) {
        cerr << "Unable to get port" << endl;
    }
    cout << " with thread " << pthread_self() << endl;  //ERROR occurs in this line.

pthread_t may be an integer, or it may be a pointer.
It's an opaque type you're not meant to know the internal representation of. You can store it, and compare it with other pthread_t values, but that's about it.

With this in mind, your best hope is
 
cout << " with thread " << reinterpret_cast<unsigned long long>(pthread_self()) << endl;


It's not a value you can do a lot with, except that it's "for information only".
The actual values likely won't make any sense, but if you create 5 threads, then you'll see that from having 5 different thread id's printed out.

Bear in mind if you're using these to track thread creation and destruction in your log files, that having destroyed a thread, you may (or may not) get that same thread-id back in a subsequent thread create.
how can I use C++ threads? Do i need to go to NuGet Packages..>Browse>search and find pthreads >Install? OR use Vcpkg to install and use pthread in Windows?

std::thread is part of the C++11 standrd so there is no need to install anything.
Another option to use might be std::async which is higher level than std::thread.

Why don't you start from the beginning and tell us what you actually want to do.
I am trying to create a client server stream echo based application using c++ and sockets for windows based. I had installed pthread using NuGet. do you want me to remove it?

After I remove pthread package, it shows the below errors.
How can I get rid of this?

Severity Code Description Project File Line Suppression State
Error (active) E1696 cannot open source file "pthread.h"
Error (active) E0020 identifier "pthread_t" is undefined
Error (active) E0020 identifier "pthread_create" is undefined
Error (active) E0020 identifier "pthread_self" is undefined
Error (active) E0020 identifier "pthread_detach" is undefined
Error (active) E0020 identifier "pthread_self" is undefined
Last edited on
While i tried to add the below line, it shows errors.
E0171 invalid type conversion
C2440 'reinterpret_cast':cannot convert from 'pthread_t to 'unsigned_int64'

What does this mean?


 
cout << " with thread " << reinterpret_cast<unsigned long long>(pthread_self()) << endl;
A number of articles on stackoverflow suggest that pthread_t is an opaque type in some implementations. That would prevent you from casting it to an integral type.

I think it is best to start a new project and forget about pthread and Nuget.
Have a look at this tutorial to get you started.
https://www.geeksforgeeks.org/multithreading-in-cpp/
Last edited on
I need winsock stream based client & server application using C++. A server which can serve many clients at a time.
Last edited on
Pages: 123