OSX:当所述符号存在时,C中未定义的符号

OSX:当所述符号存在时,C中未定义的符号,c,macos,C,Macos,奇怪的事情正在发生 我有一个C语言的静态库,使用CMake编译 我链接反对在Ubuntu上构建一个可执行文件,但在Snow Leopard下,当我尝试这样做时,我得到一个未定义的符号错误: per-ms006:mbuild douglasl$ make Linking C executable Sample Undefined symbols: "_na_Gfx_Impl", referenced from: _na_impl_render in libdesktop.a(imp

奇怪的事情正在发生

我有一个C语言的静态库,使用CMake编译

我链接反对在Ubuntu上构建一个可执行文件,但在Snow Leopard下,当我尝试这样做时,我得到一个未定义的符号错误:

per-ms006:mbuild douglasl$ make
Linking C executable Sample
Undefined symbols:
  "_na_Gfx_Impl", referenced from:
      _na_impl_render in libdesktop.a(impl.c.o)
ld: symbol(s) not found
collect2: ld returned 1 exit status
make[2]: *** [Sample] Error 1
make[1]: *** [CMakeFiles/Sample.dir/all] Error 2
make: *** [all] Error 2
这对我来说很神秘,因为静态库中的nm显示:

。。。(一堆东西省略了)

并使用:

/** Render implementation. */
int na_impl_render(struct na_Api *api) {
  struct na_Gfx_Impl *impl = (struct na_Gfx_Impl *) (api->gfx->impl);
  ... 

然而,我的结论是,一定有什么事情搞砸了。按照我的理解,没有理由在静态库中出现struct符号

我不确定这是否是您的确切问题,但在静态编译库时,链接的顺序往往很重要。如果要确保最低级别的函数是按顺序排列的,请参见此处的次要说明,静态库的代码布局为:impl.c、desktop.gfx.c、desktop.gfx.h。struct na_Gfx_Impl{…}是在desktop.Gfx.h中定义的,它包含在Impl.c
man nm
中,事实上,它会告诉你名字旁边神秘的“U”字符意味着符号没有定义。啊,是的,我没有注意到。问题是,为什么?它只是头文件中的一个结构。出于好奇,请尝试删除
api->gfx->impl
周围的括号。如果一个简单的结构导致链接错误,那么@user353820可能有更大的问题。
/** Implementation struct. */
struct na_Gfx_Impl {

  /** Parent. */
  struct na_Gfx *gfx;

  /** SDL surface for rendering. */
  SDL_Surface *screen;

  /** Handler for sprites. */
  struct na_utils_SetHandler *key;

  /** Set of texture values. */
  GLfloat *texture;

  /** Set of vextex values. */
  GLfloat *vertex;
};
/** Render implementation. */
int na_impl_render(struct na_Api *api) {
  struct na_Gfx_Impl *impl = (struct na_Gfx_Impl *) (api->gfx->impl);
  ...