Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating an Objective-C++ Static Library in Xcode

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.

I created a new project called Skeleton and copied all of my engine files over to it. I used this guide 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:

  "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.

like image 223
LandonSchropp Avatar asked May 17 '10 04:05

LandonSchropp


People also ask

How do I create an Objective C project in Xcode?

To create a new Objective-C project select Create a New Xcode Project from the Welcome Screen. As an alternative you can select File > New > New Project… from the menu at the top of your screen. In the Mac OS X section left-hand pane select Application.

How do I create a static library in Xcode 13?

Show activity on this post. Open XCode and start a new project. Under iOS, select Library and “Cocoa Touch Static Library” say it as "staticlibrary". This will create a nice new project for us that builds a .

How do I create a library in Xcode?

Open Xcode application and select File > New > Swift Package... option or use ⌃⇧⌘ N keyboard shortcut. Alternatively, go to File > New > Project... and choose Swift Package template under Multiplatform option. Type your package name, choose your location, and hit the Create button.

What is a static library in Xcode?

Static library: A unit of code linked at compile-time, which does not change. (Can only contain code). The code that the app uses is copied to the generated executable file by a static linker during compilation time. However, iOS static libraries are not allowed to contain images/assets (only code).


2 Answers

Set the "-lstdc++" to Other Linker Flags

like image 182
dale Avatar answered Oct 10 '22 15:10

dale


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).

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.

like image 34
Michael Aaron Safyan Avatar answered Oct 10 '22 14:10

Michael Aaron Safyan