新手,求教大神这个错误怎么改,问题出在哪
/***项目名称:查询本机或指定主机的IP地址信息
*编译方法:gcc getbyname,c -lws2_32 -0 getbyname,exe
*zaiwenjian winsock2.h头文件中:
*struck hostent {
* char *h_name;
* char **h_aliases;
* short h_addrtype;
* short h_length;
* char **h_addr_list;
* #define h_addr h_addr_list[0]
*};
*
*/
#include <iostream>
#include <stdio.h>
#include <winsock2.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[])
{
WSADATA wsaData;
int retvalue;
DWORD errcode;
int i = 0;
struct hostent *remotehost;
char *host_name;
struct in_addr addr;
char **palias;
char local_host[256];
if(argc != 2)
{
printf("用法:getbyname 主机名\n getbyname localhost\
查询本地主机的IP地址");
return 1;
}
retvalue = WSAStartup(MAKEWORD(2,2), &wsaData);
if (retvalue != 0)
{
printf("WSAStartup失败:%d\n",retvalue);
return 1;
}
if (strcmp(argv[1],"localhost") == 0)
{
retvalue = gethostname(local_host,sizeof(local_host));
if (retvalue != 0)
{
printf("获取本地主机名失败: %d\n", retvalue);
return 1;
}
host_name = local_host;
}
else
{
host_name = argv[1];
}
printf("主机名: %s\n",host_name);
remotehost = gethostbyname(host_name);
if (remotehost == NULL)
{
errcode = WSAGetLastError();
if (errcode == WSAHOST_NOT_FOUND)
{
printf("主机没有找到!");
return 1;
}
else if(errcode == WSANO_DATA)
{
printf("没找到相应的数据记录!");
return 1;
}
else
{
printf("函数调用失败:%ld\n", errcode);
return 1;
}
}
else
{
printf("查询结果: \n");
printf("\t 主机名: %s\n",remotehost->h_name);
for (palias = remotehost->h_aliases; *palias != 0; palias++)
{
printf("\t 别名 #%d: %s\n", ++i, *palias);
}
printf("\t 地址类型:");
switch (remotehost->h_addrtype)
{
case AF_INET:
printf("AF_INET\n");
break;
case AF_NETBIOS:
printf("AF_NETBIOS\n");
break;
default:
printf("%d\n", remotehost->h_addrtype);
break;
}
printf("\t 地址长度: %d\n", remotehost->h_length);
i = 0;
if (remotehost->h_addrtype == AF_INET)
{
while (remotehost->h_addr_list[i] != 0)
{
addr.s_addr = *(u_long *)remotehost->h_addr_list[i++];
printf("\tIP 地址#%d: %s\n",i, inet_ntoa(addr));
}
}
else if (remotehost->h_addrtype == AF_NETBIOS)
{
printf("返回的是NETBIOS地址");
WSACleanup();
return 0;
}
}
下面是错误信息 调试失败
C:\Users\Administrator\Desktop\main.o main.cpp:(.text+0x176): undefined reference to `__imp_gethostbyname'
C:\Users\Administrator\Desktop\main.o main.cpp:(.text+0x190): undefined reference to `__imp_WSAGetLastError'
C:\Users\Administrator\Desktop\main.o main.cpp:(.text+0x345): undefined reference to `__imp_inet_ntoa'
C:\Users\Administrator\Desktop\main.o main.cpp:(.text+0x3aa): undefined reference to `__imp_WSACleanup'
D:\C++\collect2.exe [Error] ld returned 1 exit status
D:\C++\Makefile.win recipe for target 'firstp.exe' failed
[此贴子已经被作者于2022-10-1 15:49编辑过]