C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,483 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,779 people online right now. Registration is fast and FREE... Join Now!




WinSock Linker Errors in "Main.obj"

 

WinSock Linker Errors in "Main.obj"

chris772

2 Jul, 2009 - 05:14 PM
Post #1

D.I.C Head
**

Joined: 13 Mar, 2009
Posts: 166



Thanked: 2 times
My Contributions
I borrowed some code from a snippet I found here at D.I.C. to try wrapping my head around Winsock again, but I keep getting linker errors. After looking over the errors I'm convinced it can't find any of the functions in "winsock.h".

most of the build log:
CODE

------ Build started: Project: winsockTest1, Configuration: Debug Win32 ------
Compiling...
main.cpp
Linking...
main.obj : error LNK2001: unresolved external symbol _WSACleanup@0
main.obj : error LNK2001: unresolved external symbol "int __cdecl DoWinsock(char const *,int)" (?DoWinsock@@YAHPBDH@Z)
main.obj : error LNK2001: unresolved external symbol _WSAStartup@8
C:\Documents an...\winsockTest1\Debug\winsockTest1.exe : fatal error LNK1120: 3 unresolved externals


the program (main.cpp)
CODE

#include <winsock.h>
#include <stdlib.h>
#include <iostream>

using namespace std;

extern int DoWinsock(const char* pcHost, int nPort);

const int kDefaultServerPort = 4242; //default port to connect to on server

int main(int argc, char* argv[])
{
    //test for enough arguments
    if (argc < 2)
    {
        cerr<<"usage: "<<argv[0]<<" <server-address> [server-port]"<<endl<<endl;
        cerr<<"\tIf you don't pass server-port, it defaults to "<<kDefaultServerPort<<"."<<endl;
        return 1;
    }

    //get host and (optionally) port from command line
    const char* pcHost = argv[1];
    int nPort = kDefaultServerPort;

    if (argc >= 3)
    {
        nPort = atoi(argv[2]);
    }

    //sanity check
    int nNumArgsIgnored = (argc - 3);
    
    if (nNumArgsIgnored > 0)
    {
        cerr<<nNumArgsIgnored<<" extra argument"<<(nNumArgsIgnored == 1 ? "" : "s")<<" ignored."<<endl;
    }

    //start Winsock
    WSAData wsaData;
    int nCode;
    
    if ((nCode = WSAStartup(MAKEWORD(1, 1), &wsaData)) != 0)
    {
        cerr<<"WSAStartup() returned error code "<<nCode<<"."<<endl;
        return 255;
    }

    //call main example routine
    int retVal = DoWinsock(pcHost, nPort);

    //shut Winsock down
    WSACleanup();
    return retVal;
}


User is offlineProfile CardPM
+Quote Post


JackOfAllTrades

RE: WinSock Linker Errors In "Main.obj"

3 Jul, 2009 - 04:05 AM
Post #2

I exist to Google your problems.
Group Icon

Joined: 23 Aug, 2008
Posts: 4,948



Thanked: 424 times
Dream Kudos: 50
Expert In: Being annoyed with lazy people.

My Contributions
You need to link in the winsock lib (I think it's ws2_32.lib) for the WSA* functions, and you need to add whatever source file contains DoWinsock into your project.
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 04:05AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month