2017-08-15 10 views
0

g++でコンパイルされたCネスト構造体に関するコンパイルエラーでコミュニティのヘルプが必要です。g入れ子構造体でg ++コンパイルエラー

私は次の3つのファイルがあります。

main.cppに(完全性のため、このファイルはコンパイルエラーを再現する必要はありません): ns.h

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

int main(int argc, char* argv[]) 
{ 
    someFunc(); 
    return 0; 
} 

#ifndef NS_H 
#define NS_H 

#ifdef __cplusplus 
extern "C" { 
#endif 

void someFunc(); 

#ifdef __cplusplus 
} 
#endif 

#endif // NS_H 

ns.c

#include "ns.h" 

#ifdef __cplusplus 
extern "C" { 
#endif 

#define MY_MAX (42) 

typedef struct _outer 
{ 
    int count; 
    struct Inner 
    { 
    int count; 
    void* cb; 
    void* ctx [ MY_MAX ]; 
    } inner_[ MY_MAX ]; 
} Outer; 

Outer g_outer[ 10 ]; 

#include "staticFuncs.h" 

void someFunc() 
{ 
    staticFunc(); 
} 

#ifdef __cplusplus 
} 
#endif 

staticFuncs.h

#include <stdio.h> 

#ifdef __cplusplus 
extern "C" { 
#endif 

static void anotherStaticFunc() 
{ 
    printf("%s", __FUNCTION__); 

    struct Inner* ptr = NULL; 
    ptr = &(g_outer[ 0 ].inner_[ 0 ]); 
    (void)ptr; 
} 

static void staticFunc() 
{ 
    printf("%s", __FUNCTION__); 
    anotherStaticFunc(); 
} 

#ifdef __cplusplus 
} 
#endif 

次のように関連したコンパイルは次のとおりです。

>g++ --version 
g++ (GCC) 4.8.3 20140911 (Red Hat 4.8.3-7) 
Copyright (C) 2013 Free Software Foundation, Inc. 
This is free software; see the source for copying conditions. There is NO 
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 


>g++ -g -c ns.c -o ns.o 
In file included from ns.c:22:0: 
staticFuncs.h: In function 'void anotherStaticFunc()': 
staticFuncs.h:12:7: error: cannot convert '_outer::Inner*' to 'anotherStaticFunc()::Inner*' in assignment 
    ptr = &(g_outer[ 0 ].inner_[ 0 ]); 
    ^

できますか?

Cでは、ネストされた構造体はスコープ構文では参照されませんが、ネストされていないものとして参照されています。私はstaticFuncs.hのようにstruct Inner* ptr = NULL;をやることができると確信しています。私の自信よりも重要なことに、g++ではなくccでコンパイルすると、コンパイルが実行されます。

私はどこでもextern "C"の中で投げることによってC、ないC++、コードをコンパイルしていg++を伝えるために、私のdamnedestを試してみたが、それはまだInnerのスコープにつまずくます。

誰でもこのコンパイラエラーが発生する理由とその回避方法を特定できますか?コンパイルにはg++を使用する必要があります。

staticFuncs.hstaticFuncs.cの場合、問題は変わりません。

静的関数が静的でない場合、問題は変わりません。

staticFuncs.h/cの内容が#includeではなくns.cに埋め込まれている場合、問題は変わりません。

+0

です«なぜあなたはその後も、C++コンパイラを使用しようとしていますか? 'g ++'の代わりに 'gcc'を使うだけです。 –

+0

これは私がコンパイラで選択肢を持っていない仕事で問題を単純化したものです。 – StoneThrow

+3

'extern" C "'は言語に何の影響も与えず、シンボルの生成方法にのみ影響します。 – molbdnilo

答えて

1

特定の接尾辞がある場合、gccはC++としてコードをコンパイルしますが、g ++でCをコンパイルする方法はありません。
gccとg ++の唯一の違いは、後者は常にC++をコンパイルし、C++ライブラリとリンクすることです。

最も簡単な修正では、私はCではなく、C++をコンパイルするよそのG ++伝えるために私damnedestを試してみた»

struct Inner 
{ 
    int count; 
    void* cb; 
    void* ctx [ MY_MAX ]; 
}; 

typedef struct _outer 
{ 
    int count; 
    struct Inner inner_[ MY_MAX ]; 
} Outer; 
+0

これは機能しました。ありがとう。私は 'gcc'と' g ++ 'の間の' C'/'C++'二重機能を混ぜていたと思います。 – StoneThrow

1

コンパイラからのエラーメッセージは非常に明確です。同等のライン

struct Inner* ptr = NULL; 

代入演算子のLHSは次のように宣言されているにあなたがする必要がどのような

// Declare a struct in the function. This is different from _outer::Inner. 
struct Inner; 

// Declare the variable using the struct declared in the function. 
struct Inner* ptr = NULL; 

ptrの種類として_outer::Inner*を使用しています。

_outer::Inner* ptr = NULL; 
+0

私はこの記事で説明したように、 'g ++ 'を実行して' C'コードをコンパイルしています。 '_outer :: Inner'スタイルは' C++ 'の構文です。このコードは、 'C'と' C++ 'コンパイラによってコンパイル可能である必要があります。 – StoneThrow

関連する問題