当前位置: 技术问答>linux和unix
编译时发生“符号参照错误”提示!
来源: 互联网 发布时间:2015-11-17
本文导语: 在solaris9下编译socket程序时发生“符号参照错误”提示! 我使用命令"cc test.c -o test" 提示为 bind,accept,listen,gethostbyname,socket“ld:致命的:没有输出写入到test” 但是相关的头文件我也都包含了,以下是源码: #include...
在solaris9下编译socket程序时发生“符号参照错误”提示!
我使用命令"cc test.c -o test"
提示为
bind,accept,listen,gethostbyname,socket“ld:致命的:没有输出写入到test”
但是相关的头文件我也都包含了,以下是源码:
#include
#include
#include
#include
#include
#include
#include
#include
#define PORTNUM 13000
#define HOSTNAMELEN 256
#define oops(msg) {perror(msg);exit(1);}
int main(int ac,char **av)
{
struct sockaddr_in saddr;
struct hostent *hp;
char hostname[HOSTNAMELEN];
int sock_id,sock_fd;
FILE *sock_fp;
char *ctime();
time_t thetime;
sock_id = socket(PF_INET,SOCK_STREAM,0);
if( sock_id == -1 )
oops("socket create error");
bzero( (void *)&saddr,sizeof(saddr));
gethostname(hostname,HOSTNAMELEN);
hp=gethostbyname(hostname);
bcopy((void *) hp->h_addr,(void *)&saddr.sin_addr,hp->h_length);
saddr.sin_family=AF_INET;
saddr.sin_port=htons(PORTNUM);
if(bind(sock_id,(struct sockaddr*)&saddr,sizeof(saddr))!= 0 )
oops("socket bind error");
if( listen( sock_id,1) != 0 )
oops("socket listen error");
while(1)
{
sock_fd=accept(sock_id,NULL,NULL);
printf("Server catch a connect!n");
if( sock_fd == -1 )
oops("socket accept error");
sock_fp=fdopen(sock_fd,"w");
if( sock_fp == NULL )
oops("fdopen error");
thetime=time(NULL);
fprintf(sock_fp,"the time here is ..");
fprintf(sock_fp,"%s",ctime(&thetime));
fclose(sock_fp);
}
}
我使用命令"cc test.c -o test"
提示为
bind,accept,listen,gethostbyname,socket“ld:致命的:没有输出写入到test”
但是相关的头文件我也都包含了,以下是源码:
#include
#include
#include
#include
#include
#include
#include
#include
#define PORTNUM 13000
#define HOSTNAMELEN 256
#define oops(msg) {perror(msg);exit(1);}
int main(int ac,char **av)
{
struct sockaddr_in saddr;
struct hostent *hp;
char hostname[HOSTNAMELEN];
int sock_id,sock_fd;
FILE *sock_fp;
char *ctime();
time_t thetime;
sock_id = socket(PF_INET,SOCK_STREAM,0);
if( sock_id == -1 )
oops("socket create error");
bzero( (void *)&saddr,sizeof(saddr));
gethostname(hostname,HOSTNAMELEN);
hp=gethostbyname(hostname);
bcopy((void *) hp->h_addr,(void *)&saddr.sin_addr,hp->h_length);
saddr.sin_family=AF_INET;
saddr.sin_port=htons(PORTNUM);
if(bind(sock_id,(struct sockaddr*)&saddr,sizeof(saddr))!= 0 )
oops("socket bind error");
if( listen( sock_id,1) != 0 )
oops("socket listen error");
while(1)
{
sock_fd=accept(sock_id,NULL,NULL);
printf("Server catch a connect!n");
if( sock_fd == -1 )
oops("socket accept error");
sock_fp=fdopen(sock_fd,"w");
if( sock_fp == NULL )
oops("fdopen error");
thetime=time(NULL);
fprintf(sock_fp,"the time here is ..");
fprintf(sock_fp,"%s",ctime(&thetime));
fclose(sock_fp);
}
}
|
solaris上这些函数在libsocket里。在连接时加上-lsocket。