Solved

Compile

Posted on 2001-08-20
15
674 Views
Last Modified: 2013-12-14
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?
0
Comment
Question by:ronandersen
15 Comments
 
LVL 6

Expert Comment

by:kotan
Comment Utility
Which compiler are you using?
0
 

Expert Comment

by:Neelima
Comment Utility
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..
0
 

Expert Comment

by:mhermier
Comment Utility
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.
0
 
LVL 2

Expert Comment

by:sateesh_babu
Comment Utility
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
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
cc filename.cc -lnsl -lsocket
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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.
0
 

Author Comment

by:ronandersen
Comment Utility
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!
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 22

Expert Comment

by:cookre
Comment Utility
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.
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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
0
 

Author Comment

by:ronandersen
Comment Utility
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);
}
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
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.
0
 

Author Comment

by:ronandersen
Comment Utility
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)
0
 
LVL 22

Accepted Solution

by:
cookre earned 100 total points
Comment Utility
Did you forget to add WS2_32.LIB to the project:

* Project
* Add To Project
* Files...
* navigate to and select ...\vc98\lib\ws2_32.lib


0
 

Author Comment

by:ronandersen
Comment Utility
cookre,

It compiled and linked successfully. You know not only C but also MS C++ very well. Thanks for all the support!
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Thanks much, and good luck with the newsreader.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now