我这个server 哪里错了???

LLSullivan 2012-10-29 08:52:47

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <strings.h>
#include <unistd.h>
#include <arpa/inet.h>

int connectSock(const char *host,const char *service , const char *transport)
{

struct sockaddr_in sa;
struct hostent *hp;
struct servent *sp;
struct protoent *ppe;
u_short portbase;
int s;
int type;

memset(&sa,0,sizeof(sa));
sa.sin_family = AF_INET;

if((hp = gethostbyname(host))==NULL){
fprintf(stderr,"%s: no such host?\n", host);
exit(1);
}

bcopy((char*)hp->h_addr, (char*)&sa.sin_addr, hp->h_length);


if((sp = getservbyname(service,transport))==NULL){
fprintf(stderr,"%s: No such service on this host\n");
exit(1);
}

portbase =9000;
sa.sin_port = htons((u_short)atoi(service)+portbase);

if((ppe = getprotobyname(transport))==0){
perror("can't get this protocol entry\n");
exit(1);
}
if (strcmp(transport, "udp")==0)
type = SOCK_DGRAM;
else type = SOCK_STREAM;

if((s=socket(PF_INET, type ,ppe->p_proto))<0){
perror("socket");
exit(1);
}

if(connect(s,(struct sockaddr*)&sa,sizeof sa)<0){
perror("connect");
exit(1);
}

return s;
}





为啥我gcc之后出现这个错误提示啊:

Undefined first referenced
symbol in file
main /opt/local/sources/packages/SUNWspro/prod/lib/crt1.o
ld: fatal: Symbol referencing errors. No output written to a.out

我看网上说是因为没有把main。c写入????不懂是什么意思

...全文
129 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
swordtan 2012-10-29
  • 打赏
  • 举报
回复
1.生成静态库
gcc -c -o server.o server.c
ar -cru libserver.a server.o

在需要使用静态库的地方,这样使用
gcc -o main main.c -lserver

2.直接使用法
直接将这个代码拷贝到你的项目中,一起编译
manxiSafe 2012-10-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
比如我想写个connectTCP 调用这段代码,我还想写个connectUDP也调用这段代码,所以我想是不是先编译一下这段代码????
[/Quote]
呃,我知道你的 意思了, 你可以 将 它 制作成 静态库 ,编译成 .o 文件,想用的时候就可以 连接进去 就可以了
manxiSafe 2012-10-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]
比如我想写个connectTCP 调用这段代码,我还想写个connectUDP也调用这段代码,所以我想是不是先编译一下这段代码????
[/Quote]
你的意思是 想在不同的 程序中调用这个函数 是吧??
那你就将这个 函数 写成独立的 C文件,在不同的 程序中 包涵这个 .c 不就可以调用了么。
LLSullivan 2012-10-29
  • 打赏
  • 举报
回复
比如我想写个connectTCP 调用这段代码,我还想写个connectUDP也调用这段代码,所以我想是不是先编译一下这段代码????
manxiSafe 2012-10-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]
是啊,我木有写main函数,能不能只写个方法,compile之后再从别的方法include呢????
[/Quote]
我表示没看懂 你的include 想 说明什么,你不知道一个C程序 都必须要有一个主函数 作为 程序的入口么,
你要是想测试 函数的功能 ,那你就 叫和 main函数 到 mian 函数里边调用 测试不久行了
LLSullivan 2012-10-29
  • 打赏
  • 举报
回复
是啊,我木有写main函数,能不能只写个方法,compile之后再从别的方法include呢????
manxiSafe 2012-10-29
  • 打赏
  • 举报
回复
楼主,貌似你 没 写 main() 函数 ???
是不??

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧