我需要社区的帮助,编译错误是关于使用g++
编译的C嵌套结构。C++嵌套结构的g ++编译错误
我有以下三个文件:
main.cpp中(的完整性;该文件不需要重现编译错误):
#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
相关编译如下:
>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;
。比我的自信更重要的是,如果我用cc
而不是g++
进行编译,编译就会完成。
我已经尽我damnedest告诉g++
说我在extern "C"
到处都是扔编译C
,不C++
,代码,但它仍然绊倒的Inner
作用域。
任何人都可以请帮助找出为什么这个编译器错误出现,我该如何解决呢?我必须使用g++
进行编译。
的问题是,如果没有改变是staticFuncs.h
代替staticFuncs.c
。
如果静态函数不是静态的,则问题不变。如果staticFuncs.h/c
内容被嵌入在ns.c
而不是被#include
编
的问题是保持不变。
»我已经尽我damnedest告诉克+ +我正在编译C,而不是C++«你为什么那么即使试图用C++编译器?只需使用'gcc'而不是'g ++'。 –
这是在工作中我没有选择编译器的问题的简化。 – StoneThrow
'extern“C”'对语言没有影响,它只影响如何生成符号。 – molbdnilo