在 Xcode 中创建一个 Objective-C++ 静态库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2846679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating an Objective-C++ Static Library in Xcode
提问by LandonSchropp
So I've developed an engine for the iPhone with which I'd like to build a couple different games. Rather than copy and paste the files for the engine inside of each game's project directory, I'd a way to link to the engine from each game, so if I need to make a change to it I only have to do so once. After reeding around a little bit, it seems like static libraries are the best way to do this on the iPhone.
所以我为 iPhone 开发了一个引擎,我想用它来构建几个不同的游戏。我没有将引擎的文件复制并粘贴到每个游戏的项目目录中,而是一种从每个游戏链接到引擎的方法,因此如果我需要对其进行更改,我只需要这样做一次。在稍微摸索之后,似乎静态库是在 iPhone 上执行此操作的最佳方式。
I created a new project called Skeleton and copied all of my engine files over to it. I used thisguide to create a static library, and I imported the library into a project called Chooser. However, when I tried to compile the project, Xcode started complaining about some C++ data structures I included in a file called ControlScene.mm. Here's my build errors:
我创建了一个名为 Skeleton 的新项目,并将我所有的引擎文件复制到其中。我使用本指南创建了一个静态库,并将该库导入到一个名为 Chooser 的项目中。然而,当我尝试编译项目时,Xcode 开始抱怨我包含在一个名为 ControlScene.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
If anybody could offer some insight as to why these problems are occuring, I'd appreciate it.
如果有人能就为什么会出现这些问题提供一些见解,我将不胜感激。
回答by dale
Set the "-lstdc++" to Other Linker Flags
将“-lstdc++”设置为其他链接器标志
回答by Michael Aaron Safyan
The problem is that your library is dynamically linking against libstdc++. As to how to fix it, you should try "-static", "-static-libstdc++", and "-static-libgcc" in various combinations when building your library (not sure which of them are needed, but some combination thereof should make it fully static).
问题是您的库正在动态链接 libstdc++。至于如何修复它,您应该在构建库时尝试各种组合中的“-static”、“-static-libstdc++”和“-static-libgcc”(不确定需要哪一个,但应该有一些组合)使其完全静态)。
Edit
Well, it turns out that you are permitted to dynamically link against libstdc++ on the iPhone, so actually a better solution is to simply put "-lstdc++" in (that is, explicitly link against libstdc++) in your build.
编辑
好吧,事实证明你可以在 iPhone 上动态链接 libstdc++,所以实际上更好的解决方案是简单地将“-lstdc++”放入你的构建中(即明确链接 libstdc++)。
回答by LandonSchropp
I fixed the problem by going into the build settings for Chooser, searching out "Compile Source As" and selecting Objective-C++. This is probably a dirty solution, but it worked.
我通过进入选择器的构建设置,搜索“编译源为”并选择 Objective-C++ 解决了这个问题。这可能是一个肮脏的解决方案,但它奏效了。
回答by Ege Akpinar
I ran into this issue when trying to link a .framework. I've managed to fix it by adding an emptycppstub.mm
file as source (to Compile Sources
phase)
我在尝试链接 .framework 时遇到了这个问题。我设法通过添加一个空cppstub.mm
文件作为源(Compile Sources
阶段)来修复它
I guess it must be forcing some sort of C++ compilation when you do this, don't ask me why
我猜你这样做时一定是在强制进行某种 C++ 编译,别问我为什么