winsock error : 'stderr' was not declared in this scope

Hi everyone,
I started following beej's guide to network proggraming (here : http://beej.us/guide/bgnet/output/html/multipage/index.html).
In the intro section, he gives a piece of code that you have to put inorder to be able to use the socket librarie. I put that piece of code in a pretty much plain programm but I get this error : 'stderr' was not declared in this scope
I get it at line 15.
Here is my code, I'm using codeblock IDE :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <winsock.h>

using namespace std;

int main()
{

        //WSADATA wsaData;   // if this doesn't work
    WSAData wsaData; // then try this instead

    // MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:

    if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
        fprintf(stderr, "WSAStartup failed.\n");
        exit(1);
    }

    WSACleanup();
    return 0;
}


If you have any idea of what would be causing that error, it would really help me.

mindoo
#include <cstdio>
Hi,
Thanks for your answer.
It gets rid of my error but it still gives me 2 errors :

line 15 : undefined reference to 'WSACleanup@8'
line 20 : undefined reference to 'WSACleanup@0'

please tell me what you think,

mindoo
As Homberto's link says, you should be using winsock 2 rather than winsock 1. That means using the right header file and linking to the right lib. Check out the link for details.
Incidentally, as you're including <iostream>, rather than including <cstdio> to fix the "'stderr' was not declared in this scope" error you should be using the C++ IO Streams equivalent: cerr.

std::cerr << "WSAStartup failed.\n"; // write to error stream

I would also just use return to exit main, rather than exit(), which can be problematic in C++ code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <winsock2.h> // now the newer header

// get compiler to tell the linker what lib you need to link with
// (or add lib to linker options)
#pragma comment(lib, "ws2_32.lib")

using namespace std;

int main()
{
    WSADATA wsaData = {0}; // it is all CAPS!

    // MAKEWORD(1,1) for Winsock 1.1, MAKEWORD(2,0) for Winsock 2.0:

    if (WSAStartup(MAKEWORD(1,1), &wsaData) != 0) {
        cerr << "WSAStartup failed.\n"; // use error stream
        return 1; // not exit()
    }

    cout << "WSAStartup succeeded.\n"; 
    WSACleanup();
    return 0;
}


Andy

return statement vs exit() in main()
http://stackoverflow.com/questions/461449/return-statement-vs-exit-in-main
Last edited on
Hi,
Thanks for your answers. I tried your code andy but it still gave me the same errors but at line 16 and 22. Then I tried linking to "ws2_32.lib" but now it gives me this error : Cannot find -ws2_32.lib.
Do i need to put a copy of the lib in my project file ? And if so please tell me where I can find that file.

mindoo
Sorry, checking back I see you're using Code::Blocks, so the #pragma was a bad suggestion.

You need to add ws2_32 to the "Link libraries" list on the "Linker settings" page of your projects "Build options".

(MinGW provides libws2_32.a not ws2_32.lib, like the Microsoft SDK. But GCC knows you mean libws2_32.a when you say just ws2_32.)

Andy
Last edited on
Hi,
Thanks a lot, it works.
Now I will be able to continue following the tutorial.

mindoo

P.S. This might seem to be a dum question, but is there a way to mark the post as solved ?
Oh, found it ;-)
Topic archived. No new replies allowed.