在ubuntu上编译rpcgen程序

时间:2012-12-04 04:33:23

标签: rpc ubuntu-12.04

我是rpcgen编程的新手。 在ubuntu上编译以下示例rpcgen示例时,我得到错误。主要功能是计算给定数字的平方

square.X

struct square_in {
 long arg1;
 };

struct square_out {
long    res1;
};

program SQUARE_PROG {
version SQUARE_VERS {
square_out  SQUAREPROC(square_in) = 1;
                    /* procedure number = 1 */
} = 1;              /* version number = 1 */
} = 0x31230000;         /* program number = 0x31230000 */

client.c

  #include  <rpc/rpc.h>
  #include  "square.h"

int
main(int argc, char **argv)
  {
CLIENT      *cl;
square_in   in;
square_out  *outp;

if (argc != 3)
    //err_quit("usage: client <hostname> <integer-value>");
    exit(0);

cl = clnt_create(argv[1], SQUARE_PROG, SQUARE_VERS, "tcp");

in.arg1 = atol(argv[2]);
if ( (outp = squareproc_1(&in, cl)) == NULL)
    //err_quit("%s", clnt_sperror(cl, argv[1]));
   exit(0);

printf("result: %ld\n", outp->res1);
exit(0);
 }

server.c

#include    <rpc/rpc.h>
#include    "square.h"
#include <stdio.h>


     square_out *
    squareproc_1_svc(square_in *inp, struct svc_req *rqstp)
     {
static square_out   out;

printf("thread %d started, arg = %ld\n",
       pr_thread_id(NULL), inp->arg1);
sleep(5);
out.res1 = inp->arg1 * inp->arg1;
printf("thread %d done\n", pr_thread_id(NULL));

return(&out);
       }

生成文件:

PROGS = client server
CFLAGS += -DDEBUG

all:    ${PROGS}

square.h square_clnt.c square_svc.c square_xdr.c:   square.x
            rpcgen -C square.x

square_clnt.o: square_clnt.c square.h

square_svc.o: square_svc.c square.h

client: square.h client.o square_clnt.o square_xdr.o
            ${CC} ${CFLAGS} -o $@ client.o square_clnt.o square_xdr.o \
                ${LIBS} ${LIBS_RPC}

server: square.h server.o square_svc.o square_xdr.o
            ${CC} ${CFLAGS} -o $@ server.o square_svc.o square_xdr.o \
                ${LIBS} ${LIBS_RPC}

clean:
        rm -f ${PROGS} ${CLEANFILES} *_clnt.c *_svc.c *_xdr.c square.h

执行时,我收到以下错误:

cc -DDEBUG   -c -o client.o client.c
cc -DDEBUG -o client client.o square_clnt.o square_xdr.o \

cc -DDEBUG   -c -o server.o server.c
cc -DDEBUG -o server server.o square_svc.o square_xdr.o \

server.o: In function `squareproc_1_svc':
server.c:(.text+0x14): undefined reference to `pr_thread_id'
server.c:(.text+0x52): undefined reference to `pr_thread_id'
collect2: error: ld returned 1 exit status
make: *** [server] Error 1

2 个答案:

答案 0 :(得分:3)

使用像这样的服务器文件

// SERVER FILE: server.c

#include"rpc/rpc.h"
#include"square.h"
#include"stdio.h"
#include"stdlib.h"
#include"math.h"

square_out *squareproc_1_svc(square_in *inp,struct svc_req *rqstp)
{

    static square_out out;
    out.res1 = inp->arg1 * inp->arg1;
    return(&out);
}

使用

运行程序
[root@localhost ~]# rpcgen -C square.x
[root@localhost ~]# cc -c client.c -o client.o
[root@localhost ~]# cc -c square_clnt.c -o square_clnt.o
[root@localhost ~]# cc -c square_xdr.c -o square_xdr.o
[root@localhost ~]# cc -o client client.o square_clnt.o square_xdr.o
[root@localhost ~]# cc -c client.c server.c square_xdr.c
[root@localhost ~]# cc -c server.c -o server.o
[root@localhost ~]# cc -c square_svc.c -o square_svc.o
[root@localhost ~]# cc -o server server.o square_svc.o square_xdr.o
[root@localhost ~]# ./server &

[1] 3334

[root@localhost ~]# ./client localhost 4

result is : 16

答案 1 :(得分:-1)

我认为您不需要使用pr_thread_id。尝试编译没有它。