Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link to coco2d static library (1.1 beta 2) using workspaces in Xcode 4.3.1

I have done the following so far:

  1. Created workspace
  2. Added my project
  3. Added the Cocos2d iOS project (by dragging dropping into project navigator). It is a sibling of my project (not child).
  4. In my project, in build phase, linked to libcocos2d.a
  5. In build settings of my project - Set Always search paths to YES
  6. Created source tree variable pointing to folder containing cocos2d iOS xcodeproj file.
  7. In user header paths of my project, added source tree variable from step 6. Set as recursive. 8.In project navigator, clicked on libcocos2d.a library (it appears red). Set location "Relative to build products" You set this in the right most window in Xcode 4.3.1.
  8. Closed workspace, and opened pbxproj file in TextWrangler. Searched for path containing libcocos2d.a Remove path, so the entry only reads "libcocos2d.a" . This is because after compiling the library is in the same build directory of the app anyway.

Codesense and autocomplete work. But this will not compile. Around 70 linker errors pop up when I try to build, all saying something like:

Undefined symbols for architecture armv7: "_OBJC_CLASS_$_CCDirector", referenced from: objc-class-ref in AppDelegate.o objc-class-ref in ViewController.o "_kEAGLColorFormatRGB565", referenced from: -[RIAppDelegate applicationDidFinishLaunching:] in AppDelegate.o "_OBJC_CLASS_$_CCTexture2D", referenced from: objc-class-ref in AppDelegate.o "_OBJC_CLASS_$_EAGLView", referenced from: objc-class-ref in AppDelegate.o

If I try to build for the simulator, then instead of armv7, it says the symbols are missing for i386. The cocos2d project by itself compiles & builds fine. It is my project which has the above errors.

The cocos2d library is present in the build directory of my app after I try to build.

EDIT: Forgot to mention. I also edited the scheme for my project to not build in parallel. I also added the cocos2d target, and made it the top of the list (so it should build first).

EDIT: To clarify - after making the changes to the pbxproj file as detailed above, the libcocos2d.a file is no longer red. It looks normal. Also my app is also built, even though there are the link errors and the build fails. (It doesn't actually build, but the product is not "red". An app file is created in the same location as where the cocos2d library is produced.

like image 723
Rahul Iyer Avatar asked Mar 25 '12 12:03

Rahul Iyer


2 Answers

I've been trying for a while to use Cocos2d as a static library, instead of a template. The reason being that it makes it easier to develop my project in a modular fashion. In addition, it makes it easier to switch between cocos2d versions and so on. After searching the cocos2d-x forums I found this link: http://cfc.kizzx2.com/index.php/cocos2d-x-with-xcode-4-from-scratch-without-template/

I found out the frameworks that I needed to add, and that solved all linking problems.

I still haven't submitted an application to the store, so if someone with more experience could look it over to check if there are any pitfalls / errors it would be great. English is a second language for me, so please don't mind. This was done using cocos2d1.1beta2 and Xcode 4.3.1

  1. Create an Xcode workspace
  2. Navigate to the folder containing the cocos2d xcodeproj file (iOS / Mac). This is where you unzipped cocos2d after downloading it.
  3. Drag and drop the xcodeproj file into the navigator window of Xcode.(left most window).
  4. Click on the tiny triangle near the cocos2d project file so all its contents disappear.
  5. Add a new project to your workspace by right clicking in the navigator and "New Project". Make sure that the project is added as a sibling to the cocos2d project, and not as a child. I chose single window application (iOS)
  6. In this new project, go to the build settings, and change "Always Search User Paths" to "Yes"
  7. Add the location of your cocos2d folder to "User Header Search Paths". If you like you can instead add the cocos2d folder to your list of source trees in Xcode, and instead just fill in the name of the variable here. Eg: ${COCOS2D_SRC} . This is handy if later on you want to change the folder structure of build environment.

  8. Click on the target and select the build phases tab. You want to link the following binaries with your library. Make sure they are there. libcocos2d.a libz.dylib libxml2.dylib OpenGLES.framework QuartzCore.framework UIKit.framework Foundation.framework CoreGraphics.framework

If libcocos2d.a is "red", then don't worry about it. It still compiles / links properly.

  1. Under build settings, change "Build active architectures only" to YES.

Otherwise you may get an error ignoring file /Users/Pteriedaktyl/Library/Developer/Xcode/DerivedData/Build/Products/Debug-iphoneos/libcocos2d.a, file was built for archive which is not the architecture being linked (armv6)

  1. In your info.plist file, make sure to remove the entry inside "required device capabilities" that says armv7. Otherwise, your code will only run on newer devices.

Now you're set! You should be able to access the cocos2d api from the sibling project. If you ever want to change your version of cocos2d, you just need to change the cocos2d project that is in the workspace, update the source tree path (if necessary), and link to the new libCocos2d.a

Testing: Just to try it out, I modified the app delegate and view controller code to match the code from one of my old hello world projects.

Please try this out, and add to the thread if you have any problems.

like image 112
Rahul Iyer Avatar answered Oct 19 '22 02:10

Rahul Iyer


In the Utilities pane for the libCocos2d.a, underneath the Identity and Type where you set the library to relative to Build Products, scroll to the bottom and make sure that the Target Membership is selected for your targets. If it's not, it won't include it in the actual build made by the target. At least, that was the only way I was able to reproduce this issue.

like image 34
Peter Nolen Avatar answered Oct 19 '22 02:10

Peter Nolen