PDA

View Full Version : Error trying to compile winsock app with mingw



jenkem
May 12th, 2007, 07:49 PM
I am trying to compile a simple windows application on Ubuntu fiesty using mingw-gcc. I am getting the following errors:


$ i586-mingw32msvc-gcc -lws2_32 coco.c
coco.c: In function `main':
coco.c:61: warning: return type of 'main' is not `int'
/tmp/ccxABKPU.o:coco.c:(.text+0x2f): undefined reference to `_send@16'
/tmp/ccxABKPU.o:coco.c:(.text+0x78): undefined reference to `_WSAStartup@8'
/tmp/ccxABKPU.o:coco.c:(.text+0x89): undefined reference to `_socket@12'
/tmp/ccxABKPU.o:coco.c:(.text+0xb0): undefined reference to `_inet_addr@4'
/tmp/ccxABKPU.o:coco.c:(.text+0xd4): undefined reference to `_htons@4'
/tmp/ccxABKPU.o:coco.c:(.text+0xec): undefined reference to `_connect@12'
/tmp/ccxABKPU.o:coco.c:(.text+0x12f): undefined reference to `_recv@16'
/tmp/ccxABKPU.o:coco.c:(.text+0x140): undefined reference to `_send@16'
/tmp/ccxABKPU.o:coco.c:(.text+0x172): undefined reference to `_recv@16'
/tmp/ccxABKPU.o:coco.c:(.text+0x1a9): undefined reference to `_send@16'
/tmp/ccxABKPU.o:coco.c:(.text+0x1db): undefined reference to `_recv@16'
/tmp/ccxABKPU.o:coco.c:(.text+0x22b): undefined reference to `_send@16'
/tmp/ccxABKPU.o:coco.c:(.text+0x25a): undefined reference to `_recv@16'
collect2: ld returned 1 exit status

I've tried to link with the wine DLL, with the exact same errors:
$ i586-mingw32msvc-dlltool --input-def libws2_32.def --dllname WS2_32.DLL --output-lib libws2_32
$ i586-mingw32msvc-gcc libws2_32.a coco.c
coco.c: In function `main':
coco.c:61: warning: return type of 'main' is not `int'
/tmp/ccPOcY4D.o:coco.c:(.text+0x2f): undefined reference to `_send@16'
/tmp/ccPOcY4D.o:coco.c:(.text+0x78): undefined reference to `_WSAStartup@8'
/tmp/ccPOcY4D.o:coco.c:(.text+0x89): undefined reference to `_socket@12'
/tmp/ccPOcY4D.o:coco.c:(.text+0xb0): undefined reference to `_inet_addr@4'
/tmp/ccPOcY4D.o:coco.c:(.text+0xd4): undefined reference to `_htons@4'
/tmp/ccPOcY4D.o:coco.c:(.text+0xec): undefined reference to `_connect@12'
/tmp/ccPOcY4D.o:coco.c:(.text+0x12f): undefined reference to `_recv@16'
/tmp/ccPOcY4D.o:coco.c:(.text+0x140): undefined reference to `_send@16'
/tmp/ccPOcY4D.o:coco.c:(.text+0x172): undefined reference to `_recv@16'
/tmp/ccPOcY4D.o:coco.c:(.text+0x1a9): undefined reference to `_send@16'
/tmp/ccPOcY4D.o:coco.c:(.text+0x1db): undefined reference to `_recv@16'
/tmp/ccPOcY4D.o:coco.c:(.text+0x22b): undefined reference to `_send@16'
/tmp/ccPOcY4D.o:coco.c:(.text+0x25a): undefined reference to `_recv@16'
collect2: ld returned 1 exit status


Any idea how I can rectify this problem? Thanks.

Wim Sturkenboom
May 13th, 2007, 06:31 AM
I don't know the finer details of 'cross-compiling', but the error indicates that you're missing a library.

omax
January 21st, 2008, 04:08 AM
I know this is a really old thread, but I had the same problem and couldn't find the answer anywhere...

It's easy, -lws2_32 must come after source. ie:

$ i586-mingw32msvc-gcc -o prog.exe prog.c -lws2_32

efittery
August 4th, 2008, 09:56 PM
Really, thanks for this tip. I have been trying to compile socket code with -mno-cygwin flag for 2 weeks. This suggestion of using the -lws2_32 flag at link time worked great. I am not sure the code is working, but at least it compiles and links. Keep on trucking.

swmail
July 29th, 2009, 07:29 PM
Thanks omax wor the cool tip!
For me works too.

Xerxesx
January 24th, 2010, 02:09 PM
With source given below, which I called winsock.cpp. I was able to get a working executable with the following build command (run under MINGW32 bourne):


g++ winsock.cpp -lws2_32 -owinsock.exe
I was able to execute with:

./winsock.exe
PC name: XerxexX
Press any key to continue . . .
For those of you like me using using slick-edit:

Build->GNU C Options...
under "Link" tab in "Libraries/Objects" add -lws2_32
//------- winsock.cpp -----------------------------
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <fstream>
#include <sstream>
#include <string>
#include <time.h>
#include <winsock.h>
#define MAX_SIZE 200
using namespace std;
int main (int argc, char *argv[])
{
struct WSAData wsa_data;
int ret_code;

char host_name[MAX_SIZE]="unknown\0";
char buf[MAX_SIZE];

WSAStartup(MAKEWORD(1, 1), &wsa_data);
ret_code = gethostname(buf, MAX_SIZE);

if (ret_code != SOCKET_ERROR)
{
int i=-1;
do
{
i++;
host_name[i]=buf[i];
} while (buf[i] > '\0');
}
cout<<"PC name: " << host_name << endl;
system("pause");

WSACleanup();
return(0);
}

dwhitney67
January 24th, 2010, 02:47 PM
Why not just strcpy() for this bit of code?:


int i=-1;
do
{
i++;
host_name[i]=buf[i];
} while (buf[i] > '\0');

Or since you seem to be developing in C++, and you have included the STL string header file, use a string object to hold the results:


std::string host_name = buf;