Results 1 to 12 of 12

Thread: Winsock2 issues

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Winsock2 issues

    I hate errors...
    Code:
    $ gcc -o sck sck.o
    sck.o:sck.c:(.text+0x45): undefined reference to `WSAStartup@8'
    collect2: ld returned 1 exit status
    Code:
    #include <stdio.h>
    #include <windows.h>
    #include <winsock2.h>
    
    int main()
    {
    	WSADATA wsaData;
    	
    	if(WSAStartup(MAKEWORD(2, 0), &wsaData) != 0)
    	{
    		printf("Socket failed to init\n");
    		return 0;
    	}
    	printf("Socket init.\n");
    	
    	return 0;
    }
    I checked out winsock2.h and WSAStartup() is declared, so I have no idea what's going on. Anyone have any ideas?

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Winsock2 issues

    You need to link your program against the winsock 2 library (-lws2_32) as well as including the header.

    You can do this by making sure your library path includes the folder containing the library (-Lpath/to/lib).
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: Winsock2 issues

    Ahhh, thank you VERY much.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: Winsock2 issues

    Code:
    $ gcc -o main main.c -llibws2_32.a -L/lib
    main.c: In function `main':
    main.c:29: error: `sockaddr_in' undeclared (first use in this function)
    main.c:29: error: syntax error before "k"
    Code:
    	sockaddr_in k;
    hrm...

    [edit]
    Okay, this is really pissing me off. No matter what I do I can't fix this. Adding in the struct definition gives me a redecl error, so I know that it's there, but I don't know why I can't use it!
    Last edited by R.a.B.B.i.T; May 19th, 2005 at 03:50 PM.

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Winsock2 issues

    Show us a few lines around this.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: Winsock2 issues

    Code:
    	SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    	if(s == INVALID_SOCKET)
    	{
    		printf("error creating socket: %ld\n", WSAGetLastError());
    		WSACleanup();
    		return 2;
    	}
    	
    	sockaddr_in k;
    	/*k.sin_family = AF_INET;
    	k.sin_addr.s_addr = inet_addr("63.240.202.139");
    	k.sin_port = htons(6112);
    I commented everything after to make sure it was just that line...and nothing's wrong with the lines before....

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Winsock2 issues

    In C, all variables must be declared at the start of the function.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2003
    Posts
    255

    Re: Winsock2 issues

    SOCKET s is down there, but I don't get errors from it...grr. I'll try moving that all around.

    .....
    Code:
    #include <stdio.h>
    #include <winsock2.h>
    
    int main()
    {
    	WSADATA w;
    	SOCKET s;	
    	sockaddr_in k;
    //......etc
    Same error -.-

  9. #9
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Winsock2 issues

    Quote Originally Posted by CornedBee
    In C, all variables must be declared at the start of the function.
    Actually, I think C99 allows you to mix declarations of variables in with your code. I couldn't find this anywhere in the massive C99 draft, but according to wikepedia it's true.

    Code:
    // sockaddr_in k;
    // try this instead:
    struct sockaddr_in k;
    // or this:
    SOCKADDR_IN k; // SOCKADDR_IN is typedef'ed as struct sockaddr_in
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Winsock2 issues

    But no MS compiler supports C99. (GCC does if you set -std=c99. It also supports arbitrary placement of variables if you set -std=gnu89 (the default).)

    But I think you're on to something with the struct keyword.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: Winsock2 issues

    Quote Originally Posted by CornedBee
    But no MS compiler supports C99.
    I wonder why that is. They've made good strides recently with the compliance of their C++ compiler; I guess they're just not as concerned about the C compiler.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Winsock2 issues

    MS doesn't care about C. The core of Windows is in C, but that already can be compiled. Everything else is done in C++ and other higher-level languages.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width