Build C Library from makefile for ios and not macos - ios

I have a makefile that builds some C files and if I run it on an M1 mac the resulting library has the architecture arm64 which I thought it what is necessary for them to compile with an Xcode project for iOS. I discovered I can run the command otool -l libf2c.a | grep platform which should tell me what it was compiled for and in my case it returns platform1 which indicates macOS. Based on this, I think I need a value of platform2 for iOS.
The reason this is an issue is because in Xcode I get the error ld: building for iOS, but linking in object file built for macOS, file '/Users/e.../close.o' for architecture arm64.
Based on what I have been researching it seems iOS and macOS have the same architecture (arm64) but are a different 'platform'? But, I am not sure how the platform is determined. Is there some setting in my makefile I need to specify the platform? I am assuming that if I am able to get the platform to be iOS then Xcode will cooperate and be able to build the library I have generated.

The preferred way to compile for iOS via command line would probably be to use the xcrun command. This will allow you to specify the correct SDK for the platform you actually want to run on. For example:
prompt$ xcrun --sdk iphoneos --toolchain iphoneos clang -c test.c -o test.o -arch arm64
prompt$ otool -v -l test.o | grep platform
platform IOS
TL;DR: change your compiler invocation from plain clang to xcrun --sdk iphoneos --toolchain iphoneos clang.

Related

Build static lib for Apple Silicon mac simulator

My static lib is built with xcodebuild and then a fat lib is created from the simulator and device build result.
Here is my xcodebuild command:
xcodebuild OTHER_CFLAGS="-fembed-bitcode" -configuration "iphoneos" -target "${LIB_NAME}Common" -sdk iphoneos
xcodebuild OTHER_CFLAGS="-fembed-bitcode" -configuration "iphonesimulator" -target "${LIB_NAME}Common" -sdk iphonesimulator
lipo command:
lipo -create "${DEVICE_DIR}/lib${LIB_NAME}Common.a" "${SIMULATOR_DIR}/lib${LIB_NAME}Common.a" -output "${INSTALL_DIR}/include/${LIB_NAME}/lib${LIB_NAME}Common.a"
After checking the architectures in the fat lib, I got:
$ lipo -info MyLibCommon.a
Architectures in the fat file: MyLibCommon.a are: armv7 i386 x86_64 arm64
However, when I add the lib to a project via cocoapods and run the project on Apple's new Silicon (with arm64 chipset) on simulator, I got the following compile error:
building for iOS Simulator, but linking in object file built for iOS, file 'MyLibCommon.a' for architecture arm64
Excluding the arm64 architecture for Simulator is not an option because on Apple Silicon Mac has arm64 chipset.
Any idea how can I build a static library for Apple Silicon Simulator?
This is not possible.
In all likelihood, your simulator binaries are just i386 and x86_64. If you truly had arm64 iOS binaries and arm64 macOS binaries, lipo would error out on you:
fatal error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/lipo: test.a.ios and test.a.macos have the same architectures (arm64) and can't be in the same fat output file
This happens no matter whether you try it with full-fledged binaries, unlinked object files or static libraries. The reason is simply a shortcoming in the fat file format: you can only have one slice per architecture. You want both arm64 iOS and the Apple Silicon simulator, but that would be 2x arm64.
You might be tempted to try and build a single thin arm64 binary that works on both iOS and macOS, but that too is impossible. Binaries are platform-locked:
% otool -l test.o.ios | fgrep -B1 -A5 LC_BUILD_VERSION
Load command 1
cmd LC_BUILD_VERSION
cmdsize 24
platform 2
sdk 14.2
minos 14.2
ntools 0
% otool -l test.o.macos | fgrep -B1 -A5 LC_BUILD_VERSION
Load command 1
cmd LC_BUILD_VERSION
cmdsize 24
platform 1
sdk 11.0
minos 11.0
ntools 0
Notice the platform 2 vs platform 1. The kernel will actually ignore this load command, but dyld will not. And you can't have two such load commands in a single binary either, that's considered invalid as well.
You might also remember talk of a "Universal 2" file format from Apple's announcement or something citing that - but they lied. There is no "universal 2", it's exactly the same file format it was a decade ago. When they say "universal 2", all they mean is "adding an arm64 slice to your macOS binary".
The way I see it, you have three options:
You build separate libraries and keep the names separate.
You never build both architectures at the same time.
You build the simulator target for x86_64 and run it under Rosetta.
The latter two are both being suggested widely across the internet, with number 2 as "build only for active architecture" and number 3 being "exclude arm64". Given that Rosetta is expected to go away eventually, the third option seems not viable for the long term.

How to check a lib (static or dynamic) is built for iOS simulator or Mac OSX

My work is blocked by 'ld: warning: URGENT: building for iOS simulator, but linking in object file built for OSX' or vice versa, so I want to find out which platform a lib was built for.
I tried:
file tmp/openssl/lib/libcrypto.dylib
lipo -info tmp/openssl/lib/libcrypto.dylib
otool -hv -arch all tmp/openssl/lib/libcrypto.a
otool -hv -arch all /usr/local/opt/openssl/lib/libcrypto.a
and only thing I got is CPU type x86_64 or i386.
As originally described in this post, you can run otool -l /path/to/binary and inspect output for 'Load' commands. Output should contain LC_VERSION_MIN_IPHONEOS command for iOS binaries, and LC_VERSION_MIN_MACOSX for macOS.

Bitcode and dylib

I am trying to compile a C library to use it in my iOS project, and I want to embed bitcode.
I can successfully build static libraries targeting each arch. And those static library do contain bitcode (checked using otool), but the dynamic library doesn't contain bitcode. Why? Is bitcode not supported in dylib?
The library I am trying to build is xz. Here is the script
build_iOS()
{
ARCH=$1
if [ $ARCH == "i386" ] || [ $ARCH == "x86_64" ];
then
SDKROOT="$(xcodebuild -version -sdk iphonesimulator | grep -E '^Path' | sed 's/Path: //')"
else
SDKROOT="$(xcodebuild -version -sdk iphoneos | grep -E '^Path' | sed 's/Path: //')"
fi
export CC="$(xcrun -sdk iphoneos -find clang)"
export CFLAGS="-fembed-bitcode -isysroot $SDKROOT -arch ${ARCH} -miphoneos-version-min=9.0"
export LDFLAGS="-arch ${ARCH} -isysroot $SDKROOT"
if [ $ARCH == "i386" ] || [ $ARCH == "x86_64" ];
then
./configure --prefix=$XZPATH/build/iOS/$ARCH --host=i686-apple-darwin11 --disable-static --enable-shared
else
./configure --prefix=$XZPATH/build/iOS/$ARCH --host=arm-apple-darwin --disable-static --enable-shared
fi
make && make install && make clean
}
build_iOS i386
build_iOS x86_64
build_iOS armv7
build_iOS armv7s
build_iOS arm64
Thanks!
It looks like I cannot add bitcode to dylibs. I tried building several dylibs, then use otool -l path_to_dylib | grep bitcode to test if they contain any bitcode, all got nothing.
More evidence:
in Xcode(7.3.1), macOS (previously called OS X) targets don't have enable bitcode option in build settings
in the bitcode section of App Thinning, Apple didn't mentioned bitcode on macOS. Plus, App Thinning is only available on iOS, watchOS and tvOS.
I currently don't know why macOS apps don't have enable bitcode option in build setting. Maybe it's because Mac App store is not the only way for distributing mac apps? And people might copy one mac app from one mac app to another using USB sticks?
I was not able to verify bitcode in my bitcode enabled dynamic library via cmd line tools (otool, file or clang). Also comparing the diff between bitcode and non-bitcode build showed no difference, except the filesize.
Interestingly when using the dynamic library withouth bitcode enabled in an actual app and archiving xcode will fail to archive when I use the non-bitcode build:
ld: bitcode bundle could not be generated because '...' was built without full bitcode. All frameworks and dylibs for bitcode must be generated from Xcode Archive or Install build for architecture armv7
When building the dylib with bitcode enabled the filesize increases a lot and also xcode does not fail on archiving a sample project with bitcode enabled. So I am pretty sure that bitcode must be included in the dynamic lib, although we haven't found a way to verify that via cmd line tools yet...

Using custom built OpenCV for iOS on XCode produces ___sincos_stret undefined symbol

I'm trying to use a C++ static library into my iPhone app which uses a modified version of OpenCV for iOS and I'm stuck with this issue at linking time:
Undefined symbols for architecture armv7:
"___sincos_stret", referenced from:
cv::initInterTab2D(int, bool) in opencv2(imgwarp.o)
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I really do not understand what I'm missing, that function does not seem to be present in OpenCV and I do not find anything meaningful on the web; I'm wondering if it's in some mathematical library, but I haven't found anything yet.
I've compiled OpenCV with Clang and using the default libc++ library.
The libmylibrary.a and the OpenCV2 framework are correctly in the list of linked libraries.
I'm new to XCode, therefore I could have missed something trivial in the compilation of the static library and/or in the linking of it into my project.
I haven't changed the source code of that object as my changes were related to another part of the imgproc module of OpenCV, therefore I guess that this could have happen even using the default version.
Do you have any clues?
For fixing this problem with the Xcode 5 toolchain I specified the minimum supported iOS version as a compiler option to match the configuration in Xcode. For example:
-miphoneos-version-min=5.0
You can add this to the C and CXX flags in the makefile
CFLAGS += -miphoneos-version-min=5.0
CXXFLAGS += -miphoneos-version-min=5.0
For those who don't want to build with lower version of XCode, try changing python build script of OpenCV iOS. In build_framework.py, I added IPHONEOS_DEPLOYMENT_TARGET=6.0 in lines and rebuilt OpenCV for iOS.
os.system("xcodebuild -parallelizeTargets ARCHS=%s -jobs 8 -sdk %s -configuration Release -target ALL_BUILD" % (arch, target.lower()))
os.system("xcodebuild ARCHS=%s -sdk %s -configuration Release -target install install" % (arch, target.lower()))
to get
os.system("xcodebuild IPHONEOS_DEPLOYMENT_TARGET=6.0 -parallelizeTargets ARCHS=%s -jobs 8 -sdk %s -configuration Release -target ALL_BUILD" % (arch, target.lower()))
os.system("xcodebuild IPHONEOS_DEPLOYMENT_TARGET=6.0 ARCHS=%s -sdk %s -configuration Release -target install install" % (arch, target.lower()))
For me that fixed the issue. A nice read on the symbol __sincos_stret
TODO: Though that fixes the issue, in OpenCV.xcodeproj (in build folder) generated from the python script, it still has deployment target as iOS 7.0. There might be a cleaner way.
I ran into this problem after installing the XCode 5 developer preview and building OpenCV with the build_framework.py script. ___sincos_stret appears to be coming from using the new compiler version.
I fixed this problem by changing the path to the command-line tools.
In Terminal, verify the XCode command-line path:
xcode-select --print-path
If it prints a path inside XCode5-DP.app, then switch to the tools for Xcode 4:
xcode-select --switch /Applications/XCode.app/Contents/Developer
And rebuild the framework. Then try recompiling the project.
Per a quick search in support of Adam's question elsewhere, the symbol is defined in [path to SDK]/usr/lib/system/libsystem_m.dylib. Proof:
nm /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/usr/lib/system/libsystem_m.dylib | grep sincos
Rather than sticking to old versions of the tools or SDK, just make sure you're linking against that.

How to build Boost-Libraries for iPhone

Can someone tell me, where to find a detailed guide, how to build the Boost-Libraries for using it on the iPhone-Device.
I've allready build the libs for Mac and can use them in my project (only on iPhone-Simulator). While building the project for iPhone-Device, XCode haunts me a warning: "file is not of required architecture" ond some other errors.
Please Help
Start a new project in Xcode using the iPhone Static Library project template.
Then import the source and headers, and compile it that way. The result should be an iPhone compatible static library
I started here:
http://lists.boost.org/boost-build/2009/02/21326.php
With most of Boost you probably don't need to actually compile it, just include the useful headers. In my case, I just did the compiler define in my own Xcode project.
Hey I have updated Pete Goodliffes script in my openFrameworks addon:
It currently has arm64, armv7, i386, x86_64
Boost 1.59.0 or previous
libc++ / std=c++11 -- Now optional release for libstdc++
Precompiled and Script to build yourself (so if you need libstdc++ quite easy to change)
Supports Xcode 7
[https://github.com/danoli3/ofxiOSBoost][1]
For boost libraries which have only headers files (.hpp) you can just set header search path from your project to them.
For boost libraries with sources you can build static libraries for both ios phone/simulator with next simple steps:
Download and unpack a boost release archive (from https://www.boost.org/users/download/) e.g.: https://boostorg.jfrog.io/artifactory/main/release/1.77.0/source/boost_1_77_0.tar.bz2
Run bootstrap.sh with needed libraries to build for instance 'context' (format =library1,library2,...):
./bootstrap.sh --with-libraries=context
Add toolsets with correct paths to installed SDKs to project-config.jam:
# IOS ARM64
using clang : iphoneos
: xcrun clang -arch arm64 -stdlib=libc++ -std=c++11 -miphoneos-version-min=12.0 -fvisibility-inlines-hidden -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk
;
# IOS x86_64
using clang : iphonesimulator
: xcrun clang -arch x86_64 -stdlib=libc++ -std=c++11 -miphoneos-version-min=12.0 -fvisibility-inlines-hidden -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk
;
Create and run build.sh script (where lib name is libboost_<name>.a):
lib=libboost_context.a
dir='stage/lib'
# Build arm64
./b2 -a -j4 toolset=clang-iphoneos binary-format=mach-o abi=aapcs link=static stage
mv $dir/$lib $dir/arm64_$lib
# Build x86_64
./b2 -a -j4 toolset=clang-iphonesimulator binary-format=mach-o abi=sysv link=static stage
mv $dir/$lib $dir/x86_64_$lib
# Make fat
lipo -create $dir/arm64_$lib $dir/x86_64_$lib -output $dir/$lib
Now you have next compiled static libraries in "/stage/lib" dir for boost context: arm64_libboost_context.a, x86_64_libboost_context.a and fat one libboost_context.a.
We use boost too. To simplify its inclusion into new applications I have created a Xcode project you can drop into your workspace to include boost. It is based on a Makefile so you need the Xcode commandline tools installed.
The project is here https://github.com/Cogosense/iOSBoostFramework.
Clone the project into your workspace, then click on Menu File->"Add Files to workspace". Select iOSBoostFramework/iOSBoostFramework.xcodeproj in the file finder and click add.
The Makefile in the iOSBoostFramework directory controls what is built and how it is built. There is support for Xcode workspace dependencies, bitcode generation, and only the target architectures selected by Xcode are built.
The following libraries are built test, thread, atomic, signals, filesystem, regex, program_options, system date_time, serialization, exception, locale, and random.
All the separate libraries and architectures are combined, the final build output is a FAT boost.framework Framework bundle which can be linked into the application.
The version of boost is specified in the Makefile (currently 1.64.0), it is downloaded, built for all active architectures and installed in the BUILT_PRODUCTS_DIR specified by xcode.
The previous answer helped me when I wanted to build boost for the arm simulator. When you have a Mac with M1 processor and want to use the simulator, you cannot use the arm64 build for the iPhone.
I added this to the project-config.jam:
# IOS Arm Simulator
using clang : iphonesimulatorarm64
: xcrun clang -arch arm64 -stdlib=libc++ -std=c++11 -miphoneos-version-min=10.0 -fvisibility-inlines-hidden -target arm64-apple-ios10.0-simulator -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk ;
Then pass toolset=clang-iphonesimulatorarm64 to the b2 command.

Resources