c++ - g++ compile error on C nested struct -
i need community's compile error regarding c nested structs compiled g++
.
i have following 3 files:
main.cpp (for completeness; file not required reproduce compile error):
#include <iostream> #include "ns.h" int main( int argc, char* argv[] ) { somefunc(); return 0; }
ns.h:
#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
the relevant compilation follows:
>g++ --version g++ (gcc) 4.8.3 20140911 (red hat 4.8.3-7) copyright (c) 2013 free software foundation, inc. free software; see source copying conditions. there no warranty; not merchantability or fitness particular purpose. >g++ -g -c ns.c -o ns.o in file included ns.c:22:0: staticfuncs.h: in function 'void anotherstaticfunc()': staticfuncs.h:12:7: error: cannot convert '_outer::inner*' 'anotherstaticfunc()::inner*' in assignment ptr = &(g_outer[ 0 ].inner_[ 0 ]); ^
what gives?
i'm aware in c
, nested structs not referred scoping syntax, referred though not nested. i.e., i'm pretty confident in can struct inner* ptr = null;
did in staticfuncs.h
. more important confidence, if compile cc
instead of g++
, compilation goes through.
i've tried damnedest tell g++
i'm compiling c
, not c++
, code throwing in extern "c"
s everywhere, it's still tripping on scoping of inner
.
can please identify why compiler error arises , how can work around it? must use g++
compile.
the problem unchanged if staticfuncs.h
staticfuncs.c
instead.
the problem unchanged if static functions not static.
the problem unchanged if content of staticfuncs.h/c
embedded in ns.c
instead of being #include
ed.
while gcc compile code c++ if has suffixes, there no way compile c g++.
difference between gcc , g++ latter compiles c++, , links c++ libraries.
the simplest fix is
struct inner { int count; void* cb; void* ctx [ my_max ]; }; typedef struct _outer { int count; struct inner inner_[ my_max ]; } outer;
Comments
Post a Comment