gcc的任何标记,用于编译包含C第三方头文件的Cpp文件

时间:2011-04-01 05:32:44

标签: c++ gcc struct g++ header-files

第3部分头文件(header.h)定义了下面的结构,它可以在将其视为C语言时通过编译。但我们试图将此文件包含在CPP文件中,并且由于g ++编译了更多限制或其他原因,编译失败了?

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.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文件main.cpp

#include "header.h"
#include <iostream>

using namespace std;
int main()
{
    conn_t list;
    list.data = 1;
    list.next = NULL;
    cout<<"hello:"<<list.data<<endl;
    return 0;
}

问题:是否有编译gcc / g ++的标签?

4 个答案:

答案 0 :(得分:1)

typedef struct
{
   int data;
   struct conn_t *next;
}conn_t;

我不知道在某些编译器中你在这里做什么是有效的,但对我来说似乎不对。如果使用typedef,则不应在typename前加struct作为前缀。此外,在声明结构时,将定义名称conn_t,但声明中 无效。这样代码就可以工作:

struct conn_t
{
   int data;
   struct conn_t *next;
};

没有typedef,我只是使用结构名称。

答案 1 :(得分:1)

它应该这样写,以便有一个typedef并引用结构本身:

typedef struct tag_conn_t
{
   int data;
   struct tag_conn_t *next;
}conn_t;
祝你好运,

尤利安

答案 2 :(得分:0)

  1. 首先,头文件是第三方C头文件,它可以用C语言编译;
  2. 其次,头文件无法更新,只是为了特定的目的,我必须在我的C ++文件中包含这个头文件,然后在编译C ++期间,头文件也会被视为C ++,那是什么问题我遇到了。
  3. 我仍然需要你天才给我答案,真的很感谢你们的回答。

答案 3 :(得分:0)

我认为gcc / g ++语言的-x language标记c-header就是你要找的。

相关问题