[C] winsok e MinGw Clicca QUI per vedere il messaggio nel forum |
cato |
sto usando dev-c++ che si basa su MinGW per qualche programma con le socket.
ma quando compilo il linker fallisce...
sapete se c'e' qualche parametro da settare?grazie, |
DeepBlue |
con che messaggio fallisce?
stai compilando programmi che utilizzano socket sotto win? Se sì, ricordati che gli header di rete probabilmente cambiano: devi usare winsock.h e winsock2.h |
cato |
[Linker error] undefined reference to `socket@12' ad esempio |
cato |
prendendo un esempio dal comer
ftp://ftp.cs.purdue.edu/pub/comer/
http://www.cs.purdue.edu/homes/dec/netbooks.html
code:
/* TCPecho.cpp - main, TCPecho */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <winsock.h>
void TCPecho(const char *, const char *);
void errexit(const char *, ...);
SOCKET connectTCP(const char *, const char *);
#define LINELEN 128
#define WSVERS MAKEWORD(2, 0)
/*------------------------------------------------------------------------
* main - TCP client for ECHO service
*------------------------------------------------------------------------
*/
int
main(int argc, char *argv[])
{
char *host = "localhost"; /* host to use if none supplied */
char *service = "echo"; /* default service name */
WSADATA wsadata;
switch (argc) {
case 1:
host = "localhost";
break;
case 3:
service = argv[2];
/* FALL THROUGH */
case 2:
host = argv[1];
break;
default:
fprintf(stderr, "usage: TCPecho [host [port]]\n");
exit(1);
}
if (WSAStartup(WSVERS, &wsadata) != 0)
errexit("WSAStartup failed\n");
TCPecho(host, service);
WSACleanup();
exit(0);
}
/*------------------------------------------------------------------------
* TCPecho - send input to ECHO service on specified host and print reply
*------------------------------------------------------------------------
*/
void
TCPecho(const char *host, const char *service)
{
char buf[LINELEN+1]; /* buffer for one line of text */
SOCKET s; /* socket descriptor */
int cc, outchars, inchars; /* characters counts */
s = connectTCP(host, service);
while (fgets(buf, sizeof(buf), stdin)) {
buf[LINELEN] = '\0'; /* ensure line null-termination */
outchars = strlen(buf);
(void) send(s, buf, outchars, 0);
/* read it back */
for (inchars = 0; inchars < outchars; inchars += cc) {
cc = recv(s, &buf[inchars], outchars-inchars, 0);
if (cc == SOCKET_ERROR)
errexit("socket recv failed: %d\n",
GetLastError());
}
fputs(buf, stdout);
}
closesocket(s);
}
non compila...mi da errori con il linker...
che parametri vanno dati al compilatore? |
yeah |
Hai incluso la libreria delle socket in Dev-cpp? Io uso solo mingw, senza IDE, quindi non so come si setta :/
se usi winsock.h devi linkare con wsock32
se invece usi winsock2.h devi linkare con ws2_32 |
fabpicca |
io mi ricordo che per un problema di questo tipo il progetto di reti in C l'ho fatto sotto linux... |
cato |
Originally posted by yeah
H
se usi winsock.h devi linkare con wsock32
:approved: |
cato |
mhh uso queste opzioni:
-lwsock32 -ladvapi32
e mi da questi errori:
[Linker error] undefined reference to `recv'
[Linker error] undefined reference to `send'
[Linker error] undefined reference to `bzero'
[Linker error] undefined reference to `bcopy'
M:\Vxx\vte\_ferrario\0307_onwork\wsocket\Makefile.win [Build Error] [Wsoket.exe] Error 1
:( |
DeepBlue |
per bzero e bcopy hai incluso memory.h? |
yeah |
Dal tuo post precedente sembrava che wsock32 risolvesse il problema.
Gli errori sono arrivati dopo aver aggiunto qualcosa? Puoi postare il codice?
Eventualmente prova a scambiare wsock32 e advapi32:
-ladvapi32 -lwsock32
Off-Topic:
Carino memory.h incluso in Mingw
code:
/*
* This file is part of the Mingw32 package.
*
* memory.h maps to the standard string.h header.
*/
#ifndef __STRICT_ANSI__
#include <string.h>
#endif
Non me lo immaginavo :)
Non sono sicuro, ma bzero() e bcopy() non esistono solo in Unix/Linux?
Forse no :)
|
|
|
|