[英]Why this program is compiling with gcc but not with g++?
程序用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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.