Creating an SIO2 static library

SIO2 can be compiled into a static library, then used in a separate tutorial project.
Details can be found here….

http://blog.lyxite.com/2009/01/better-way-of-using-sio2-iphone-sdk.html

NOTE: There are some important corrections and clarifications in the comments section of that page.

The gist of it is this:

  1. Create a Cocoa Static Library project in Xcode. Xcode -> File -> New Project -> Static Library -> Cocoa Static Library, named sio2.
  2. Download sio2 sdk, extract it. In the folder SIO2_SDK_v1.3.1, locate 'src' folder.
  3. In Finder, copy that src (or move if you wish) into the project folder 'sio2'
  4. In Xcode, add the 'src' folder to the newly created project. To do so:
  5. Ctrl click on the project root, Add -> Existing Files -> Select the 'src' folder (note that this is the one in the sio2 folder)
  6. In the confirmation dialog, make sure 'copy items to destination folder' is UNCHECKED
  7. If you try to compile now, you get a few thousand errors, there are a few things you need to do in order make it compile
  8. In Xcode, delete a file named 'sio2_wrap.c' from the 'lua' group. Since I don't know whether this file is going to be useful in the future or not, just choose 'Delete References' when Xcode asks.
  9. Now, select the sio2 group, you will see a bunch of files named like 'sio2_xxxxx.h' or 'sio2_xxxxx.cc'. You need to select all of them (except README.txt) in the top-right view, hit the Get Info icon in the tool bar. In the General tab, change the file type to 'sourcecode.cpp.objcpp'.
  10. In Other Resources, open sio2_Prefix.pch. Replace '#import <Cocoa/Cocoa.h>' with '#import <Foundation/Foundation.h>'
  11. Now build. You get no error but a tiny warning. I assume that is not critical.
  12. Change the configurations, make sure you build for all the following 4 combinations. (if you are targeting 2.1, change version number from 2.0 to 2.1 accordingly).
  13. Device - 2.0 | Debug
  14. Device - 2.0 | Release
  15. Simulator - 2.0 | Debug
  16. Simulator - 2.0 | Release
  17. In Xcode, open your own game project
  18. Ctrl click on game project root, Add -> Existing Files, choose the sio2.xcodeproj we've just created. In the confirmation dialog, make sure 'copy items to destination folder' is UNCHECKED
  19. Once added, you will see a file name 'libsio2.a' under sio2.xcodeproj. Drag that file to Targets -> game_name -> Link Binary With Binaries.
  20. Remember we have built 4 version of the static library?
  21. Xcode is smart enough to know which one to link to, when you change the configuration in your own game project.
  22. Now you need to include a single header file 'sio2.h'. You can locate the header file by either add header search path to your project configuration, OR you may just place the sio2 project folder in parallel with your game folder and use a relative path like this: #import "../../sio2/src/sio2/sio2.h"
  23. You are now good to go. Enjoy the power of sio2 sdk.

Guard Variable Visibility Warnings

If you get >200 warnings when you link against the static library, something about guard variable visibility, read the information about c++ visibility support since gcc 4.0: http://gcc.gnu.org/wiki/Visibility

Basically you need to add the following to sio2.h:

// Generic helper definitions for shared library support
#if defined _WIN32 || defined __CYGWIN__
  #define FOX_HELPER_DLL_IMPORT __declspec(dllimport)
  #define FOX_HELPER_DLL_EXPORT __declspec(dllexport)
  #define FOX_HELPER_DLL_LOCAL
#else
  #if __GNUC__ >= 4
    #define FOX_HELPER_DLL_IMPORT __attribute__ ((visibility("default")))
    #define FOX_HELPER_DLL_EXPORT __attribute__ ((visibility("default")))
    #define FOX_HELPER_DLL_LOCAL  __attribute__ ((visibility("hidden")))
  #else
    #define FOX_HELPER_DLL_IMPORT
    #define FOX_HELPER_DLL_EXPORT
    #define FOX_HELPER_DLL_LOCAL
  #endif
#endif

// Now we use the generic helper definitions above to define FOX_API and FOX_LOCAL.
// FOX_API is used for the public API symbols. It either DLL imports or DLL exports (or does nothing for static build)
// FOX_LOCAL is used for non-api symbols.

#ifdef FOX_DLL // defined if FOX is compiled as a DLL
  #ifdef FOX_DLL_EXPORTS // defined if we are building the FOX DLL (instead of using it)
    #define FOX_API FOX_HELPER_DLL_EXPORT
  #else
    #define FOX_API FOX_HELPER_DLL_IMPORT
  #endif // FOX_DLL_EXPORTS
  #define FOX_LOCAL FOX_HELPER_DLL_LOCAL
#else // FOX_DLL is not defined: this means FOX is a static lib.
  #define FX_API
  #define FOX_LOCAL
#endif // FOX_DLL

Replace FOX_ if you want with something like SIO2_ for consistency. Make sure also that in your project's build settings, Symbols hidden by default is turned off, and that should be that.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License