注册  找回密码

JBTALKS.CC


查看: 4577|回复: 2
打印 上一主题 下一主题

如何在 Solaris 编译 C 文件

[复制链接]

23

主题

0

好友

122

积分

高级会员

Rank: 3Rank: 3Rank: 3

跳转到指定楼层
1#
发表于 2011-3-10 09:05 AM |只看该作者 |倒序浏览
各位,我想请问下该如何在Solaris的环境下 编译 C 文件。

[img][/img]
  1. /* TCPdaytime.c - TCPdaytime, main */

  2. #include <sys/errno.h>

  3. #include <unistd.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <stdio.h>

  7. int        TCPdaytime(const char *host, const char *service);
  8. int        errexit(const char *format, ...);
  9. int        connectTCP(const char *host, const char *service);

  10. #define        LINELEN                128

  11. /*------------------------------------------------------------------------
  12. * main - TCP client for DAYTIME service
  13. *------------------------------------------------------------------------
  14. */
  15. int
  16. main(int argc, char *argv[])
  17. {
  18.         char        *host = "localhost";        /* host to use if none supplied        */
  19.         char        *service = "daytime";        /* default service port                */

  20.         switch (argc) {
  21.         case 1:
  22.                 host = "localhost";
  23.                 break;
  24.         case 3:
  25.                 service = argv[2];
  26.                 /* FALL THROUGH */
  27.         case 2:
  28.                 host = argv[1];
  29.                 break;
  30.         default:
  31.                 fprintf(stderr, "usage: TCPdaytime [host [port]]\n");
  32.                 exit(1);
  33.         }
  34.         TCPdaytime(host, service);
  35.         exit(0);
  36. }

  37. /*------------------------------------------------------------------------
  38. * TCPdaytime - invoke Daytime on specified host and print results
  39. *------------------------------------------------------------------------
  40. */
  41. TCPdaytime(const char *host, const char *service)
  42. {
  43.         char        buf[LINELEN+1];                /* buffer for one line of text        */
  44.         int        s, n;                        /* socket, read count                */

  45.         s = connectTCP(host, service);

  46.         while( (n = read(s, buf, LINELEN)) > 0) {
  47.                 buf[n] = '\0';                /* ensure null-terminated        */
  48.                 (void) fputs( buf, stdout );
  49.         }
  50. }
复制代码
  1. /* connectTCP.c - connectTCP */

  2. int        connectsock(const char *host, const char *service,
  3.                 const char *transport);

  4. /*------------------------------------------------------------------------
  5. * connectTCP - connect to a specified TCP service on a specified host
  6. *------------------------------------------------------------------------
  7. */
  8. int
  9. connectTCP(const char *host, const char *service )
  10. /*
  11. * Arguments:
  12. *      host    - name of host to which connection is desired
  13. *      service - service associated with the desired port
  14. */
  15. {
  16.         return connectsock( host, service, "tcp");
  17. }
复制代码
  1. /* connectsock.c - connectsock */



  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <sys/errno.h>

  5. #include <netinet/in.h>
  6. #include <arpa/inet.h>

  7. #include <netdb.h>
  8. #include <string.h>
  9. #include <stdlib.h>

  10. #ifndef        INADDR_NONE
  11. #define        INADDR_NONE        0xffffffff
  12. #endif        /* INADDR_NONE */

  13. int        errexit(const char *format, ...);

  14. /*------------------------------------------------------------------------
  15. * connectsock - allocate & connect a socket using TCP or UDP
  16. *------------------------------------------------------------------------
  17. */
  18. int
  19. connectsock(const char *host, const char *service, const char *transport )
  20. /*
  21. * Arguments:
  22. *      host      - name of host to which connection is desired
  23. *      service   - service associated with the desired port
  24. *      transport - name of transport protocol to use ("tcp" or "udp")
  25. */
  26. {
  27.         struct hostent        *phe;        /* pointer to host information entry        */
  28.         struct servent        *pse;        /* pointer to service information entry        */
  29.         struct protoent *ppe;        /* pointer to protocol information entry*/
  30.         struct sockaddr_in sin;        /* an Internet endpoint address                */
  31.         int        s, type;        /* socket descriptor and socket type        */


  32.         memset(&sin, 0, sizeof(sin));
  33.         sin.sin_family = AF_INET;

  34.     /* Map service name to port number */
  35.         if ( pse = getservbyname(service, transport) )
  36.                 sin.sin_port = pse->s_port;
  37.         else if ((sin.sin_port=htons((unsigned short)atoi(service))) == 0)
  38.                 errexit("can't get \"%s\" service entry\n", service);

  39.     /* Map host name to IP address, allowing for dotted decimal */
  40.         if ( phe = gethostbyname(host) )
  41.                 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
  42.         else if ( (sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE )
  43.                 errexit("can't get \"%s\" host entry\n", host);

  44.     /* Map transport protocol name to protocol number */
  45.         if ( (ppe = getprotobyname(transport)) == 0)
  46.                 errexit("can't get \"%s\" protocol entry\n", transport);

  47.     /* Use protocol to choose a socket type */
  48.         if (strcmp(transport, "udp") == 0)
  49.                 type = SOCK_DGRAM;
  50.         else
  51.                 type = SOCK_STREAM;

  52.     /* Allocate a socket */
  53.         s = socket(PF_INET, type, ppe->p_proto);
  54.         if (s < 0)
  55.                 errexit("can't create socket: %s\n", strerror(errno));

  56.     /* Connect the socket */
  57.         if (connect(s, (struct sockaddr *)&sin, sizeof(sin)) < 0)
  58.                 errexit("can't connect to %s.%s: %s\n", host, service,
  59.                         strerror(errno));
  60.         return s;
  61. }
复制代码
Things should be changed for Solaris :
(NO change is needed for FreeBSD and Linux systems)
1.add the following to the Makefile:
     。 -lsocket -lnsl to CFLAGS (for socket and related system calls)
     。CC = gcc (compiler, some files required by cc are missing, we have to use gcc instead)
2.errno.h is in the directory /usr/include, NOT in the directory /usr/include/sys
      。you should change all the occurrences
3.make the following changes to the file TCPtecho.c
     。include another header file:  <sys/filio.h> (constant FIONBIO is defined here)
     。define the macro MIN, which returns the min. of two integers

这是我找到要在solaris环境下编译C 文件时要更换的东西。但是我不懂怎样做更换。尤其是第一项的第一个。第二项是不是说#include <sys/errno.h> 换成 #include<errno.h>.?而第三项 增加 #include<sys/filio.h>,那第二项呢?

先谢谢大家






收藏收藏0

7

主题

1

好友

5108

积分

一流名嘴

Rank: 12Rank: 12Rank: 12

2#
发表于 2011-3-10 12:36 PM |只看该作者
本帖最后由 Super-Tomato 于 2011-3-10 12:44 PM 编辑

在 linux 下可以使用 cc, gcc 或 g++ 來编译 c, 如 gcc -o 輸出檔名 原C檔名
使用和参數介绍可自己輸入 gcc --help 取得

library 庫就和 windows 环境一样,自己去检查所使用的庫是否存在於 linux 系统的 /usr/include 路径中








回复

使用道具 举报

2

主题

1

好友

349

积分

超级会员

Rank: 5Rank: 5Rank: 5Rank: 5Rank: 5

3#
发表于 2012-5-2 04:52 AM |只看该作者
我想上面的错误文件已经说明得很清楚了!
第一项是下面的那两个修改进makefile里,而不是修改你的源文件
第二项大概是这个意思,主要是取决于你的环境变量
第三项的第二项就是要你去定义MIN这个宏,但是从你的代码中没有看到你有用到这个宏,具体是否你用到的函数里需要用到这个定义,这个具体要你去查了






回复

使用道具 举报

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

JBTALKS.CC |联系我们 |隐私政策 |Share

GMT+8, 2024-5-15 06:25 PM , Processed in 0.101013 second(s), 26 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

Thanks to ImageShack for Free Image Hosting
Ultra High-speed web hosting powered by iCore Technology Sdn. Bhd.
本论坛言论纯属发表者个人意见,与本论坛立场无关
Copyright © 2003-2012 JBTALKS.CC All Rights Reserved
合作联盟网站:
JBTALKS 马来西亚中文论坛 | JBTALKS我的空间 | JBTALKS部落 | ICORE TECHNOLOGY SDN. BHD. | GXUnlimited-ECSHOP | TM UniFi | TM Streamyx | TM Fixed Line
www.jbtalks.cc | mobile.jbtalks.cc | www.jbtalks.com | www.jbtalks.my | www.icore.com.my | www.icorehosting.com | www.icorehosting.net | www.cttsite.com | www.icore.my | www.lpohchin.com
bbs.jbtalks.cc | bbs.jbtalks.com | bbs.jbtalks.my | bbs.jbtalks.com
回顶部