Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > winsock/mingw compile problem (undefined reference)

Reply
Thread Tools

winsock/mingw compile problem (undefined reference)

 
 
Dom
Guest
Posts: n/a
 
      08-18-2005
I'm new to c++. Just started learning it 24 hours ago. Am running into a
compile problem. Please, no one waste the effort telling me to google it.
I've been researching it for quite a while with no joy. I got dev-c++ and a
bit of winsock sample code. I've done nothing out of the ordinary. I could
only assume that anyone else that downloaded this software and attempted
this would meet with the same result. The problem lies with either the
compiler or the source. I'm not sure which. Help greatly appreciated.
Compiler output and source to follow.

Compiler: Default compiler
Executing g++.exe...
g++.exe "C:\Documents and Settings\d\My Documents\c\test.cpp" -o
"C:\Documents and Settings\d\My
uments\c\test.exe" -I"C:\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Dev-Cpp\include\c++\3.4.2\backward"
-I"C:\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Dev-Cpp\include\c++\3.4.2"
-I"C:\Dev-Cpp\include" -L"C:\Dev-Cpp\lib"
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp.text+0x46): undefined
reference to `WSAStartup@8'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp.text+0x80): undefined
reference to `socket@12'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp.text+0x97): undefined
reference to `WSAGetLastError@0'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp.text+0xac): undefined
reference to `WSACleanup@0'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp.text+0xd0): undefined
reference to `inet_addr@4'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp.text+0xe5): undefined
reference to `htons@4'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp.text+0x10f): undefined
reference to `connect@12'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp.text+0x12: undefined
reference to `WSACleanup@0'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp.text+0x1e5): undefined
reference to `send@16'
C:\DOCUME~1\D\LOCALS~1\Temp/ccWSaaaa.o:test.cpp.text+0x239): undefined
reference to `recv@16'
collect2: ld returned 1 exit status

Execution terminated





#include <stdio.h>
#include "winsock2.h"

int main() {

// Initialize Winsock.
WSADATA wsaData;
int iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
if ( iResult != NO_ERROR )
printf("Error at WSAStartup()\n");

// Create a socket.
SOCKET m_socket;
m_socket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );

if ( m_socket == INVALID_SOCKET ) {
printf( "Error at socket(): %ld\n", WSAGetLastError() );
WSACleanup();
return 0;
}

// Connect to a server.
sockaddr_in clientService;

clientService.sin_family = AF_INET;
clientService.sin_addr.s_addr = inet_addr( "127.0.0.1" );
clientService.sin_port = htons( 27015 );

if ( connect( m_socket, (SOCKADDR*) &clientService,
sizeof(clientService) ) == SOCKET_ERROR) {
printf( "Failed to connect.\n" );
WSACleanup();
return 0;
}

// Send and receive data.
int bytesSent;
int bytesRecv = SOCKET_ERROR;
char sendbuf[32] = "Client: Sending data.";
char recvbuf[32] = "";

bytesSent = send( m_socket, sendbuf, strlen(sendbuf), 0 );
printf( "Bytes Sent: %ld\n", bytesSent );

while( bytesRecv == SOCKET_ERROR ) {
bytesRecv = recv( m_socket, recvbuf, 32, 0 );
if ( bytesRecv == 0 || bytesRecv == WSAECONNRESET ) {
printf( "Connection Closed.\n");
break;
}
if (bytesRecv < 0)
return 0;
printf( "Bytes Recv: %ld\n", bytesRecv );
}

return 0;
}


 
Reply With Quote
 
 
 
 
Azdo
Guest
Posts: n/a
 
      08-18-2005
Dom wrote:

> I'm new to c++. Just started learning it 24 hours ago. Am running into a
> compile problem. Please, no one waste the effort telling me to google it.
> I've been researching it for quite a while with no joy.


You'd better wasted a few more time googling it:

http://www.google.es/search?hl=gl&q=...Procurar&meta=

Choose the *first* occurence.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
compile directive for conditional compile for Java 1.4 versus Java 5 timjowers Java 7 02-02-2011 12:08 AM
How to compile the following source code in VC6// I have error inVC++6 but compile ok in GCC fAnSKyer C++ 2 06-07-2009 07:57 AM
computation at compile time i.e. compile time functions usingtemplates Carter C++ 2 03-04-2009 06:43 PM
Compile versus not compile (VS 2005)?? stupid48@gmail.com ASP .Net 1 04-11-2008 08:24 PM
cant compile on linux system.cant compile on cant compile onlinux system. Nagaraj C++ 1 03-01-2007 11:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57