2

I created programs transferring information between client and server by C++ TCP socket programming and compile and run it on Linux. But how to compile the source code to windows executable for Windows user clients?

I've tried using

x86_64-w64-mingw32-g++ ...

to compile. But the it fails for not find these files.

#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h> 

I know Microsoft provides windows programming APIs, but it will require a lot of adjustment of codes. How to correctly set a cross-compiler to compile it properly for Windows, without having to modify codes?

In theory, C++/C should be a high level language and is not platform-specific.

The solution here doesn't work for me (codes containing TCP sockets). How to compile C code in Linux to run on Windows?

3
  • 3
    "C++/C should be a high level language and is not platform-specific." - the language isn't platform-specific. But socket APIs are not part of the language itself. Commented Apr 5, 2023 at 10:45
  • WSL2, cygwin, a VM, a lot of #ifdef WIN32/#else/#endif blocks to conditionally use winsock instead of POSIX headers and apis... Commented Apr 5, 2023 at 12:55
  • The correct solution is to add a bunch of #ifdef _WIN32s to your code (not WIN32, btw), or to use a wrapper library that does it for you. There's also Cygwin, that might be able to compile your code as is, but A - I don't know if it's possible to cross-compile with it, and B - I'd argue that it shouldn't be a development target of new cross-platform software, but only a way of porting old non-portable software from Linux. Commented Apr 5, 2023 at 17:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.