GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 5.00 average.
  #1  
Old 23-Feb-2006, 19:36
vermin1302 vermin1302 is offline
Awaiting Email Confirmation
 
Join Date: Feb 2006
Location: Indiana, USA
Posts: 19
vermin1302 is on a distinguished road
Question

[Linker error] undefined reference to `WinMain@16'


I'm using Dev-C++ 4.9.9.2 to compile a program I wrote about five years ago.

I recall that it compiled without errors when I first wrote it.

I had to change a few things, because I guess the code was getting a little old. Anyway, I got rid of all of the errors except this WinMain@16 thing. I've noticed that this can be a problem if you're writing a console program and accidentally have your project set to Windows Program, but I've checked this and it's definitely set as a console program. So now I'm not exactly sure what to do. My code is attached to the bottom of this post...please help if you can!

chat.cpp
CPP / C++ / C Code:
/*
	CPP Final Projekt
	JMVermillion

	chat.cpp

	If they can do it, so can I. 24 hours!

   All of the WinSock routines were blatantly butchered from
   Open-Source code. Thanks to the authors. :)
*/

#include <iostream>
using namespace std;

#include "chat.h"

CComm::CComm()
{
	ListenSocket = INVALID_SOCKET;	// Start with INVALID socket. Ease of use.

	/*
		Check to see if it's a Windows machine. If not, don't worry.
	*/
	#ifdef _WIN32 // Why does Windows *need* WinSock? Who knows...
		WORD VersionRequested = MAKEWORD(1,1);
		WSADATA wsaData;
		WSAStartup(VersionRequested, &wsaData);	// starts the Winsock service
		if ( wsaData.wVersion != VersionRequested )
		{
			cout << "Error Code \"MICROSOFT\": Wrong version or WinSock not loaded!\n";
		}
	#endif
}

CComm::~CComm()
{
	if ( ListenSocket != INVALID_SOCKET )
		closesocket( ListenSocket );	// Was there a socket? Close!

	#ifdef _WIN32	  // Windows is the only crappy OS that uses WinSock!
		WSACleanup(); // WinSock cleanup routine.
	#endif
}

bool CComm::SendMsg( char *Msg, int Len, char *host, short port )
{
	signed int Sent;
	hostent *hostdata;
	if ( atoi(host) )	// Is it an IP, or not?
	{
		u_long ip = inet_addr( host );
		hostdata = gethostbyaddr( (char *)&ip, sizeof(ip), PF_INET );
	}
	else	// Don't we love DNS!? This also does WINS resolution!
	{
		hostdata = gethostbyname( host );
	}

	if ( !hostdata )
	{
		cout << "Error Code 1227: We couldn't find your host. Sorry. :)\n";
		return false;
	}

	sockaddr_in dest;	// What's our destination?
	dest.sin_family = PF_INET;
	dest.sin_addr = *(in_addr *)(hostdata->h_addr_list[0]);
	dest.sin_port = htons( port );
	cout << "Message sent to: " << inet_ntoa(dest.sin_addr) << ":"
		  << ntohs(dest.sin_port) << "\n";
	Sent = sendto(ListenSocket, Msg, Len, 0, (sockaddr *)&dest, sizeof(sockaddr_in));

	if ( Sent != Len )
	{
		cout << "Error Code 666: We couldn't send that there UDP packet on ListenPort. :-P\n";
		return false;
	}

	return true;
}

void *CComm::ListenThread( void *data )
{
	char buf[4096];
	CComm *Comm = (CComm *)data;
	int len = sizeof(Comm->client);

	while(1)	// Recursion? I think so!
	{
		int result = recvfrom( Comm->ListenSocket, buf, sizeof(buf)-1, 0, (sockaddr *)&Comm->client, (socklen_t *)&len);
		if ( result > 0 )
		{
			buf[result] = 0;
			cout << "Message rcvd from: " << inet_ntoa(Comm->client.sin_addr)
				  << ":" << ntohs(Comm->client.sin_port) << "\n";
			cout << "]--[\n " << buf << "]--[\n";
		}
	}	// end infinite loop
}

bool CComm::Listen( int PortNum )
{
	ListenSocket = socket(PF_INET, SOCK_DGRAM, 0);
	if ( ListenSocket == INVALID_SOCKET )
	{
		cout << "Error Code 123: We failed to initialize a listen socket.\n";
		return false;
	}

	srv.sin_family = PF_INET;
	srv.sin_addr.s_addr = htonl( INADDR_ANY );	// any address
	srv.sin_port = htons( PortNum );

	if ( bind( ListenSocket, (struct sockaddr *)&srv, sizeof(srv)) != 0 )
	{
		cout << "Error Code 2001: Sorry Dave, we couldn't bind the listen socket.\n";
		closesocket( ListenSocket );
		return false;
	}

	int ThreadID;	// the listening thread's handle

	#ifdef _WIN32
   /*
   	Always extra routines for Windows. Why!?
   	Well, the 'net said I should do this to make it work
      properly, and the 'net is never wrong.
   */
		DWORD thread;
		ThreadID = (int)CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(CComm::ListenThread), (void *)this, 0, &thread);
		ThreadID = ThreadID ? 0 : 1;	// reverse the value for Windows

	/*
		Are we NOT running Windows? Who cares!?
	*/
	#else
		pthread_t thread;
		ThreadID = pthread_create(&thread, 0, CComm::ListenThread, (void *)this);
	#endif

	if(ThreadID)	// Did we fail to create a listen thread?
	{
		cout << "Error Code 911: Did we fail? Of course we did! No listen thread for you!\n";
		return false;
	}
	else
		return true;
}

and chat.h
CPP / C++ / C Code:
/*
	CPP Final Projekt
	JMVermillion

	chat.h

	Well? Who's laughing now!?

    Again, WinSock crap butchered from Open-Source code. Thanks! :)

    Also, OS-Specific routines either taken from John Gray(UNIX) or random
    internet tutorials(WINDOWS). Others noted in comments.
*/

#ifndef _CHAT_H_
#define _CHAT_H_

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#ifndef _WIN32	// Are we UNIX? Huzzah! Use these includes.
	#include <sys/types.h>
	#include <sys/socket.h>
	#include <netinet/in.h>
	#include <unistd.h>
	#include <netdb.h>
	#include <arpa/inet.h>
	#define SOCKET			int
	#define INVALID_SOCKET	-1
	#define closesocket		close
	#include <pthread.h>
#else	// Are we using Windows? WHY!?!?!?
	#include <winsock.h>
	#define socklen_t	int
	/*
		Library stuff that the internet said I needed.
		Always trust the 'net.[tm]
	*/
	#if defined(_MSC_VER)
		#pragma comment(lib, "wsock32.lib")
	#elif defined(__BORLANDC__)
		#pragma(lib, "mswsock.lib")
	#endif
#endif

class CComm
{
private:
	static void *ListenThread(void *data);
	SOCKET ListenSocket;	// Listen Socket?
	sockaddr_in srv;		// Address for listen socket?
	sockaddr_in client;	    // Where did we get our message?

public:
	/*
		CComm default constructor
	*/
	CComm();

	/*
		CComm destructor
	*/
	~CComm();

	/*
		Send messages!
	*/
	bool SendMsg(char *Msg, int Len, char *host, short port);

    /*
   	    Receive messages!
	*/
	bool Listen(int PortNum);

};

#endif

Thanks a lot for looking.
  #2  
Old 23-Feb-2006, 20:03
Chris.Dev's Avatar
Chris.Dev Chris.Dev is offline
Junior Member
 
Join Date: Jan 2005
Posts: 48
Chris.Dev will become famous soon enough

Re: [Linker error] undefined reference to `WinMain@16'


You don't have a main function declared. You may be missing one of the original source files. If you simply add:

CPP / C++ / C Code:
int main(int argc, char *argv[]) {
    return 0;
}

your code will compile just fine.

Every executeable needs a entry point. 'main' is the default entry point for console apps.
  #3  
Old 23-Feb-2006, 23:03
vermin1302 vermin1302 is offline
Awaiting Email Confirmation
 
Join Date: Feb 2006
Location: Indiana, USA
Posts: 19
vermin1302 is on a distinguished road

Re: [Linker error] undefined reference to `WinMain@16'


Well, thanks.

It compiles just fine, like you said.

You might be right about me missing something, because...it doesn't actually WORK. Haha.

Maybe I had a main.cpp lying around somewhere. I dunno~

Thanks though!
  #4  
Old 24-Feb-2006, 03:58
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,435
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: [Linker error] undefined reference to `WinMain@16'


Chris' fix allowed the code to compile, but the program doesn't do anything with this fix. You undoubtedly have (had?) a main laying around somewhere otherwise it would not have compiled even 5 years ago.
__________________

Definition: Politics
Latin, from
poly meaning many and
tics meaning blood sucking parasites
-- Tom Smothers
  #5  
Old 24-Feb-2006, 10:39
vermin1302 vermin1302 is offline
Awaiting Email Confirmation
 
Join Date: Feb 2006
Location: Indiana, USA
Posts: 19
vermin1302 is on a distinguished road

Re: [Linker error] undefined reference to `WinMain@16'


Probably "had," as I can't seem to find it.

Oh well. Thanks a lot, guys
  #6  
Old 20-Dec-2008, 03:59
nehhar's Avatar
nehhar nehhar is offline
New Member
 
Join Date: Dec 2008
Posts: 1
nehhar is on a distinguished road

Re: [Linker error] undefined reference to `WinMain@16'


Many Thanks
  #7  
Old 04-Jul-2010, 09:34
Gero Gero is offline
New Member
 
Join Date: Jul 2010
Posts: 1
Gero is on a distinguished road

Re: [Linker error] undefined reference to `WinMain@16'


CNUB.org standard manifest.cpp:

CPP / C++ / C Code:
#pragma comment(linker, "/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")

#define WINAPI __stdcall
#define HINSTANCE int
#define LPSTR char*

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPredInstance, LPSTR lpCmdLine, int nShowCmd)
{
    // Zilch
} 

--Gero
Last edited by admin : 05-Jul-2010 at 01:25. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
G++ question... TreyAU21 C++ Forum 10 24-Feb-2006 09:45
How to interpret characters as they are being entered? nkhambal C Programming Language 18 14-Feb-2006 11:41
c++ inheritance illbemissingu C++ Forum 14 23-Oct-2005 17:35
Linker Errors with Dev-C++ 5 Gamer_2k4 C++ Forum 1 18-Apr-2005 11:08
fltk-2.0 cvs Plumb FLTK Forum 20 13-Nov-2004 08:10

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 22:52.


vBulletin, Copyright © 2000 - 2014, Jelsoft Enterprises Ltd.