在cpp中声明的全局变量在目标c中使用

时间:2013-03-07 16:19:58

标签: c++ ios objective-c xcode

我在xcode项目中使用cpp文件。在cpp文件中,我执行以下操作

  

ReadYML.h

typedef struct {
    float Position[3];
    float Color[4];
    float TexCoord[2];
} Vertex_OR;

extern Vertex_OR Vertices_OR [100];

extern GLubyte Indices_OR [30];
  

在ReadYML.cpp

中      

我为此分配了值。

     

在view.m

中      

我宣布“sample.h”

并尝试访问Vertices_OR和Indices_OR但收到以下错误?

Undefined symbols for architecture i386:
  "_Indices_OR", referenced from:
      loadyml() in ReadYMLfile.o
  "_Vertices_OR", referenced from:
      loadyml() in ReadYMLfile.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这是什么问题?我需要使用在“Sample.h”中声明的全局变量来访问view.m?有可能吗?

1 个答案:

答案 0 :(得分:2)

extern Vertex_OR Vertices_OR [100];

extern GLubyte Indices_OR [30];

extern表示“嘿,编译器,此符号存在某处”。如果你在某个编译单元中没有像下面这样的相应声明,你会得到那个链接错误(即将它放在相应的.m文件中):

Vertex_OR Vertices_OR [100];

GLubyte Indices_OR [30];