Re: [GCC] Any tag of gcc to compile Cpp file which includes the 3rd party C header file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Liao-Yuan Zhang <zhang.lyuan@xxxxxxxxx> writes:

> GCC experts, I encounter a problem here need your help:
> There is 3rd part header file (header.h) define a structure a below,
> it can be passed compiling when treat it as C language. But we trying
> to include this file in CPP file, and the compiling is failed since
> g++ compiling more restriction or other reason.
>
> shell@hercules$ g++ main.cpp
> In file included from main.cpp:1:
> header.h:10: error: conflicting declaration âtypedef struct conn_t conn_tâ
> header.h:9: error: âstruct conn_tâ has a previous declaration as âstruct conn_tâ
> main.cpp: In function âint main()â:
> main.cpp:8: error: aggregate âconn_t listâ has incomplete type and
> cannot be defined
>
> Header file header.h:
>
> Â1 Â#ifndef __HEADER_H__
> Â2 Â#define __HEADER_H__
> Â3 Â#ifdef __cplusplus
> Â4 Âextern "C" {
> Â5 Â#endif
> Â6 Âtypedef struct
> Â7 Â{
> Â8 Â Â Âint data;
> Â9 Â Â Âstruct conn_t *next;
> 10 Â}conn_t;
> 11
> 12 Â#ifdef __cplusplus
> 13 Â}
> 14 Â#endif
> 15
> 16 Â#endif // __HEADER_H__
>
> Cpp file main.cpp
>
> Â1 Â#include "header.h"
> Â2 Â#include <iostream>
> Â3
> Â4 Âusing namespace std;
> Â5 Âint main()
> Â6 Â{
> Â7 Â Â Âconn_t list;
> Â8 Â Â Âlist.data = 1;
> Â9 Â Â Âlist.next = NULL;
> 10 Â Â Âcout<<"hello:"<<list.data<<endl;
> 11 Â Â Âreturn 0;
> 12 Â}
>
> Question: Is there any tag of gcc/g++ to compile it?
> First, the header file is 3rd party C header file, it can be compiled
> in C language;
> Second, the header file can not be updated, just for specific purpose,
> I have to includes this header file in my C++ file, and then during
> compile the C++, the header file also will be treat as C++, that's
> what problem I encountered.

The header file is not written in valid C++.  There is no option which
will make it compile as C++.  It's valid C, but in C it doesn't mean
what you may think it means.  The field "next" does not point to to a
value of type "conn_t".  It points to a value of type "struct conn_t",
an entirely different type.  In C, "conn_t" and "struct conn_t" can be
different types.  In C++, they can not.  That is why this is an error in
C++.

If you can't change the header file, then I think your only option is to
#include it in a C file, write a C interface which does whatever you
need, and call that interface from your C++ code.

Ian



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux