Link to home
Start Free TrialLog in
Avatar of ronandersen
ronandersen

asked on

Compile

My programs compile, but when I try to create the exe, I receive the following errors:

Linking...
t6.obj : error LNK2001: unresolved external symbol _htons@4
t6.obj : error LNK2001: unresolved external symbol _inet_ntoa@4
t6.obj : error LNK2001: unresolved external symbol _gethostbyname@4
t6.obj : error LNK2001: unresolved external symbol _inet_addr@4
t6.obj : error LNK2001: unresolved external symbol _connect@12
t6.obj : error LNK2001: unresolved external symbol _socket@12
t6.obj : error LNK2001: unresolved external symbol _send@16
t6.obj : error LNK2001: unresolved external symbol _recv@16
ws-util.obj : error LNK2001: unresolved external symbol _recv@16
ws-util.obj : error LNK2001: unresolved external symbol _WSAGetLastError@0
ws-util.obj : error LNK2001: unresolved external symbol _closesocket@4
ws-util.obj : error LNK2001: unresolved external symbol _shutdown@8
LIBCD.lib(crt0.obj) : error LNK2001: unresolved external symbol _main
Debug/t6.exe : fatal error LNK1120: 12 unresolved externals
Error executing link.exe.

t6.exe - 14 error(s), 0 warning(s)


WHat am I missing?
Avatar of kotan
kotan
Flag of Malaysia image

Which compiler are you using?
Avatar of Neelima
Neelima

Are you including the proper .h files if you are doing socket programming?If u r doing so at Unix you can see the man pages for the relevent syscall and check what all files need to be included..
It sounds to me that you are missing libraries during the link... Check in which libraries the function are and add them in the library path of your project.
When you are compiling, link to the library files nlsl and lsocket.

So if you are using a gcc compiler or anything, compile it as

gcc filename.cc -lnlsl -lsocket

Also take care to see that you have included the requiered header files
cc filename.cc -lnsl -lsocket
It would seem as if you're using MS (LNK2001 is their message code for undefined).  

You need to include either WS2_32.LIB or WSOCK32.LIB in your project.

Which one you choose depends on which version of winsock you want to support and which OS.  If Winsock 1.1 is OK and you want to run on both 9x and NT, use WSOCK32.  Note that the cooresponding DLL must be on the client at run time.

Also, I didn't see a call to WSAStartup.  You'll need that to specify which winsock versions you need.
Avatar of ronandersen

ASKER

cookre,

I am using MS Visual c++, although I am writing it in C. I'll give this a try and let you know what happens. I am new at writing socket programs, although I have C experience. Is there an example socket program you can direct me to that simply intialize a connection to a server, sends some commands, and closes a connection to a server. I am trying to write a newsreader program. I plan on executing this as a service..

Thanks!
Since you mentioned running it as a service, I presume you're limiting yourself to NT and 2000.  In that case, use the WS2_32.LIB and DLL.


You'll find bunches of hits if you search for:
socket c sample wsastartup

This one is pretty clean:
http://www.netbook.cs.purdue.edu/code/code28_1.htm

And here's some reference material:
http://www-net.cs.umass.edu/cs653-1997/notes/ch3/ch3-2.htm


PS: I don't know how attached you are to C.  If you'd consider VB, there's a WinSock object that makes writing that sort of thing very easy - it lets you concentrate on the program's objective without having to worry too much about fiddling with the socket code.  It's an ideal way to begin socket programming.  Shoot, you may want to experiment with VB first just to get some quickie successes and coarse overview, then switch over to C.
Boy, talk about serendipity!  I was nosing around:
http://www.snible.org/winsock/

and came across this complete Windows source for a news reader(the socket stuff is in subdir gensock):
ftp://ftp.ksc.nasa.gov/pub/winvn/source/current/winvn
cookre,

Thanks for all the excellent suggestions. I tried compiling one of the example programs(it is included in this message):

http://www.netbook.cs.purdue.edu/code/code28_1.htm

It is short and to the point, however, when I tried compiling it for windows, I received the following error(I believe I am missing a header file, but I do not know which one):

--------------------Configuration: t8 - Win32 Debug--------------------
Compiling...
t8.cpp
f:\program files\microsoft visual studio\myprojects\t8\t8.cpp(122) : fatal error C1010: unexpected end of file while looking for precompiled header directive
Error executing cl.exe.

t8.obj - 1 error(s), 0 warning(s)


/* client.c - code for example client program that uses TCP */

#include <windows.h>
#include <winsock.h>

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

#define PROTOPORT       5193            /* default protocol port number */

extern  int             errno;
char    localhost[] =   "localhost";    /* default host name            */
/*------------------------------------------------------------------------
 * Program:   client
 *
 * Purpose:   allocate a socket, connect to a server, and print all output
 *
 * Syntax:    client [ host [port] ]
 *
 *               host  - name of a computer on which server is executing
 *               port  - protocol port number server is using
 *
 * Note:      Both arguments are optional.  If no host name is specified,
 *            the client uses "localhost"; if no protocol port is
 *            specified, the client uses the default given by PROTOPORT.
 *
 *------------------------------------------------------------------------
 */
main(argc, argv)
int     argc;
char    *argv[];
{
        struct  hostent  *ptrh;  /* pointer to a host table entry       */
        struct  protoent *ptrp;  /* pointer to a protocol table entry   */
        struct  sockaddr_in sad; /* structure to hold an IP address     */
        int     sd;              /* socket descriptor                   */
        int     port;            /* protocol port number                */
        char    *host;           /* pointer to host name                */
        int     n;               /* number of characters read           */
        char    buf[1000];       /* buffer for data from the server     */

        WSADATA wsaData;
        WSAStartup(0x0101, &wsaData);

        memset((char *)&sad,0,sizeof(sad)); /* clear sockaddr structure */
        sad.sin_family = AF_INET;         /* set family to Internet     */

        /* Check command-line argument for protocol port and extract    */
        /* port number if one is specified.  Otherwise, use the default */
        /* port value given by constant PROTOPORT                       */

        if (argc > 2) {                 /* if protocol port specified   */
                port = atoi(argv[2]);   /* convert to binary            */
        } else {
                port = PROTOPORT;       /* use default port number      */
        }
        if (port > 0)                   /* test for legal value         */
                sad.sin_port = htons((u_short)port);
        else {                          /* print error message and exit */
                fprintf(stderr,"bad port number %s\n",argv[2]);
                exit(1);
        }

        /* Check host argument and assign host name. */

        if (argc > 1) {
                host = argv[1];         /* if host argument specified   */
        } else {
                host = localhost;
        }

        /* Convert host name to equivalent IP address and copy to sad. */

        ptrh = gethostbyname(host);
        if ( ((char *)ptrh) == NULL ) {
                fprintf(stderr,"invalid host: %s\n", host);
                exit(1);
        }
        memcpy(&sad.sin_addr, ptrh->h_addr, ptrh->h_length);

        /* Map TCP transport protocol name to protocol number. */

        if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) {
                fprintf(stderr, "cannot map \"tcp\" to protocol number");
                exit(1);
        }

        /* Create a socket. */

        sd = socket(PF_INET, SOCK_STREAM, ptrp->p_proto);
        if (sd < 0) {
                fprintf(stderr, "socket creation failed\n");
                exit(1);
        }

        /* Connect the socket to the specified server. */

        if (connect(sd, (struct sockaddr *)&sad, sizeof(sad)) < 0) {
                fprintf(stderr,"connect failed\n");
                exit(1);
        }

        /* Repeatedly read data from socket and write to user's screen. */

        n = recv(sd, buf, sizeof(buf), 0);
        while (n > 0) {
                write(1,buf,n);
                n = recv(sd, buf, sizeof(buf), 0);
        }

        /* Close the socket. */

        closesocket(sd);

        /* Terminate the client program gracefully. */

        exit(0);
}
Welcome to the world of MS.

It's complaining about not finding an include of stdafx.h. One of the many defaults when creating a project is the use of precompiled headers to make the compile go markedly faster.

So, either start off with a 'Hello World' console app then add your stuff in, or, using what you probably have now:

* Project
* Settings
* C/C++ tab
* Category: Precompiled Headers
* select Not using precompiled headers


PS: If you're using WS2_32.LIB, change the include above to winsock2.h.
cookre,

It compiled, but when building the exe, it returned:

Linking...
t8.obj : error LNK2001: unresolved external symbol _closesocket@4
t8.obj : error LNK2001: unresolved external symbol _recv@16
t8.obj : error LNK2001: unresolved external symbol _connect@12
t8.obj : error LNK2001: unresolved external symbol _socket@12
t8.obj : error LNK2001: unresolved external symbol _getprotobyname@4
t8.obj : error LNK2001: unresolved external symbol _gethostbyname@4
t8.obj : error LNK2001: unresolved external symbol _htons@4
t8.obj : error LNK2001: unresolved external symbol _WSAStartup@8
Debug/t8.exe : fatal error LNK1120: 8 unresolved externals
Error executing link.exe.

t8.exe - 9 error(s), 0 warning(s)
ASKER CERTIFIED SOLUTION
Avatar of cookre
cookre
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
cookre,

It compiled and linked successfully. You know not only C but also MS C++ very well. Thanks for all the support!
Thanks much, and good luck with the newsreader.