|
-
May 18th, 2005, 05:32 PM
#1
Thread Starter
Addicted Member
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?
-
May 18th, 2005, 07:03 PM
#2
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.
-
May 18th, 2005, 07:37 PM
#3
Thread Starter
Addicted Member
Re: Winsock2 issues
Ahhh, thank you VERY much.
-
May 19th, 2005, 01:56 PM
#4
Thread Starter
Addicted Member
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"
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.
-
May 21st, 2005, 03:39 AM
#5
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.
-
May 21st, 2005, 08:14 AM
#6
Thread Starter
Addicted Member
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....
-
May 21st, 2005, 08:25 AM
#7
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.
-
May 21st, 2005, 08:42 AM
#8
Thread Starter
Addicted Member
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 -.-
-
May 21st, 2005, 03:02 PM
#9
Re: Winsock2 issues
 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.
-
May 21st, 2005, 03:20 PM
#10
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.
-
May 21st, 2005, 03:24 PM
#11
Re: Winsock2 issues
 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.
-
May 21st, 2005, 05:58 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|