undefined reference to `WSAStartup@8'

This is a discussion on undefined reference to `WSAStartup@8' within the C Programming forums, part of the General Programming Boards category; As the topic suggests I'm having trouble linking libs under Dev C++ 4(windows 2000). Also when creating a C project ...

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    11

    undefined reference to `WSAStartup@8'

    As the topic suggests I'm having trouble linking libs under Dev C++ 4(windows 2000). Also when creating a C project all my code I write providing it's more than a simple "hello world" kind of application doesn't seem to compile unless I change it's source file to .cpp rather than .c and no I'm not using c++ functions in the program. To get around these problems I've been having I've had to use MSVC++ changing the .c file to .cpp and use #pragma comment(lib, "wsock32.lib") rather than linking libs normally.

    wicked

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    If memory serves correct, the libraries in DevC++ have a different naming convention to other compilers. In most compilers you would use "wsock32.lib", but I think in DevC++ it's called "libwsock32.a" or something similar.

    Try find this lib and link to it.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Thats the thing, I've got no idea how to link to it via Dev C++ 4.

    wicked

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Add "-lwsock32" (without the quotes) to the linker box under Project->Project Options->Parameters.

    If you have not set up a project, you will have to do that first. Go to File->New->Project->Empty Project. Then add your files by using Project->Add to Project.

    If you want to link with other windows libraries you can use the same syntax. For example, "-lgdi32 -ladvapi32" will link to gdi32.lib and advapi32.lib.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Project->Project Options->Parameters.

    I fail to see a parameters option. Yes I've done everything else correctly.

    wicked

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>Dev C++ 4<<

    Possibly because you're using the 'Methuselah' version. Try upgrading to the latest beta version (go for the 'full' version) and anonytmouse's instructions should make a lot more sense.

    Alternatively, just look around for a 'linker' field in your project options; it's likely that it will be there (somewhere!) and that you can add the required lib either by navigaing to the libs directory and selecting it or manually typing in the name of the lib as described by anonytmouse. However, if you are using the older v4 then it's likely that the compiler and other tools (MinGW) will be older, too; in this case linking libs may well be the least of your problems.

    If, by chance, you're not using v4 then look again - I can only assure you that there is a 'parameters' tab as anonytmouse has described.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Thank you all it's now working wonderfully but one of my original problems remain. When creating a project my code wont compile unless it's a C++ project. As I said before it's written in C.

    wicked

  8. #8
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Quote Originally Posted by wicked
    Thank you all it's now working wonderfully but one of my original problems remain. When creating a project my code wont compile unless it's a C++ project. As I said before it's written in C.

    wicked
    Are you sure there isnt any C++ creeping in there? An iostream here or there or an unintentional reference?

    AFAIK, DevC++ doesnt usually have a problem with straigh C as long as it's legal and the compiler is setup normally

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by wicked
    Thank you all it's now working wonderfully but one of my original problems remain. When creating a project my code wont compile unless it's a C++ project. As I said before it's written in C.

    wicked
    when you create a project a "new project" box pops up. At the bottom right you can choose to compile it as a C or a C++ project and or make either language compile as the default. Check these boxes and make sure they are on the desired language.
    I'm pretty posotive that is what is causing your problem.

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Yes when creating a new project I choose a C project. I'm almost positive my code contains no C++. Am I allowed to paste it to verify my claim?

    wicked

  11. #11
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Sure. You could also post what compiler errors you're getting, though
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Code:
    #include <stdio.h>
    #include <winsock.h>
    #include <stdlib.h>
    
    #pragma comment(lib, "wsock32.lib")
    
    int main() {
    
    	WSADATA wsadata;
    	int error;
    
    	error = WSAStartup(MAKEWORD(1,1),&wsadata);
    	if(error == SOCKET_ERROR) {
    		printf("Error starting winsock\n",WSAGetLastError());
    		return -1;
    	}
    
    	int Socket;
    
    	if((Socket = socket(AF_INET, SOCK_STREAM, 0))<0) {
    		printf("Error opening socket\n",WSAGetLastError());
    		return -1;
    	}
    
    	sockaddr_in addr;
    	addr.sin_family = AF_INET;
    	addr.sin_port = htons(1234);
    	addr.sin_addr.s_addr = INADDR_ANY;
    
    	if((bind(Socket,(sockaddr*)&addr, sizeof(addr)))<0) {
    		printf("Bind error\n",WSAGetLastError());
    		return -1;
    	}
    
    	if((listen(Socket,1))<0) {
    		printf("Error listening\n",WSAGetLastError());
    		return -1;
    	}
    	else
    		printf("Listening\n");
      
        getch();
        
        closesocket(Socket);
    	WSACleanup();
    	return 0;
    }
    And the errors:

    Compiler: Default compiler
    Building Makefile: "C:\Documents and Settings\Administrator\Desktop\Code\server\Makefil e.win"
    Executing make...
    make.exe -f "C:\Documents and Settings\Administrator\Desktop\Code\server\Makefil e.win" all
    gcc.exe -c server1.c -o server1.o -I"C:/Dev-Cpp/include"

    server1.c: In function `main':

    server1.c:25: error: `sockaddr_in' undeclared (first use in this function)

    server1.c:25: error: (Each undeclared identifier is reported only once

    server1.c:25: error: for each function it appears in.)

    server1.c:25: error: parse error before "addr"

    server1.c:26: error: `addr' undeclared (first use in this function)

    server1.c:30: error: `sockaddr' undeclared (first use in this function)

    server1.c:30: error: parse error before ')' token

    make.exe: *** [server1.o] Error 1

    Execution terminated
    Last edited by wicked; 01-07-2005 at 05:18 AM.

  13. #13
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,806
    change those <> to [] on your code tags and we will all love you
    Woop?

  14. #14
    Registered User
    Join Date
    Jan 2005
    Posts
    11
    Sorry, my mistake.

    wicked

  15. #15
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    In traditional C, all variable declarations must follow a block opening ( a '}' ) and be made before any statements. Your code has variable declarations such as sockaddr_in addr; and int Socket; below statements. This will work in C++ and even C99, but not traditional C.

    Code:
    int main() {
        int v1;
        char v2;  /* Variable declarations fine here. */
    
        printf("Test");
    
        short v3; /* Invalid: Variable declarations after statements are not allowed. */
    
        if (v2 == 0)
        {
             int v4; /* Variable declaration allowed because it is at start of new block. */
    
             v2 = 1;
             v4 = 100;
        }
    
        v4 = 0; /* Will fail. A variable is not in scope outside the block in which it is declared. */
    
        {
              void* v5; /* Valid. We can start a new block without an if, etc. */
    
              v5 = NULL;
        }
    }
    Last edited by anonytmouse; 01-07-2005 at 08:03 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. C OpenGL Compiler Error?
    By Matt3000 in forum C Programming
    Replies: 12
    Last Post: 07-07-2006, 04:42 PM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21