LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 818|回复: 3

代码编译出错但不知错在何处?

[复制链接]
发表于 2007-5-5 13:56:57 | 显示全部楼层 |阅读模式
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>

#include <stdarg.h>
#include <stdio.h>


#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif

#define LINELEN 128
extern int errno;

TCPdaytime(const char *host, const char *service)
{
        char buf[LINELEN+1];
        int s,n;
       
        s = connectTCP(host, service);

        while((n = read(s, buf, LINELEN)) > 0)
        {
                buf[n] = '\0';
                (void)fputs(buf, stdout);

        }
}
int errexit(const char *format, ...)
{
        va_list args;
        va_start(args, format);
        vfprintf(stderr, format, args);
        va_end(args);
        exit(1);
}

int connectsock(const char *host, const char *service, const char *transport)
                {
                        struct hostent *phe;
                        struct servent *pse;
                        struct protoent *ppe;
                        struct sockaddr_in sin;
                        int s, type;
               
                        memset(&sin, 0, sizeof(sin));
                        sin.sin_family = AF_INET;
                        if(pse = getservbyname(service,transport))
                                sin.sin_port = pse->s_port;
                        else if((sin.sin_port=htons((unsigned short)atoi(service))) == 0)
                                errexit("can't get \"%s\" service entry\n",service);
                        if(phe = gethostbyname(host))
                                memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
                        else if((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
                                errexit("can't get \"%s\" host entry\n",host);
                        if((ppe = getprotobyname(transport)) == 0)
                                errexit("can't get \"%s\" protocol entry\n",transport);
                        if(strcmp(transport,"udp") == 0)
                                type = SOCK_DGRAM;
                        else
                                type = SOCK_STREAM;
                        s = socket(PF_INET, type, ppe->p_proto);
                        if(s<0)
                                errexit("can't create socket : %s\n",strerror(errno));
                        if(connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                                errexit("can't connect to %s.%s: %s\n", host, service, strerror(errno));
                        return s;
                }
int connectTCP(const char *host, const char *service)
        {
               
                return connectsock(host, service, "tcp");
        }


int main(int argc, char *argv[])
{
        char *host = "localhost";
        char *service = "daytime";

        switch(argc)
        {       
                case 1:
                        host = "localhost";
                        break;
                case 3:
                        service = argv[2];
                case 2:
                        host = argv[1];
                        break;
                default:
                        fprintf(stderr, "usage: TCPdaytime[host[port]]\n");
                        exit(1);
        }
        TCPdaytime(host, service);
        exit(0);
}

以上是程序代码,用于TCP连接并且获取一条信息。

以下是报错信息,大家帮忙分析一下,是什么原因造成的编译不成功,如何解决?

cd '/tmp/daytimetcp/debug' && WANT_AUTOCONF_2_5="1" WANT_AUTOMAKE_1_6="1" gmake -k
gmake all-recursive
Making all in src
正在编译 daytimetcp.c (gcc)
正在连接 daytimetcp (libtool)
正在连接 daytimetcp (gcc)
/usr/bin/ld: errno: TLS definition in /lib/libc.so.6 section .tbss mismatches non-TLS reference in daytimetcp.o
/lib/libc.so.6: could not read symbols: Bad value
collect2: ld returned 1 exit status
gmake[2]: *** [daytimetcp] 错误 1
gmake[2]: 由于错误目标“all”并未重新创建。
gmake[2]: Nothing to be done for `all-am'.
gmake[1]: *** [all-recursive] 错误 1
gmake: *** [all] 错误 2
*** 退出状态:2 ***


本人刚刚接触linux程序设计,望高人指点,谢谢!
发表于 2007-5-5 19:17:34 | 显示全部楼层
把编译链接时具体执行的命令贴出来看看。

另外,这个不是编译错误,而是链接错误。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-5-5 23:39:15 | 显示全部楼层
我是用开发工具写的程序,每次写好都是直接点选调试运行显示报错改正后都通过了,这是第一次遇到这问题。
所以具体的编译链接命令我也不知道,不过用./configure && make 后的报错和这个相同。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2007-5-5 23:41:20 | 显示全部楼层
我估计很有可能是建立工程时必要的选项我没有选上。代码应该不会有问题
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表