1

I want to compile my program from linux to run on Windows as exe. I know how to do it with mingw64 etc. but real problem is Windows doesn't have libraries of code but linux has. How can i compile it?

#define <sys/socket.h>
#define <arpa/inet.h>
#define <netinet/in.h>
int main(){

//some codes etc.

}
Uur Kn
  • 62
  • 7
  • 3
    Possible duplicate of [Where does one get the "sys/socket.h" header/source file?](https://stackoverflow.com/questions/4638604/where-does-one-get-the-sys-socket-h-header-source-file) – newkid Jan 20 '19 at 11:22
  • Why can't you compile your program on a Windows machine? If you are required to give a program runnable on Windows, why can't you give source code to be compiled and run on [WSL](https://en.wikipedia.org/wiki/Windows_Subsystem_for_Linux) ? That probably should be simpler than your cross-compilation – Basile Starynkevitch Jan 20 '19 at 12:48
  • Without motivation and context, your question is too broad. So please **[edit](https://stackoverflow.com/posts/54275860/edit) your question to improve it** a lot. What kind of program are you writing? What are all the functions outside of the C standard [n1570](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) that you are using? Why do you need it to be runnable on Windows? Why don't you give the source code to your Windows users? – Basile Starynkevitch Jan 20 '19 at 12:51

2 Answers2

2

You may use Windows Sockets 2 (Winsock) which is used for socket programming in Windows:

#include <winsock2.h>
#include <ws2tcpip.h>
#pragma comment(lib, "Ws2_32.lib")

int main()
{
}
Swordfish
  • 12,971
  • 3
  • 21
  • 43
mukuls
  • 21
  • 1
  • Be careful though that there are many subtle differences, starting from `WSAInit` and going to many similar-but-different `setsocktopt`. It may be tempting to iron them out with localized `#ifdef`s in your application code, but you'll soon see that they get all over the place. My tip: abstract them away in a nicer API. – Matteo Italia Jan 20 '19 at 12:27
  • 1
    or, to not reinvent the wheel use an existing cross platform library. (eg. `boost::asio`) – Swordfish Jan 20 '19 at 12:47
  • 1
    @Swordfish I don't think Boost works in a C program... ;-) – Andrew Henle Jan 20 '19 at 14:01
  • 1
    @AndrewHenle Uuuups. – Swordfish Jan 20 '19 at 14:08
0
#ifdef __WIN32__
    #include <winsock2.h>
#else
    #include <sys/socket.h>
#endif

Why not try preprocessor macros?

xerdink
  • 121
  • 1
  • 5