代码之家  ›  专栏  ›  技术社区  ›  LandonSchropp

在Xcode中创建Objective-C++静态库

  •  11
  • LandonSchropp  · 技术社区  · 16 年前

    所以我为iPhone开发了一个引擎,我想用它构建几个不同的游戏。我不想在每个游戏的项目目录中复制和粘贴引擎的文件,我想从每个游戏中链接到引擎,所以如果我需要对它进行更改,我只需要这样做一次。经过一段时间的纠结,静态库似乎是在iPhone上实现这一点的最佳方法。

    我创建了一个名为“骨架”的新项目,并将所有引擎文件复制到该项目中。我用过 this 创建静态库的指南,我将库导入到名为Chooser的项目中。但是,当我试图编译这个项目时,XCODE开始抱怨我在一个名为CuffScEnE.mm的文件中包含的一些C++数据结构。以下是我的生成错误:

      "operator delete(void*)", referenced from:
    
    
          -[ControlScene dealloc] in libSkeleton.a(ControlScene.o)
    
    
          -[ControlScene init] in libSkeleton.a(ControlScene.o)
    
    
          __gnu_cxx::new_allocator<operation_t>::deallocate(operation_t*, unsigned long)in libSkeleton.a(ControlScene.o)
    
    
          __gnu_cxx::new_allocator<operation_t*>::deallocate(operation_t**, unsigned long)in libSkeleton.a(ControlScene.o)
    
    
      "operator new(unsigned long)", referenced from:
    
    
          -[ControlScene init] in libSkeleton.a(ControlScene.o)
    
    
          __gnu_cxx::new_allocator<operation_t*>::allocate(unsigned long, void const*)in libSkeleton.a(ControlScene.o)
    
    
          __gnu_cxx::new_allocator<operation_t>::allocate(unsigned long, void const*)in libSkeleton.a(ControlScene.o)
    
    
      "std::__throw_bad_alloc()", referenced from:
    
    
          __gnu_cxx::new_allocator<operation_t*>::allocate(unsigned long, void const*)in libSkeleton.a(ControlScene.o)
    
    
          __gnu_cxx::new_allocator<operation_t>::allocate(unsigned long, void const*)in libSkeleton.a(ControlScene.o)
    
    
      "___cxa_rethrow", referenced from:
    
    
          std::_Deque_base<operation_t, std::allocator<operation_t> >::_M_create_nodes(operation_t**, operation_t**)in libSkeleton.a(ControlScene.o)
    
    
          std::_Deque_base<operation_t, std::allocator<operation_t> >::_M_initialize_map(unsigned long)in libSkeleton.a(ControlScene.o)
    
    
      "___cxa_end_catch", referenced from:
    
    
          std::_Deque_base<operation_t, std::allocator<operation_t> >::_M_create_nodes(operation_t**, operation_t**)in libSkeleton.a(ControlScene.o)
    
    
          std::_Deque_base<operation_t, std::allocator<operation_t> >::_M_initialize_map(unsigned long)in libSkeleton.a(ControlScene.o)
    
    
      "___gxx_personality_v0", referenced from:
    
    
          ___gxx_personality_v0$non_lazy_ptr in libSkeleton.a(ControlScene.o)
    
    
          ___gxx_personality_v0$non_lazy_ptr in libSkeleton.a(MenuLayer.o)
    
    
      "___cxa_begin_catch", referenced from:
    
    
          std::_Deque_base<operation_t, std::allocator<operation_t> >::_M_create_nodes(operation_t**, operation_t**)in libSkeleton.a(ControlScene.o)
    
    
          std::_Deque_base<operation_t, std::allocator<operation_t> >::_M_initialize_map(unsigned long)in libSkeleton.a(ControlScene.o)
    
    
    ld: symbol(s) not found
    
    
    collect2: ld returned 1 exit status
    

    如果有人能提供一些有关这些问题发生的原因的见解,我将不胜感激。

    4 回复  |  直到 10 年前
        1
  •  18
  •   Michael Aaron Safyan    16 年前

    问题是您的库正在动态链接libstdc++。关于如何修复它,您应该在构建库时以各种组合尝试“-static”、“-static libstdc++”和“-static libgcc”(不确定需要哪种组合,但其中的某些组合应该使它完全静态)。

    编辑
    好吧,事实证明你可以在iPhone上动态链接libstdc++,所以实际上一个更好的解决方案是在你的构建中简单地放入“-lstc++”(也就是说,显式链接libstdc++)。

        2
  •  25
  •   dale    14 年前

    将“-lstdc++”设置为其他链接器标志

        3
  •  1
  •   LandonSchropp    16 年前

    我通过进入选择器的构建设置,搜索“编译源代码为”并选择Objective-C++,解决了这个问题。这可能是一个肮脏的解决方案,但它起作用了。

        4
  •  1
  •   Ege Akpinar    11 年前

    我在链接.framework时遇到了这个问题。我已经通过添加 空的 cppstub.mm 文件作为源(到 Compile Sources 阶段)

    我想当你这么做的时候,一定是强迫C++编译,不要问我为什么