Following Program compiles with gcc but not with g++, I am generating only object file.
This is prog.c:
#include "prog.h"
static struct clnt_ops tcp_nb_ops = {4};
This is prog.h:
#ifndef _PROG_
#define _PROG_
#include <rpc/rpc.h>
#endif
When I do:
gcc -c prog.c
That generates object code but,
g++ -c prog.c
gives error:
variable ‘clnt_ops tcp_nb_ops’ has initializer but incomplete type
How to solve this issue?
DIFFERENCE BETWEEN g++ & gccg++ is used to compile C++ program. gcc is used to compile C program.
Both are the compilers in Linux to compile and run C and C++ programs. Initially gcc was the GNU C Compiler but now a day's GCC (GNU Compiler Collections) provides many compilers, two are: gcc and g++. gcc is used to compile C program while g++ is used to compile C++ program.
C source files compiled with g++ will be compiled as C++ code.
Supported languagesAs of May 2021, the recent 11.1 release of GCC includes front ends for C ( gcc ), C++ ( g++ ), Objective-C, Fortran ( gfortran ), Ada (GNAT), Go ( gccgo ) and D ( gdc , since 9.1) programming languages, with the OpenMP and OpenACC parallel language extensions being supported since GCC 5.1.
Look at the definition of this struct in 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;
/* ...*/
};
As you can see, the struct clnt_ops
is defined inside struct CLIENT
. So the proper name for this type in C++ is CLIENT::clnt_ops
. In C, however there is no such thing as nested structs, so it is visible as simply struct clnt_ops
.
If you want to be portable you may add something along the lines of:
#ifdef __cplusplus
typedef CLIENT::clnt_ops clnt_ops;
#else
typedef struct clnt_ops clnt_ops;
#endif
clnt_ops tcp_nb_ops = ...;
But I think that this type is simply not intended to be used directly by client code. Instead use whole struct CLIENT
only.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With