Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this program is compiling with gcc but not with g++?

Tags:

c++

c

gcc

g++

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?

like image 548
Sumit Avatar asked Jan 10 '14 08:01

Sumit


People also ask

Is GCC and G ++ the same?

DIFFERENCE BETWEEN g++ & gccg++ is used to compile C++ program. gcc is used to compile C program.

Should I compile with GCC or G ++?

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.

Can you compile C files with g ++?

C source files compiled with g++ will be compiled as C++ code.

Does GCC have G ++?

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.


1 Answers

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.

like image 72
rodrigo Avatar answered Nov 15 '22 21:11

rodrigo