How to link to static lib of cocos2d-x?

I see that when I use ‘cocos new’ to create a project, it always copies whole source code of cocos2d-x. I wonder if it is possible to link to the static libs.

I can build static libs using cocos2d_libs.xcodeproj and get several lib*.a files in cocos2d-x-3.2rc0/build/DerivedData/cocos2d_libs/Build/Products/Debug-iphoneos.

I tried to edit project.pbxproj of my test project directly using a text editor. I replaced $(SRCROOT)/…/cocos2d to $(SRCROOT)/…/…/…/cocos, which should be correct and pointing to the static libs. The project can compile, but cannot link. It seems Xcode cannot find the correct libs.

Has any else tried this approach and succeeded?

EDIT:
I think the LIBRARY_SEARCH_PATH is correct. But somehow I get this error:

ld: warning: ignoring file /Users/user/Downloads/Code/cocos2d-x-3.2rc0/build/DerivedData/cocos2d_libs/Build/Products/Debug-iphonesimulator/libCocosDenshion iOS.a, file was built for archive which is not the architecture being linked (i386): /Users/user/Downloads/Code/cocos2d-x-3.2rc0/build/DerivedData/cocos2d_libs/Build/Products/Debug-iphonesimulator/libCocosDenshion iOS.aignoring file /Users/user/Downloads/Code/cocos2d-x-3.2rc0/build/DerivedData/cocos2d_libs/Build/Products/Debug-iphonesimulator/libchipmunk iOS.a, file was built for archive which is not the architecture being linked (i386): /Users/user/Downloads/Code/cocos2d-x-3.2rc0/build/DerivedData/cocos2d_libs/Build/Products/Debug-iphonesimulator/libchipmunk iOS.a

I also compared build settings of cocos2dx_libs.xcodeproj and my test project and saw no big difference.

1 Like

why don’t you just add the static libraries to the Frameworks section of your Xcode project. Right click on it and add existing files. There should be no need to edit the .pbxproj file at all.

It’s just that find and replace is MUCH easier that manually editing many same strings in Xcode.

The link library problem is solved. It is actually not a problem - it turned out I have to clean and build for a specific target device.

What I have done is that do a clean build for a device and copy result lib*.a to a new location and repeat this step for other devices.

This brings up another question. It seems Xcode ‘hard-coded’ output location. It does not have a build setting for selected target device (such as $ARCHS). This will not let me configure final build output location, like what I do in Visual Studio.

it is, look in Xcode ->Preferences -> Locations and you can chance where files are outputted to.

I know the settings in Xcode ->Preferences, but that only gives control of the top output location. I want to gain control of exact sub directories. For example:

$TOP_DIR/Build/Build/Debug-$TARGET_DEVICE

But Xcode seems hard-coding the sub dir as follows:

$TOP_DIR/Build/Debug // for Mac
$TOP_DIR/Build/Debug-iphoneos
$TOP_DIR/Build/Debug-iphonesimulator

The problem is both 32-bit and 64-bit simulators share the same name - Debug-iphonesimulator. I believe this is also true for real physical devices. This leads to unwanted rebuild when switching between 32-bit and 64-bit builds.

oh, I see. You are correct and there is no way to change this. Xcode locus to rebuild all the time :slight_smile: