gpt4 book ai didi

c++ - 为什么这个程序是用 gcc 编译的,而不是用 g++ 编译的?

转载 作者:可可西里 更新时间:2023-11-01 15:03:14 25 4
gpt4 key购买 nike

以下程序使用 gcc 编译但不使用 g++,我只生成目标文件。

这是 prog.c:

#include "prog.h"

static struct clnt_ops tcp_nb_ops = {4};

这是 prog.h:

#ifndef _PROG_
#define _PROG_

#include <rpc/rpc.h>

#endif

当我这样做时:

gcc -c prog.c

生成目标代码但是,

g++ -c prog.c

给出错误:

variable ‘clnt_ops tcp_nb_ops’ has initializer but incomplete type

如何解决这个问题?

最佳答案

clnt.h中看这个结构体的定义:

typedef struct CLIENT CLIENT;
struct CLIENT {
AUTH *cl_auth; /* authenticator */
struct clnt_ops {
enum clnt_stat (*cl_call) (CLIENT *, u_long, xdrproc_t, caddr_t, xdrproc_t, caddr_t, struct timeval);
/* ...*/
} *cl_ops;
/* ...*/
};

如您所见,struct clnt_ops 定义在内部struct CLIENT。因此,这种类型在 C++ 中的正确名称是 CLIENT::clnt_ops。然而,在 C 中,没有嵌套结构这样的东西,因此它只是简单地显示为 struct clnt_ops

如果你想便携,你可以添加一些类似的东西:

#ifdef __cplusplus
typedef CLIENT::clnt_ops clnt_ops;
#else
typedef struct clnt_ops clnt_ops;
#endif

clnt_ops tcp_nb_ops = ...;

但我认为这种类型根本不打算由客户端代码直接使用。而是仅使用整个 struct CLIENT

关于c++ - 为什么这个程序是用 gcc 编译的,而不是用 g++ 编译的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21039768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com